8

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.

bernhof
  • 6,219
  • 2
  • 45
  • 71

1 Answers1

8

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.

bernhof
  • 6,219
  • 2
  • 45
  • 71
  • This worked for me only when I used email address instead of login name (domain\alias) in 3rd argument and set the last argument to true instead of false in the method ResolvePrincipal(). – bittusarkar Apr 16 '14 at 21:53
  • My `ClientContext` doesn't have `context.Web.SiteUsers`?. Will this method allow you to decode the claim token to DOMAIN\USER? – PeterX May 14 '15 at 02:27