代码创建SharePoint安全组

 

public static System.DirectoryServices.DirectoryEntry GetRootDEDefault(string adPath)
        {
            while (adPath.EndsWith("/"))
            {
                adPath = adPath.TrimEnd(new char[]
                {
                    '/'
                });
            }
            return new DirectoryEntry(adPath);
        }
        public static bool IsGroupExist(string GroupName, DirectoryEntry rootDE)
        {
            bool result = false;
            try
            {
                SearchResult searchResult = new DirectorySearcher(rootDE)
                {
                    Filter = "(&(objectClass=Group)(cn=" + GroupName + "))",//Ou ,cn,Dc
                    SearchScope = SearchScope.Subtree
                }.FindOne();
                if (searchResult != null)
                {
                    result = true;
                }
            }
            catch
            {
            }
            return result;
        }
        public static DirectoryEntry CreateNewGroupDefault(string adPath, string sOULocation, string sGroupName, string sDescription, GroupType oGroupTypeInput, bool bSecurityGroup)
        {
            DirectoryEntry directoryEntry = new DirectoryEntry(adPath);
            GroupType groupType;
            if (bSecurityGroup)
            {
                groupType = (oGroupTypeInput | (GroupType)2147483648u);
            }
            else
            {
                groupType = oGroupTypeInput;
            }
            int num = (int)groupType;
            lock ("")
            {
                try
                {
                    DirectoryEntry directoryEntry2 = directoryEntry.Children.Add("cn=" + sGroupName + "," + sOULocation, "group");
                    directoryEntry2.Properties["sAMAccountName"].Add(sGroupName);
                    directoryEntry2.Properties["description"].Add(sDescription);
                    directoryEntry2.Properties["groupType"].Add(num);
                    directoryEntry2.CommitChanges();
                }
                catch(Exception ex)
                {
                }
            }
            SearchResult searchResult = new DirectorySearcher(directoryEntry)
            {
                Filter = "(&(objectClass=Group)(cn=" + sGroupName + "))",
                SearchScope = SearchScope.Subtree
            }.FindOne();
            if (searchResult == null)
            {
                return null;
            }
            return searchResult.GetDirectoryEntry();
        }
 protected void Page_Load(object sender, EventArgs e)
        {
            string groupName = "小飞2";
            string adPath = "LDAP://dweb1.dep1.com/";
            string path = GetRootDEDefault(adPath).Path;
            string sOuLocation = "OU=it1,OU=it";//"CN=Users";("OU=it1,OU=it"//说明,it1是it的子目录)
            SPSecurity.RunWithElevatedPrivileges(delegate() {
                if (!IsGroupExist(groupName,GetRootDEDefault(path)))
                    CreateNewGroupDefault(GetRootDEDefault(path).Path, sOuLocation, groupName, groupName, GroupType.GlobalGroup, true);
            });
           }

 

posted on 2015-11-16 14:39  !无名之辈  阅读(181)  评论(0)    收藏  举报