首页 > 其他 > 详细

SharePoint 2013 抽取SPFieldUserValueCollection中包含的AD用户(含嵌套的AD组,AD组多层嵌套,SP组嵌套AD组)

时间:2014-03-07 21:54:42      阅读:604      评论:0      收藏:0      [点我收藏+]

本文讲述SharePoint 2013 抽取SPFieldUserValueCollection中包含的AD用户(含嵌套的AD组,AD组多层嵌套,SP组嵌套AD组)。

最近有个功能要抽取SPFieldUserValueCollection中包含的AD用户,搜索了下,发现网上很多人问这个问题。

研究出来了,不敢独享,特此共享出来

   /// <summary>
        /// Extract AD users from SPFieldUserValueCollection
        /// </summary>
        /// <param name="web">The SPWeb for resolve the SPUser</param>
        /// <param name="spFieldUserValueCollection">SPFieldUserValueCollection which need to be extracted</param>
        /// <returns>SPUser(AD users) list which extract from SPFieldUserValueCollection</returns> 
        public static List<SPPrincipal> ExtractADusers(SPWeb web, SPFieldUserValueCollection spFieldUserValueCollection)
        {
            if (web == null)
            {
                ArgumentNullException ex = new ArgumentNullException("web", "Parameter web can‘t be null for ExtractADusers");
                WriteException(ex, "ExtractADusers");
                throw ex;
            }

            if (spFieldUserValueCollection == null)
            {
                ArgumentNullException ex = new ArgumentNullException("spFieldUserValueCollection", "Parameter spFieldUserValueCollection can‘t be null for ExtractADusers");
                WriteException(ex, "ExtractADusers");
                throw ex;
            }

            List<SPPrincipal> adUsers = new List<SPPrincipal>();
            try
            {
                foreach (SPFieldUserValue spFieldUserValue in spFieldUserValueCollection)
                {
                    if (spFieldUserValue.User == null) // SharePoint group
                    {
                        ResolveGroup(web, spFieldUserValue.LookupValue, adUsers);
                    }
                    else if (spFieldUserValue.User.IsDomainGroup) // AD group
                    {
                        ResolveGroup(web, spFieldUserValue.User.LoginName, adUsers);
                    }
                    else // AD users
                    {
                        adUsers.Add(spFieldUserValue.User);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteException(ex, "ExtractADusers");
                throw ex;
            }

            return adUsers;
        }

        /// <summary>
        /// Extract the Groups(AD, SharePoint group)
        /// </summary>
        /// <param name="web">SPWeb for resolving users</param>
        /// <param name="name">Group name</param>
        /// <param name="users">SPUser(AD users) list for storing the result </param>
        private static void ResolveGroup(SPWeb web, string name, List<SPPrincipal> users)
        {
            bool reachedMaxCount;
            foreach (SPPrincipalInfo spPrincipalInfo in SPUtility.GetPrincipalsInGroup(web, name, 1000, out reachedMaxCount))
            {
                if (spPrincipalInfo.PrincipalType == SPPrincipalType.SecurityGroup)
                {
                    ResolveGroup(web, spPrincipalInfo.LoginName, users);                    
                }
                else
                { 
                    users.Add(web.EnsureUser(spPrincipalInfo.LoginName));
                }
            }
        }


 

SharePoint 2013 抽取SPFieldUserValueCollection中包含的AD用户(含嵌套的AD组,AD组多层嵌套,SP组嵌套AD组),布布扣,bubuko.com

SharePoint 2013 抽取SPFieldUserValueCollection中包含的AD用户(含嵌套的AD组,AD组多层嵌套,SP组嵌套AD组)

原文:http://blog.csdn.net/abrahamcheng/article/details/20724507

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!