SharePoint Development Blog

Nick Boumans
View my LinkedIn Profile Follow me on Twitter View my Profile on FaceBook View my projects on CodePlex View my presentations on SlideShare



Recent posts

Tags

Categories

Navigation

Pages

Archive

Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Log Exceptions to Default SharePoint LogFile

It's possible to log exceptions using the next codesnippet (MOSS Only). Advantage: exceptions are logged to a single file (i.e. in the 12 hive under the LOGS directory). No extra dll’s are needed to be installed. The only limitation to using the MOSS logger is that you cannot set the log event level e.g. critical, high, medium, low it will always display the error level as high. 

catch(Exception myException)
{
Microsoft.Office.Server.Diagnostics.PortalLog.LogString(”Exception
Occurred: {0} || {1}”, myException.Message, myException.StackTrace);
}

The MOSS logger is located in the Microsoft.Office.Server.dll and therefore it is only available with the MOSS install, not WSS 3.0. For more control and flexibility use the Enterprise library logger.

Posted: Nov 02 2008, 18:00 by Nick Boumans | Comments (0) RSS comment feed |
  • Currently 4.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: SharePoint General

Adding a List - ListFields and Items from code

Often it is required to insert a list, listitem and listitems from code. An example is creating a list if it not exists from a Feature or WebPart. By creating a list we out of the box get the Title field. The next code shows the process of creating a custom list, columns and items from code. It also shows how you can change to out-of-the-box Title column.

// Option 1) Inside SharePoint
SPSite siteCollection = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;

// Option 2) From an external tool
SPSite siteCollection = new SPSite("http://myurl");
SPWeb site = siteCollection.RootWeb;

// Get a ListCollection from the current site
SPListCollection lists = site.Lists;

// Add a custom List to the SharePoint site
lists.Add("Employee", "List containing employees", SPListTemplateType.GenericList);
// Put the new list in a variable
SPList employeeList = lists["Employee"];
// Get the out of the box fields (Title, Attachment etc)
SPFieldCollection fields = employeeList.Fields;
// Change the default title column           
SPField field = fields["Title"];
field.Title = "Name";
// Don't forget the update to commit changes
field.Update();   

// Add a new field
SPField fieldAdd = fields[fields.Add("Address", SPFieldType.Text, true)];

SPView view = employeeList.DefaultView;
SPViewFieldCollection viewFields = view.ViewFields;

// Add the field to the defaultview (all items)
viewFields.Add(fieldAdd);

view.Update();

// Add a record to the new list
SPListItem item = employeeList.Items.Add();
item["Name"] = "Name of the Employee";
item["Address"] = "Address of the employee";

item.Update();

[/code]

 

Posted: Jul 19 2008, 00:00 by Nick Boumans | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Find the public key token of an assembly using Strong Name Tool in Visual Studio

  1. In Visual Studio 2005/2008, click Tools -> External Tools    
  2. Click Add and enter the following fields   
  3. Title: Get Public Key    
  4. Command: C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe    
  5. Arguments: -Tp "$(TargetPath)"    
  6. Uncheck all options, except Use Output window 

After building the assembly you can get the public key (if signed) by clicking Tools -> Get Public Key

MSDN SN.EXE

Note: On some systems I found the sn.exe tool at the location: C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin

Posted: Apr 12 2008, 00:00 by Nick Boumans | Comments (1) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: SharePoint General

Loading UserControls (.ASCX) in a Web Part

One of the things we are missing is the ability to Visual Design WebParts in Visual Studio. We can load a UserControl into a WebPart by calling the Page.LoadControl method. It's a best practice to make a directory in your virtual directory called usercontrols and place the UserControls in it to load it from a WebPart. Note that this is the key concept of the SharePoint SmartPart.

[code:c#]
 private UserControl usercontrol;
private Button button;
private TextBox textbox;
private Label label;

public UserControlWebPart()
{
}

protected override void CreateChildControls()
{
    try
    {
        usercontrol = (UserControl)Page.LoadControl(@"/_controltemplates/usercontrols/MyUserControl.ascx");
        this.Controls.Add(usercontrol);
        if (usercontrol != null)
        {
            button = (Button)usercontrol.FindControl("Button1");
            button.Click += new EventHandler(button_Click);
            label = (Label)usercontrol.FindControl("Label1");
            textbox = (TextBox)usercontrol.FindControl("TextBox1");
        }
    }
    catch (Exception ex)
    {
        EventLog.WriteEntry("WebParts", "Custom WebPart" + ex.ToString());              
    }
}

void button_Click(object sender, EventArgs e)
{
    label.Text = textbox.Text;
}

 

Posted: Apr 07 2008, 00:00 by Nick Boumans | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Web Parts