The People Column (Reading/Updating/Querying)
The People Column
The People Column stores a SharePoint user or users and allows easy access to their profile and associated data (name, email, access rights, etc.). For many environments, the user profiles are synchronized with a directory service, such as Active Directory.
When you add the People Column to a list or library, you can select ‘Yes’ for the Allow multiple selections option in order to store several users in that column.
Reading a Single Selection People Column
When retrieving a value stored in the People Column, you must use cast the stored value as an SPFieldUserValue object, with the SPWeb and the string value in the field as arguments, before you can use the User property as an SPUser object. Once you do that, the members of the SPUser object can be accessed.
1 2 3 4 5 6 7 8 9 10 11 |
SPWeb thisWeb = SPContext.Current.Web; SPListItem item = thisWeb.Lists["Sample List"].GetItemById(1); if (item["Manager"] != null && item["Manager"].ToString() != "") { SPFieldUserValue userValue = new SPFieldUserValue(thisWeb, item["Manager"].ToString()); SPUser user = userValue.User; Label1.Text = user.Name; } |
Update a Single Selection People Column
Use a SharePoint:PeopleEditor control to allow the user to select a person or group. (Check to make sure the SharePoint tag is registered at the top of the ACSX file.)
The ACSX file:
1 2 3 4 5 6 7 8 |
<!-- First register the SharePoint tags so you can use SharePoint controls --> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="…" %> <!-- Drop in the PeopleEditor Control --> <SharePoint:PeopleEditor ID="PeopleEditor1" runat="server" /> <!-- Also drop in a Save button --> <asp:Button ID="SaveBtn" runat="server" Text="Save" OnClick="Save_Person" /> |
The ACSX.CS file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
//You also need to include this namespace import at the top of the ACSX.CS file using Microsoft.SharePoint.WebControls; // … protected void Save_Person(Object sender, EventArgs e) { //Determine scope SPWeb thisWeb = SPContext.Current.Web; SPListItem item = thisWeb.Lists["Sample List"].AddItem(); //Check if a resolved user has been entered in the PeopleEditor if (PeopleEditor1.ResolvedEntities.Count > 0) { //Get the resolved user PickerEntity entity = PeopleEditor1.ResolvedEntities[0] as PickerEntity; //Use the SPWeb’s method EnsureUser(key) to create an SPUser object SPUser user = thisWeb.EnsureUser(entity.Key); //SPUser object can be assigned to the People Column directly item["Manager"] = user; } item.Update(); } |
Reading a Multiple Selection People Column
It’s not really much different to deal with a multiple value People Column, just know that it stores as a string of serialized values. First get all the values as an SPFieldUserValueCollection using the constructor, and pass the SPWeb object and string form of the value as the arguments. Then you can iterate through the collection and convert the SPFieldUserValue.User property as an SPUser object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//Defining the scope is necessary when dealing with SPFieldUserValues SPWeb thisWeb = SPContext.Current.Web; SPListItem item = thisWeb.Lists["Sample List"].GetItemById(1); if (item["Managers"] != null && item["Managers"].ToString() != "") { SPFieldUserValueCollection userValues = new SPFieldUserValueCollection(thisWeb,item["Managers"].ToString()); //Print name and email of each selected user foreach (SPFieldUserValue userValue in userValues) { SPUser user = userValue.User; Label1.Text += user.Name + "(" + user.Email + ")" + ", "; } } |
Updating a Multiple Selection People Column
Again, use a SharePoint:PeopleEditor control with the MultiSelect property set to true. (Check to make sure the SharePoint tag is registered at the top of the ACSX file.)
The ACSX file:
1 2 3 4 5 6 7 8 |
<!-- First register the SharePoint tags so you can use SharePoint controls --> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="…" %> <!-- Drop in the PeopleEditor Control --> <SharePoint:PeopleEditor ID="PeopleEditor1" runat="server" MultiSelect="true" /> <!-- Also drop in a Save button --> <asp:Button ID="SaveBtn" runat="server" Text="Save" OnClick="Update_Users" /> |
Then, with some sort of server event (typically an asp:Button OnClick event), iterate through the entities in the PeopleEditor and add the SPUser, converted to an SPFieldUserValue, to an SPFieldUserValueCollection object. (Notice that creating an SPFieldUserValue object requires three arguments, the SPWeb, the SPUser.ID, and the display field; pass in the SPUser.Name if you selected ‘Show field: Name’ when setting up the column.)
The ACSX.CS file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
protected void Update_Users(Object sender, EventArgs e) { //Determine scope SPWeb thisWeb = SPContext.Current.Web; SPListItem item = thisWeb.Lists["Sample List"].GetItemById(1); //Create an empty SPFieldUserValueCollection SPFieldUserValueCollection users = new SPFieldUserValueCollection(); //Get all entities in PeopleEditor for (int p = 0; p < PeopleEditor1.ResolvedEntities.Count; p++) { PickerEntity entity = PeopleEditor1.ResolvedEntities[p] as PickerEntity; SPUser user = thisWeb.EnsureUser(entity.Key); SPFieldUserValue userValue = new SPFieldUserValue(thisWeb, user.ID, user.Name); users.Add(userValue); } item["Managers"] = users; item.Update(); } //end Update_Users |
Querying a People Column with CAML
When querying a list using values from the People field, you can use Value Type=’User’ to select by the display field. If you are using the ID number, then you will be using Value Type=’Integer’ – don’t forget to set LookupId=’TRUE’ (this is case-sensitive). Use the SPUser.EnsureUser function to get the user so that you can filter by their ID.
1 2 3 4 5 6 7 8 9 10 11 |
//Filter by Name SPWeb thisWeb = SPContext.Current.Web; SPQuery query = new SPQuery(); query.Query = string.Concat( "<Where>", "<Eq>", "<FieldRef Name='Created_x0020_By' />", "<Value Type='User'>John Smith</Value>", "</Eq>", "</Where>"); |
1 2 3 4 5 6 7 8 9 10 11 12 |
//Filter by ID SPWeb thisWeb = SPContext.Current.Web; SPUser user = thisWeb.EnsureUser("DOMAIN\\jsmith"); SPQuery query = new SPQuery(); query.Query = string.Concat( "<Where>", "<Eq>", "<FieldRef Name='Created_x0020_By' LookupId='TRUE' />", "<Value Type='Integer'>", user.ID, "</Value>", "</Eq>", "</Where>"); |
More Information