Sharepoint set user or group to SPListItem

We are going to use a sharepoint web control to get user or groups  named “PeopleEditor” .For using these please fallow the instructions below.

Add fallowing code to top of your page:

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

Add fallowing control to your page front for get users or groups:

Note: If  you want to set or retrieve the account types that are associated with the control use attribute “SelectionSet”
and enums are:

  • User = single user
  • DL = AD distribution list
  • SecGroup = AD security group
  • SPGroup = Sharepoint group

<SharePointWebControls:PeopleEditor runat=”server” ID=”txtScope” MultiSelect=”false” MaximumEntities=”1″ ValidatorEnabled=”true” SelectionSet=”User” Width=”300px”></SharePointWebControls:PeopleEditor>

Add fallowing to your page behind:

using Microsoft.SharePoint.WebControls;
public SPFieldUserValueCollection GetSelectedUsers(PeopleEditor editor)
       string selectedUsers = editor.CommaSeparatedAccounts;
       // commaseparatedaccounts returns entries that are comma separated. we want to split those up
       char[] splitter = { ',' };
       string[] splitPPData = selectedUsers.Split(splitter);
       // this collection will store the user values from the people editor which we'll eventually use
       // to populate the field in the list
       SPFieldUserValueCollection values = new SPFieldUserValueCollection();
       // for each item in our array, create a new sp user object given the loginname and add to our collection
       for (int i = 0; i < splitPPData.Length; i++)
                string loginName = splitPPData[i];
                if (!string.IsNullOrEmpty(loginName))
                    SPUser user = SPContext.Current.Web.SiteUsers[loginName];
                    SPFieldUserValue fuv = new SPFieldUserValue(SPContext.Current.Web, user.ID, user.LoginName);
            return values;

And now i describe add to listitem and update:

SPList list = SPContext.Current.Web.Lists[APPLIB];
SPListItem item = list.Items.Add();
item["ProgramAdmin"] = GetSelectedUsers(txtScope);

That’s all.