Wednesday, March 31, 2010

Exception Logging in SharePoint

There are several methods of logging exceptions in SharePoint such as in SharePoint Logs, Event Viewer, etc. In this post, I would like to discuss logging SharePoint exceptions in Logs folder of SharePoint 12 hive programmatically.

Add Microsoft.Office.Server dll as a reference to your project

Add following line in using section

using Microsoft.Office.Server.Diagnostics;

 

PortalLog.LogString("Source:", “YourApplicationName”, “TypeofError”, “ActualExceptionMessageGoesHere”);

 

regards,

Sudhakar Bulli

Thursday, March 25, 2010

Using People Editor Control in SharePoint

People Editor or People Picker is SharePoint control which represents a PeopleEditor object in a PeoplePicker control. Would like to discuss the usage of this control in a SharePoint custom page as well as programming with this control.

First you need to add Microsoft.SharePoint reference to your project and then add following line in register tag section

<%@ Register TagPrefix="wss" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Once we add a reference to the namespace containing People Editor control, it is ready to use. It can be added as below

<wss:PeopleEditor id="peopleEditorTest" runat="server" MultiSelect="false" ValidatorEnabled="true" />

I am not discussing all the attributes/members of the control which can be read from the msdn article http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.peopleeditor.aspx

Now let us see how this can be used through SharePoint object model. Add an object in the cs page to work with the control

using Microsoft.SharePoint.WebControls;

protected PeopleEditor peopleEditorTest;

Below code can be used wherever you want to save the value of the people editor control such as updating list item, etc

string strValue = string.Empty;

if (peopleEditorTest!= null)
  {
    if (!string.IsNullOrEmpty(peopleEditorTest.CommaSeparatedAccounts))

strValue = peopleEditorTest.CommaSeparatedAccounts;
}

You can get other values such as user name, description, etc by type casting it to Picker Entity.

PickerEntity pe = (PickerEntity)peopleEditorTest.Entities[0]; //gets first user in list
string username = pe.Description;

That’s it.

 

Regards,

Sudhakar Bulli.