3

I have a column of type People in a SharePoint list. I want to get the user properties for the users present in that column.

For this I am using below code:

PersonProperties personProperties = peopleManager.GetPropertiesFor("ACCOUNT_NAME");

Now the problem is that the user column gives the email id of the user, which if I pass to above code then it returns null: enter image description here

So how can I get Account Name from email or is there any other alternative way to get the PersonProperties.

NOTE: User column shows Name(with presence) and is not allowed to change

Update

PeopleManager peopleManager = new PeopleManager(context);
            PersonProperties personProperties = peopleManager.GetPropertiesFor("email_ID");
            context.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
            context.ExecuteQuery();
  • Can you please share your code? – Akshay Belure Sep 4 '16 at 17:46
  • added code snippet. – nbi Sep 4 '16 at 17:48
  • context.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties,p.DisplayName); try this and check – Akshay Belure Sep 4 '16 at 17:49
  • :( didn't work, actually problem is related to data passed to GetPropertiesFor method, it should be login name or account name – nbi Sep 4 '16 at 17:54
4

You can use the Microsoft.SharePoint.Client.Utilities.Utility.ResolvePrincipal method. For example:

        Web web = context.Web;
        string email = "emailaddress@yourdomain.com";
        PeopleManager peopleManager = new PeopleManager(context);
        ClientResult<PrincipalInfo> principal = Utility.ResolvePrincipal(context, web, email, PrincipalType.User, PrincipalSource.All, web.SiteUsers, true);
        context.ExecuteQuery();

Then to get the login name from the resulting principal (if found), and get the user properties

        if (principal.Value != null)
        {
            System.Console.WriteLine("Found user. LoginName is: '" + principal.Value.LoginName + "'");
            PersonProperties personProperties = peopleManager.GetPropertiesFor(principal.Value.LoginName);
            context.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
            context.ExecuteQuery();
        }
        else
        {
            System.Console.WriteLine("User with email '" + email + "' not found.");
        }

See MSDN for the ResolvePrincipal method documentation: Utility.ResolvePrincipal method. Full code example:

        Web web = context.Web;
        string email = "emailaddress@yourdomain.com";
        PeopleManager peopleManager = new PeopleManager(context);
        ClientResult<PrincipalInfo> principal = Utility.ResolvePrincipal(context, web, email, PrincipalType.User, PrincipalSource.All, web.SiteUsers, true);
        context.ExecuteQuery();
        if (principal.Value != null)
        {
            System.Console.WriteLine("Found user. LoginName is: '" + principal.Value.LoginName + "'");
            PersonProperties personProperties = peopleManager.GetPropertiesFor(principal.Value.LoginName);
            context.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
            context.ExecuteQuery();
        }
        else
        {
            System.Console.WriteLine("User with email '" + email + "' not found.");
        }
  • Thanks Kevins. It seems like there is no direct way to .... have to call executeQuery at least two times. – nbi Sep 7 '16 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.