【问题标题】:How to get users from SharePoint via CSOM?如何通过 CSOM 从 SharePoint 获取用户?
【发布时间】:2015-04-15 11:30:15
【问题描述】:

如何使用 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;
    }

【问题讨论】:

    标签: c# sharepoint csom


    【解决方案1】:

    重大改进

    由于 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;
     }
    

    【讨论】:

    • 太棒了!谢谢你。我是 CSOM 的新手,但我想我现在开始了解它了。
    【解决方案2】:

    当您处于网站的特定上下文中时,您可以执行以下操作:

    using (var ctx = new ClientContext("http://theWebsite"))
    {
        var list = ctx.Web.SiteUserInfoList;
        var users = list.GetItems(new CamlQuery());
        ctx.Load(users);
        ctx.ExecuteQuery();
    
        // do what you want with the users
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-04
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多