AndreaNobili
AndreaNobili

Reputation: 42957

How to retrieve the list of all user on a SharePoint 2013 website (SPWeb)?

I am working on a SharePoint 2013 application and I have the following problem. Into my code I have something like this:

List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);

foreach (string currentUrl in urlList)
{
    Debug.Print("Current url: " + currentUrl);

    SPSecurity.RunWithElevatedPrivileges(delegate ()
    {
        using (SPSite oSiteCollection = new SPSite(currentUrl))
        {
            using (SPWeb oWebsite = oSiteCollection.OpenWeb())
            {
                // RETRIEVE THE LIST OF USERS OF THE CURRENT SITE
            }
        }
    });
}

As you can see I am retriueving the list of the sites in a SharePoint application (it works fine).

For the current site (represented by the SPWeb oWebSite variable) I have to retrieve the list of all the user registered to this website.

How can I do it? I was trying to search some method\propery on the oWebsite object but I can't findi a solution

Upvotes: 0

Views: 599

Answers (1)

Lukas Nespor
Lukas Nespor

Reputation: 1403

It is very simple. Yo don't even have to open SPWeb.

List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);

foreach (string currentUrl in urlList)
{
    Debug.Print("Current url: " + currentUrl);

    SPSecurity.RunWithElevatedPrivileges(delegate ()
    {
        using (SPSite oSiteCollection = new SPSite(currentUrl))
        {
            # Gets the collection of all users that belong to the site collection.
            SPUserCollection users = oSiteCollection.RootWeb.SiteUsers;
        }
    });
}

Upvotes: 3

Related Questions