06 Mar

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.

Allow multiple selections

 


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.


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:

The ACSX.CS file:


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.


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:

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:


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.


More Information

 

Copyright © 2014 Keren Apura; Fruitful theme by fruitfulcode