简体   繁体   中英

Get all groups of a User in Sharepoint programmatically in C#

What is the best way to access all groups of a SPUser including the Active Directory groups?

SPUser u = someWeb.EnsureUSer(...);
//Only gives SharePoint groups
SPGroupCollection coll = user.Groups;
//Also gives only SharePoint groups
SPGroupCollection groupsonSite = someSPSite.rootWeb.siteGroups

Is there a way to access all groups a user is in, including AD groups?

If you need all users from current site collection , you can access it from hidden list UserInformationList

The User Information List can be accessed (Only if you’re admin) via the browser by navigating to /_catalogs/users/simple.aspx

Read more here : https://zimmergren.net/sharepoints-hidden-user-list-user-information-list/

From AD :

        DirectoryEntry myLdapConnection = new DirectoryEntry("LDAP://YouDomainName");
        DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ("(objectClass=user)") };
        search.CacheResults = true;
        SearchResultCollection allResults = search.FindAll();
        DataTable resultsTable = new DataTable();
        resultsTable.Columns.Add("UserID");
        resultsTable.Columns.Add("EmailID");
        foreach (SearchResult searchResult in allResults)
        {
            MembershipUser myUser = Membership.GetAllUsers()[searchResult.Properties["sAMAccountName"][0].ToString()];
            if (myUser == null)
            {
                DataRow dr = resultsTable.NewRow();
                dr["UserID"] = searchResult.Properties["sAMAccountName"][0].ToString();
                if (searchResult.Properties["mail"].Count > 0)
                {
                    dr["EmailID"] = searchResult.Properties["mail"][0].ToString();
                }
                else
                {
                    dr["EmailID"] = "";
                }
                resultsTable.Rows.Add(dr);
            }
            else
            { }
        }

http://www.dotnetcodesg.com/Article/UploadFile/2/223/Get%20List%20of%20Active%20Directory%20Users%20in%20ASP.NET%20Csharp.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM