Setting a SharePoint Person or Group Field Value with CSOM

I recently was asked a question about setting a Person or Group field value in a list using CSOM.  This post shows how to programmatically set a user or group field value using CSOM.

Background

A developer wants to programmatically add users as members to a Community site in SharePoint 2013.  Once the users have adequate permissions to the site, they still need to join the community, which adds a new list item in the Community Members list.  The Community Members list in a Community site has a few columns, but the only one that really matters is the Member column.

image

The Member column is of type Person or Group.  Most of the CSOM examples you will see use simple types, such as text or numbers, how can we programmatically set a Person or Group field?  Turns out it is pretty easy.

The Code

I created a provider-hosted app to demonstrate this using managed code, but could have done this with JavaScript in a SharePoint-hosted app as well.  

I am using the new SharePointContext in Visual Studio 2013 to greatly simplify working with OAuth tokens in SharePoint apps.  We obtain a reference to the host web, then call OpenWeb to obtain a reference to another web within the site collection.  Load the site and execute the query, and we can now access properties from the web. 

I am using Office 365 for this demonstration, so we use the claims-encoded value for the account name to check whether the specified logon name belongs to a valid user of the website, and if the logon name does not already exist, adds it to the website.

Once we ensure the user account, we next use the FieldUserValue type.  We get the user’s ID and set it as the LookupId property, then set the Member field value to the FieldUserValue type.  Call update, and the new user value is set.

 protected void Page_Load(object sender, EventArgs e)
{
    var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

    using (var clientContext = spContext.CreateUserClientContextForSPHost())
    {
        Web communitySite = clientContext.Site.OpenWeb("Community");
        clientContext.Load(communitySite);
        clientContext.ExecuteQuery();

        User newUser = communitySite.EnsureUser("i:0#.f|membership|allieb@kirke.onmicrosoft.com");
        clientContext.Load(newUser);
        clientContext.ExecuteQuery();

        FieldUserValue userValue = new FieldUserValue();
        userValue.LookupId = newUser.Id;

        List members = communitySite.Lists.GetByTitle("Community Members");
        Microsoft.SharePoint.Client.ListItem item = 
 members.AddItem(new ListItemCreationInformation());
        item["Member"] = userValue;
        item.Update();
        clientContext.ExecuteQuery();
    }
}

The Result

The result is pretty uninteresting, as you can expect it simply adds a new list item to the Community Members list.  You can see the result by inspecting the Members of the Community site.  Just by using the FieldUserValue type, the user’s information and picture are properly set without us having to write any code to support it.

image

For More Information

FieldUserValue Class

Web.EnsureUser Method

How to Allow Only Users Who Have a Community Badge to Your SharePoint 2013 Site