Listing Site Collections with any User – RunWithElevatedPermissions is your friend

Posted: August 13, 2008 in MOSS 2007

Listing Site Collections with any User – RunWithElevatedPermissions is your friend

 

So I had a web part I wrote a long time ago that would list sites and subsites on a given SPWeb as deep as you wanted to go and allow a good deal of flexibility on how they are displayed. Recently, I had the opportunity to modify it to scan a list of site collections in a given Web application.

We had a setup with 80+ site collections for a clients extranet. Each site collection is a separate project for their clients. Some clients had multiple site collections and each of these had the need to explicit security encapsulation. So some users did in fact have access to multiple site collections, most would have 1 or maybe 2.

The issue with this, the standard scanning code I had could not jump site collections. The only code samples I managed to find, involved using the Microsoft.SharePoint.Administration namespace. The issue there being, it would work fine for a MOSS administrator, however when another user hit the page with the web part, they would be given a generic MOSS Access Denied page.

The answer I got came from merging a few posts I found while frantically googling some way to do this. It involves first using the RunWithElevatedPermissions directive to run under an account that has privileges to access all the site collections. The second part involves checking the DoesUserHavePermissions function off the root web on the site collections.

It is not a huge breakthrough but if it helps save someone some time it is worth it. See code below:

 

  SPSecurity.RunWithElevatedPrivileges(delegate()

  {

      SPWebApplication webApplication = SPWebApplication.Lookup(serverUri);

      SPWeb rootweb;

 

      foreach (SPSite siteCollection in webApplication.Sites)

      {

         rootweb = siteCollection.OpenWeb();

         //do not add it is the current user does not have access to the site

         if (rootweb.DoesUserHavePermissions(objOriginalUser.Name,    

            SPBasePermissions.ViewPages) == true)

            {

             if (rootweb.Url.Contains(startingPointURL + "personal/") == false)

             {

                 userWebInfo = new clsUserWebInfo();

                 userWebInfo.ID = rootweb.ID;

                 if (rootweb.Name.Trim() == "")

                 {

                     userWebInfo.Name = rootweb.Title;

                  }

                  else

                  {

                     userWebInfo.Name = rootweb.Name;

                  }

                                                        userWebInfo.Description = rootweb.Description;

userWebInfo.Title = rootweb.Title;

userWebInfo.URL = rootweb.Url;

alUserWebs.Add(userWebInfo);

}

               }

             }

          });

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s