How to: Get user identity and properties in SharePoint 2013

apps for SharePoint

Learn the different ways to retrieve user identity and user information in SharePoint 2013.

Last modified: July 01, 2013

Applies to: apps for SharePoint | Office 365 | SharePoint Foundation 2013 | SharePoint Server 2013

There are different ways to retrieve user identity and information, depending on what information you want to retrieve. This article shows you some of the ways you can accomplish that.

Note Note

For guidance on how to setup a development environment that fits your needs, see Start building apps for Office and SharePoint.

Core concepts to know for retrieving user identity and properties

Table 1 lists some useful articles that can help you to understand the concepts involved in creating apps for SharePoint.

Table 1. Core concepts for creating apps for SharePoint.

Article Title

Description

App permissions in SharePoint 2013

Learn about SharePoint app permissions for working with SharePoint 2013, including types of app permissions, permission request scopes, and managing permissions. This article also discusses the differences in app permission rights, and user rights.

OAuth authentication and authorization flow for cloud-hosted apps in SharePoint 2013

Learn about the OAuth authentication and authorization flow for cloud-hosted apps.

How to: Create a basic provider-hosted app for SharePoint

Learn how to create a basic provider-hosted app for SharePoint with the Office Developer Tools for Visual Studio 2012, how to interact with Microsoft SharePoint 2013 sites by using the SharePoint CSOM, and how to implement OAuth in a app for SharePoint.

The easiest way to retrieve the identity of the current user of the website is via the Web object. With the TokenHelper.cs file in your project, you can get the current website user identity using the following code snippet.

ClientContext clientContext =
                    TokenHelper.GetClientContextWithAccessToken(
                        sharepointUrl.ToString(), accessToken);
 
 
            //Load the properties for the Web object.
            Web web = clientContext.Web;
            clientContext.Load(web);
            clientContext.ExecuteQuery();
 
            //Get the site name.
            siteName = web.Title;
 
            //Get the current user.
            clientContext.Load(web.CurrentUser);
            clientContext.ExecuteQuery();
            currentUser = clientContext.Web.CurrentUser.LoginName;

  • If you are using Office 365, the login name you get is similar to i:0#.f|membership|adam@contoso.com.

  • If you are using Microsoft SharePoint 2013 on-premises and the user is logged in as a normal user using NTLM, the login name you get is similar to i:0#.w|contoso\adam.

  • If you are using SharePoint 2013 on-premises and the user is logged in using a farm account, the login name you get is SHAREPOINT\System.

The following is another way to retrieve user's login name. If you have the user's email address or display name, you can use the ResolvePrincipal method to get the user's login name.

Note Note

The APIs are in the Microsoft.SharePoint.Client.Utilities namespace in the Microsoft.SharePoint.Client.dll assembly.

The following is sample code to show how to get the user login information.

ClientResult<Microsoft.SharePoint.Client.Utilities.PrincipalInfo> persons = Microsoft.SharePoint.Client.Utilities.Utility.ResolvePrincipal(clientContext, clientContext.Web, <email>, Microsoft.SharePoint.Client.Utilities.PrincipalType.User, Microsoft.SharePoint.Client.Utilities.PrincipalSource.All, null, true);
                    clientContext.ExecuteQuery();
                    Microsoft.SharePoint.Client.Utilities.PrincipalInfo person = persons.Value;

The Person.LoginName value gives the login information.

If you want to retrieve the user's identity and properties, you can use the OAuth token and the social features APIs. First get the OAuth token and then set it to the client context. The following sample code shows how to get the user profile properties.


ClientContext clientContext = new ClientContext(<sharepointurl>);
clientContext.AuthenticationMode = ClientAuthenticationMode.Anonymous;
clientContext.FormDigestHandlingEnabled = false;
clientContext.ExecutingWebRequest +=
delegate(object oSender, WebRequestEventArgs webRequestEventArgs)
{                      
    webRequestEventArgs.WebRequestExecutor.RequestHeaders["Authorization"] =
        "Bearer " + accessToken;
};

Then, use the PeopleManager API to get the properties of the user who is using the app.


PeopleManager peopleManager = new PeopleManager(clientContext);
PersonProperties personDetails = peopleManager.GetMyProperties();
clientContext.Load(personDetails, personsD => personsD.AccountName, personsD => personsD.Email,  personsD => personsD.DisplayName);
                clientContext.ExecuteQuery();

For the code to work:

  • The user profile shared service should be configured and synced on SharePoint 2013 for the users.

  • You must add the following permission scope for social features in the app manifest:

    <AppPermissionRequest Right="Read" Scope="http://sharepoint/social/tenant" />
    
    

The APIs are in Microsoft.SharePoint.Client.UserProfiles.dll.

The following is another sample code snippet that shows how to access the user profile store.


ClientContext clientContext; //Create this like you normally would.               
PeopleManager peopleManager = new PeopleManager(clientContext);
PersonProperties myProperties = peopleManager.GetMyProperties();
clientContext.Load(myProperties);
clientContext.ExecuteQuery();

Community Additions

ADD
Show:
© 2014 Microsoft