Wednesday, May 20, 2009

How to set current user in the PeopleEditor via Code

The following code sample will set the currently logged on user to a PeopleEditor:

using (SPWeb web = SPContext.Current.Web)
{
SPUser user = web.CurrentUser;
PickerEntity entity = new PickerEntity();
entity.Key = user.LoginName.ToString();
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
entityArrayList.Add(entity);
PeopleEditorControl.UpdateEntities(entityArrayList);
}

3 comments:

Anonymous said...

thank you very much. it was very useful!

Sebastian Bouckaert said...

You make a sever error in your code my friend.The using statement should not be there
this will dispose the current SharePoint context
just remove the using statments

Albert Andrada said...

Really Nice Information,Thank You Very Much For Sharing.
Web Development Company