Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Encoded" login names in SharePoint Client Object Model

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.

like image 943
bernhof Avatar asked Jan 28 '14 09:01

bernhof


People also ask

What is ClientContext in SharePoint?

ClientContext with an example. SharePoint provides the JavaScript API reference files that contains the information used to access the data and build the custom applications. From the number of files, SP. JS is the most important file in the JSOM that provides the types and members the same as the Microsoft.

What is SharePoint client object model?

The client object model for SharePoint is a set of client-based libraries that represent the server object model. They are packaged in three different DLLs to accommodate a variety of development types. The client object model includes most of the major functions of the server API.

What is server side object model in SharePoint?

You use the SharePoint Server Object Model when you are writing code that will run inside the context of SharePoint. Some common examples would be the code-behind in a page or a web part, event handlers behind a feature or a list, timer jobs etc.


1 Answers

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.

like image 154
bernhof Avatar answered Sep 18 '22 23:09

bernhof