Querying User Information From Sharepoint People Columns

What are People Fields

People fields in SharePoint allow storing and retrieving data about users and their profiles. They are commonly used to assign tasks, tag documents, populate user profile information, and enable social features. People fields store a unique user ID that points to the user’s account in SharePoint’s user profile store. The user ID can be used to retrieve detailed user profile information through SharePoint’s client-side object model or REST APIs.

Common use cases for people fields include:

  • Assigning documents or tasks to specific users
  • Tagging content with responsible users for tracking and discovery
  • Populating user profile data like name, picture, department etc. in lists and libraries
  • Enabling social collaboration through tags, mentions, activity feeds etc.

Storing user data and profile information

When a user is chosen in a people field, SharePoint stores the unique user ID associated with their profile in the site user profile store. This connects the list item or document to the user’s account and profile information. People fields can map to different user profile properties like name, email, department etc. and display them in views and forms.

SharePoint provides user profile synchronization and management interfaces to populate user data from corporate directories like Active Directory. User profiles can also be managed manually or updated through APIs. This profile data can be surfaced in people fields across the site.

Querying User Profiles with REST API

Overview of REST API for user profiles

SharePoint’s REST API provides several endpoints to retrieve user profile data, upload profile pictures and manage user properties. The main endpoints related to user profiles are:

  • /_api/SP.UserProfiles.PeopleManager – Get user profile properties
  • /_api/SP.UserProfiles.PeopleManager/SetSingleValueProfileProperty – Set user profile property values
  • /_api/SP.UserProfiles.PeopleManager/CreatePersonalSiteEnque – Create personal site
  • /_api/SP.UserProfiles.PeopleManager/GetPropertiesFor – Get user profile property definitions

These APIs can be called from client-side JavaScript code, PowerShell scripts or other custom applications using standard HTTP requests and JSON or XML responses.

Example request to get user properties

Here is an example GET request to retrieve specific user profile properties using the REST API:

  GET https://siteurl/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='i:0%23.f|membership|[email protected]'&$select=Title,Department,Manager

This returns a JSON response with the matching user profile properties:

{
  "Title": "Sales Associate", 
  "Department": "Sales",
  "Manager": "12;#[email protected]"
}

Handling the response data

The response contains user identity IDs encoded in a special format. These IDs point to user profiles stored in SharePoint’s user profile store. To retrieve full user profile information, additional requests may be required to resolve and dereference these IDs.

Use the SP.UserProfiles.PeopleManager.getPropertiesFor method to convert IDs into user properties. The returned properties can then be displayed in the user interface or processed in custom logic.

Retrieving User Details from People Picker Fields

How people pickers store user IDs

When a user is selected in a people picker field in a SharePoint list form, the user’s ID is stored in the field along with some metadata. The common formats used are:

  • 12;#User Name|DOMAIN\Username – For on-premises Active Directory users
  • 16;#[email protected]|GUID – For SharePoint online / Office 365 users

This encoded string contains the user’s account name or email for mapping the ID to their profile. The ID can be separated and resolved into the user’s name and other properties.

Fetching user details with GetUserProfileByName()

SharePoint provides a utility function GetUserProfileByName() to retrieve user profile details from a people picker ID value:

  var encodedValue = "12;#[email protected]|DOMAIN\\jsmith";
  var userProfile = GetUserProfileByName(encodedValue);

This returns an object with commonly used properties like user name, email, picture URL etc. which can be directly rendered in the user interface.

Example code to display user name and picture

  <html>
  <head>
  <script type="text/javascript">
    function GetUserInfo() {
      var userValue = $("input[title='Assigned To']").val(); 
      var profile = GetUserProfileByName(userValue);

      $("#userName").text(profile.DisplayName);  
      $("#userPic").attr("src", profile.PictureUrl);
    }
  </script>
  </head>
  
  <body>
    Assigned To: <input title="Assigned To"/>
    <button onclick="GetUserInfo()">Show User</button>
    
    <div id="userName"></div>
    <img id="userPic" />
  </body>
  </html>  

This fetches the user profile data on button click and displays name and picture dynamically.

Troubleshooting Issues with People Fields

Common problems when querying user data

Some common issues faced when using people fields and user profiles in SharePoint solutions:

  • Null or empty values instead of user properties – Permissions issue on the user profile store
  • Access denied errors – Application pool account lacks permissions
  • Invalid data type errors – Incorrect property mappings
  • Display names not updating – Refresh user profile store
  • Old user details showing – Clear cache during debugging

Checking permissions and access rights

Ensure the application pool account of the SharePoint site has read access rights to the User Profile Service Application under the Site Collection Administrators group. Individual permissions can also be assigned via the User Profile Permissions page.

Validating data types in responses

User profile properties can have different data types like string, boolean, datetime etc. Validate if the mapped column or variable is of the same data type to avoid serialization errors.

Additional Tips for Working with User Profiles

Caching user profile data

User profile data comes from an external store. Use caching mechanisms like AppFabric caching to reduce performance overhead of calls.

Updating profiles through REST API

In addition to retrieving user data, the SharePoint REST API can also update profile properties with the SetSingleValueProfileProperty endpoint.

User profile limitations in SharePoint

SharePoint user profiles have limits in terms of storage, throughput and custom property definitions. Evaluate limits based on version and workload sizes when planning solutions.

Leave a Reply

Your email address will not be published. Required fields are marked *