c# - "Encoded"SharePoint 客户端对象模型中的登录名

标签 c# .net sharepoint-2013 sharepoint-api sharepoint-clientobject

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

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

如何可靠地将 DOMAIN\user 等用户名转换为 SharePoint 客户端对象模型 中的这种编码格式?

我读过 coupleblog posts解决了 SPClaimProviderManager 的这个问题,它在客户端 API 中不可用。

最佳答案

我可以使用 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();

这似乎按预期工作。但是,如果有更好的方法或我应该注意的注意事项,请告诉我。

关于c# - "Encoded"SharePoint 客户端对象模型中的登录名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21401391/

相关文章:

c# - 生成随机颜色的问题 - asp.net 和 c#

c# - 将模拟对象与字典一起使用

c# - .NET 中预编译程序集的优点和缺点是什么?

c# - .NET 中 yield 和 await 如何实现控制流?

javascript - 获取可细化字符串的内部 id

c# - 访问 View 中的viewdata对象

c# - 在 C# 中从自身内部调用函数

c# - 似乎无法通过引用传递 EventArgs

sharepoint - 母版页更改未反射(reflect)给 sharepoint online 2013 中的其他登录用户?

javascript - SharePoint 2013 : Execute JavaScript in a form