Friday 22 November 2013

How to Update People Picker Field Programmatically

People picker field is a SharePoint column that allows you to select users, distribution lists, and security groups within the SharePoint Foundation user interface. If you have a user's requirement that needs to update the people picker field programmatically, you can try the following solution.

In your ascx page, add a hidden SharePoint people picker field (where visible property is equal to false) by using the following syntax:

<spuc:PeopleEditor AllowEmpty="false" ValidatorEnabled="true" id="userPicker" runat="server" ShowCreateButtonInActiveDirectoryAccountCreationMode="true" SelectionSet="User" MultiSelect="false" Visible=false />

But in order to use this field in a page, you need to add a directive at the top of the page:

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

In code behind, add the following:

PickerEntity entity = new PickerEntity();
entity.Key = vr.RName //display name;
entity.DisplayText = vr.RName //display name;
entity = userPicker.ValidateEntity(entity);
                       
PickerEntity entity2 = new PickerEntity();
entity2.Key = entity.Description;
entity2 = userPicker.ValidateEntity(entity2);

                       
SPFieldUserValue fuv = new SPFieldUserValue(web, Convert.ToInt32(entity2.EntityData[PeopleEditorEntityDataKeys.UserId]), entity2.Description);


// "RName" is the name of the sharepoint people picker field to update
listItem["RName"] = fuv;

HandleEventFiring eventFiring = new HandleEventFiring();
eventFiring.AccDisableEventFiring();
listItem.Update();
eventFiring.AccEnableEventFiring();


-------------------

public class HandleEventFiring : SPItemEventReceiver
        {
            public void AccDisableEventFiring()
            {
                this.DisableEventFiring();
            }

            public void AccEnableEventFiring()
            {
                this.EnableEventFiring();
            }
        }







    

No comments:

Post a Comment