SharePoint客户端对象模型中的"编码"登录名

ber*_*hof 7 .net c# sharepoint-api sharepoint-clientobject sharepoint-2013

我正在编写一个小型.NET概念验证控制台应用程序,它在SharePoint文档库上执行一系列操作.我注意到以下方法需要一个"编码"登录名 - 即登录名,包括提供者信息,例如i:0#.w|DOMAIN\user.

context.Web.EnsureUser(encodedLoginName);
context.Web.SiteUsers.GetByLoginName(encodedLoginName);
Run Code Online (Sandbox Code Playgroud)

如何DOMAIN\userSharePoint客户端对象模型中可靠地将用户名转换为此编码格式?

我读过一对夫妇博客文章,与解决这一问题SPClaimProviderManager,这是不提供客户端的API中.

ber*_*hof 8

我可以使用ResolvePrincipal实用程序方法获取编码的登录名:

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();
Run Code Online (Sandbox Code Playgroud)

这似乎按预期工作.但是,如果有更好的方法或警告我应该知道,请告诉我.