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.
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]
Find the public key token of an assembly using Strong Name Tool in Visual Studio
- In Visual Studio 2005/2008, click Tools -> External Tools
- Click Add and enter the following fields
- Title: Get Public Key
- Command: C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe
- Arguments: -Tp "$(TargetPath)"
- 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
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;
}