gpt4 book ai didi

c# - 如何通过 CSOM 从 SharePoint 获取用户?

转载 作者:太空狗 更新时间:2023-10-29 20:58:28 36 4
gpt4 key购买 nike

如何使用 CSOM 从 SharePoint 网站有效地抓取用户(及其属性)?下面的代码导致对服务器的多次调用(每个用户一次)。效率低得离谱。

另外,是否可以在服务器上执行过滤器?

    public static List<Contact> GetUsers(Uri requestUri, string Filter = "")
{
ClientContext context;
var users = new List<Contact>();
if (ClientContextUtilities.TryResolveClientContext(requestUri, out context, null))
{
using (context)
{
var web = context.Web;
var peopleManager = new PeopleManager(context);

context.Load(web, w => w.Title, w => w.Description, w => w.SiteUsers);
var siteUsers = web.SiteUsers;
context.ExecuteQuery();

foreach (var user in siteUsers)
if (user.PrincipalType == Microsoft.SharePoint.Client.Utilities.PrincipalType.User)
if (user.Title.ToLower().Contains(Filter.ToLower()) && !users.Any(x => x.FullName == user.Title))
{
var userProfile = peopleManager.GetPropertiesFor(user.LoginName);
context.Load(userProfile);
context.ExecuteQuery();

var contact = new Contact() { FullName = user.Title, EmailAddress = user.Email };
if (userProfile.IsPropertyAvailable("Title"))
contact.Position = userProfile.Title;
if (userProfile.IsPropertyAvailable("UserProfileProperties") && userProfile.UserProfileProperties.ContainsKey("WorkPhone"))
contact.PhoneNumber = userProfile.UserProfileProperties["WorkPhone"];
users.Add(contact);
}
}
}
return users;
}

最佳答案

主要改进

由于 SharePoint CSOM 支持 Request Batching可以使用 单个请求 检索所有用户配置文件,例如,而不是:

foreach (var user in siteUsers)
{
var userProfile = peopleManager.GetPropertiesFor(user.LoginName);
context.Load(userProfile);
context.ExecuteQuery();
}

用户个人资料可以检索为:

var userProfilesResult = new List<PersonProperties>(); //for storing user profiles
foreach (var user in siteUsers)
{
var userProfile = peopleManager.GetPropertiesFor(user.LoginName);
context.Load(userProfile);
userProfilesResult.Add(userProfile);
}
context.ExecuteQuery(); //submit a single request

小改进

1) 由于 SharePoint CSOM 支持 CAML queries ,过滤操作可以在服务端进行,例如:

var siteUsers = from user in web.SiteUsers
where user.PrincipalType == Microsoft.SharePoint.Client.Utilities.PrincipalType.User
select user;
var usersResult = context.LoadQuery(siteUsers);
context.ExecuteQuery();

2)您还可以使用以下检查来确定指定用户的用户配置文件是否存在:

var hasUserProfile = userProfile.ServerObjectIsNull != null && userProfile.ServerObjectIsNull.Value != true;

修改示例

public static List<Contact> GetUsers(Uri requestUri, ICredentials credentials, string filter = "")
{
ClientContext context;
var users = new List<Contact>();
if (ClientContextUtilities.TryResolveClientContext(requestUri, out context, credentials))
{
var userProfilesResult = new List<PersonProperties>();
using (context)
{
var web = context.Web;
var peopleManager = new PeopleManager(context);


var siteUsers = from user in web.SiteUsers
where user.PrincipalType == Microsoft.SharePoint.Client.Utilities.PrincipalType.User
select user;
var usersResult = context.LoadQuery(siteUsers);
context.ExecuteQuery();

foreach (var user in usersResult)
{
if (user.Title.ToLower().Contains(filter.ToLower()) && !users.Any(x => x.FullName == user.Title))
{
var userProfile = peopleManager.GetPropertiesFor(user.LoginName);
context.Load(userProfile);
userProfilesResult.Add(userProfile);
}
}
context.ExecuteQuery();


var result = from userProfile in userProfilesResult
where userProfile.ServerObjectIsNull != null && userProfile.ServerObjectIsNull.Value != true
select new Contact() {
FullName = userProfile.Title,
EmailAddress = userProfile.Email,
Position = userProfile.IsPropertyAvailable("Title") ? userProfile.Title : string.Empty,
PhoneNumber = userProfile.IsPropertyAvailable("UserProfileProperties") && userProfile.UserProfileProperties.ContainsKey("WorkPhone") ? userProfile.UserProfileProperties["WorkPhone"] : string.Empty
};
users = result.ToList();
}
}
return users;
}

关于c# - 如何通过 CSOM 从 SharePoint 获取用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29649066/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com