Thursday, March 5, 2009

How to add sharepoint DateTime Control and People Picker in custom application page

People Picker :
The control is actually called a "
PeopleEditor" and is in the 'Microsoft.SharePoint.WebControls' namespace. You will need to add a reference to 'Microsoft.Sharepoint.dll' to your project in Visual Studio, which is something you probably already have if you are writing anything that works with SharePoint.

First, we need to add a reference to the Namespace containing our control:

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

Next, add the PeopleEditor control to your page:

<wssawc:PeopleEditor
AllowEmpty="false"
ValidatorEnabled="true"
id="userPicker"
runat="server"
ShowCreateButtonInActiveDirectoryAccountCreationMode="true"
SelectionSet="User" /
%>

Next, add an object on the server to work with the control:

using Microsoft.SharePoint.WebControls; //add a reference to Microsoft.SharePoint.dll if needed

public class MyPageName : Page
{
protected PeopleEditor userPicker;

}
Now, add your code needed to retrieve the entities:

public void btnSave_Click(object sender, System.EventArgs e)
{
….
PickerEntity pe = (PickerEntity)userPicker.Entities[0]; //gets first user in list
string username = pe.Description;

SPUser user=web.AllUser[username];

}

Notes:
The Description property will return the full account name (e.g. domain\username)
The DisplayText property will return the resolved name in the editor control (e.g. First Last)


Date Time Control :


You can create user control webpart for that and add this webpart to sharepoint environment othrewise you can get error like " object reference can not set".

Cheers it and enjoy.