Question

I'm writing a small .NET proof-of-concept console app that performs a series of actions on a SharePoint document library. I noticed that the following methods expect an "encoded" login name - that is, login name including provider information, e.g. i:0#.w|DOMAIN\user.

context.Web.EnsureUser(encodedLoginName);
context.Web.SiteUsers.GetByLoginName(encodedLoginName);

How do I reliably convert a user name such as DOMAIN\user to this encoded format in the SharePoint Client Object Model?

I've read a couple of blog posts that address this issue with the SPClaimProviderManager, which is not available in the client API.

Was it helpful?

Solution

I am able to get the encoded login name using the ResolvePrincipal utility method:

using SP = Microsoft.SharePoint.Client;
//...

// resolve user principal using regular login name or e-mail:
var userPrincipal = SP.Utilities.Utility.ResolvePrincipal(
  context, 
  context.Web, 
  "DOMAIN\\user", // normal login name
  SP.Utilities.PrincipalType.User,
  SP.Utilities.PrincipalSource.All,
  context.Web.SiteUsers,
  false);

context.ExecuteQuery();

// ensure that the user principal was resolved:
if (userPrincipal.Value == null)
  throw new Exception("The specified user principal could not be resolved");

// get a User instance based on the encoded login name from userPrincipal
var user = context.Web.SiteUsers.GetByLoginName(userPrincipal.LoginName);
context.Load(user);

context.ExecuteQuery();

This seems to work as intended. However, if there is a better way or caveats that I should be aware of, please let me know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top