Friday, 8 November 2013

SharePoint People Picker using c#

Share it Please
<SharePoint:PeopleEditor ID="spPeoplePicker" runat="server" Width="350" SelectionSet="User" />

            SPSite oSPsite = new SPSite(SPContext.Current.Site.ID);
            SPWeb oSPWeb = oSPsite.OpenWeb();
            SPList list = oSPWeb.Lists["PeopleList"];
            oSPWeb.AllowUnsafeUpdates = true;
            int iPeople = spPeoplePicker.ResolvedEntities.Count;
             SPListItem itemToAdd = list.Items.Add();
            for (int i = 0; i < iPeople; i++)
            {
                PickerEntity peEntity = spPeoplePicker.ResolvedEntities[i] as PickerEntity;
                SPUser user = SPContext.Current.Web.EnsureUser(peEntity.Key);
                itemToAdd["Title"] = "Test Title";
                itemToAdd["User"] = user;  
                itemToAdd.Update();
            }

To Clear the PeoplePicker

            spPeoplePicker.Accounts.Clear();
            spPeoplePicker.Entities.Clear();

1 comment:

Followers

Follow The Author