Friday, March 25, 2011

List all users and groups from SharePoint site collection

Environment: MOSS 2007                  Click here for SharePoint Online Version

The following code is to generate a list on all site collection users, their groups and subsite details

- I've created a custom list named "All Site Users" with  5 columns as User Name, Groups, Site URL, Account Name, TagUsers

- The list template is uploaded programmatically on feature activation, The below code is a webpart code, which is added to a webpart page on solution deployment, when the webpart page is accessed, the following code will loop through the users in the site collection their groups and log the details to the custom list "All Site Users".

-'TagUsers' column can be used to create a view to list unique user names, condition for the view would be Filter by TagUsers=1


SPWeb rootweb = null;
                SPSite SPSite = null;
                try
                {
                   using (SPSite = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (rootweb = SPSite.OpenWeb())
                        {
                            //Delete and create a new Portal Users List with updates
                            DeleteListIfExists(rootweb, "Portal Users List");
                            SPListTemplateCollection templateCollection = SPSite.GetCustomListTemplates(rootweb);
                            SPListTemplate template = templateCollection["Portal Users List"];
                            rootweb.AllowUnsafeUpdates = true;
                            //Create the Portal Users List using the PortalUsersList.stp template,                            rootweb.Lists.Add("Portal Users List", "List to display all the site collection users and their groups", template);
                            //Getting the URL of the list
                            SPList alluserslist = rootweb.Lists["Portal Users List"];
                            string ListURL = alluserslist.DefaultViewUrl;
                            Hashtable htUsers = new Hashtable();
                            //Loop all the Sites
                            foreach (SPWeb SPWeb in SPSite.AllWebs)
                            {
                                //Get All users in the subsite
                                SPUserCollection AllSPWebUsers = SPWeb.AllUsers;
                                //Loop each users in the Web/subsite
                                foreach (SPUser user in AllSPWebUsers)
                                {
                                    //Adding the user to the "Portal Users List" list
                                    SPListItem newItem = alluserslist.Items.Add();
                                    SPGroupCollection AllGroups = user.Groups;
                                    //AllGroups.Count is '0' when the users are directly added to the site
                                    if (AllGroups.Count == 0)
                                    {
                                        newItem["User Name"] = user.Name;
                                        newItem["Account Name"] = user.LoginName;
                                        newItem["Parent Site"] = SPWeb.Url;
                                        newItem["Group"] = "Individual User";
                                       
                                        if (!htUsers.Contains(user.LoginName))
                                        {
                                            newItem["TagUsers"] = 1;
                                            htUsers.Add(user.LoginName,user.Name);
                                        }
                                       
                                    }
                                    else
                                    {
                                        //Loop each group the user added to
                                        foreach (SPGroup group in AllGroups)
                                        {
                                            newItem["User Name"] = user.Name;
                                            newItem["Account Name"] = user.LoginName;
                                            newItem["Parent Site"] = SPWeb.Url;
                                            newItem["Group"] = group.Name;
                                            if (!htUsers.Contains(user.LoginName))
                                            {
                                                newItem["TagUsers"] = 1;
                                                htUsers.Add(user.LoginName, user.Name);
                                            }
                                        }
                                    }
                                    //Saving new item in the list
                                    newItem.Update();
                                }
                                alluserslist.Update();
                            }
                            //After WebPart process the entire users list, will be redirected to the "Portal Users List" page
                            SPUtility.Redirect(ListURL, SPRedirectFlags.Trusted, HttpContext.Current);
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                    //Ignoring this as SPUtility.Redirect throws ThreadAbortException
                }
                   catch (Exception e)
                {
                 }
             }

3 comments:

  1. this is C sharp but we need power shell

    ReplyDelete
  2. Can this be done through a windows application or a web one that is not part of the sharepoint site?

    e.g. enter url and name then search?

    ReplyDelete
    Replies
    1. Yes, you can, using SharePoint Client Object model.

      Delete