Name names "encoded" in the SharePoint client object model

I am writing a small .NET proof-of-concept console application that performs a series of actions in a SharePoint document library. I noticed that the following methods expect a β€œcoded” login name, that is, a login name, including provider information, for example. i:0#.w|DOMAIN\user .

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

How can I reliably convert a username, such as DOMAIN\user , into this encoded format in the SharePoint client object model ?

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

+6
source share
2 answers

I can get the encoded login using the ResolvePrincipal utility:

 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(); 

It seems to work. However, if there is a better way or reservations I should be aware of, let me know.

+7
source

How to encode / decode an application through SharePoint Online CSOM

The tenant class (assembly Microsoft.Online.SharePoint.Client.Tenant.dll) contains the following methods:

Tenant.EncodeClaim Method - Returns the encoded request for the specified login

Tenant.DecodeClaim method - returns a string that represents a decoded request (username) for the specified encoded statement

 public class SPOUser { private SPOUser() { } /// <summary> /// Returns an encoded claim for the specified login name. /// </summary> /// <param name="context">Client Context for Tenant Admin site</param> /// <param name="loginName"></param> /// <returns></returns> public static string EncodeClaim(ClientRuntimeContext context, string loginName) { var tenant = new Tenant(context); var clientResult = tenant.EncodeClaim(loginName); context.ExecuteQuery(); return clientResult.Value; } /// <summary> /// Returns a string that represents the decoded claim (the login name) for the specified encoded claim. /// </summary> /// <param name="context">Client Context for Tenant Admin site</param> /// <param name="encodedLoginName">A string that represents the encoded claim.</param> /// <returns></returns> public static string DecodeClaim(ClientRuntimeContext context, string encodedLoginName) { var tenant = new Tenant(context); var clientResult = tenant.DecodeClaim(encodedLoginName); context.ExecuteQuery(); return clientResult.Value; } } 
-1
source

All Articles