Kirk Evans is a Microsoft Architect for the Azure Center of Excellence.
Introduction to SharePoint and Azure IaaS
Building SharePoint Apps with Windows Azure Platform as a Service
SharePoint Solutions and Architectures on Windows Azure Infrastructure Services
Understanding Authentication and Permissions with Apps for SharePoint and Office
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.
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.
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.
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");
User newUser = communitySite.EnsureUser("i:email@example.com");
FieldUserValue userValue = new FieldUserValue();
userValue.LookupId = newUser.Id;
List members = communitySite.Lists.GetByTitle("Community Members");
Microsoft.SharePoint.Client.ListItem item =
item["Member"] = userValue;
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.
How to Allow Only Users Who Have a Community Badge to Your SharePoint 2013 Site
How can I set a SharePoint Person or Group Field as multiple users with CSOM?
Apologies, I am not sure off the top of my head. Try posting your question to stackoverflow.com/.../sharepoint.
Thanks a lot , it worked with a bit of tweaking for only persons type