Michael's profileMike's RavingsBlogLists Tools Help

Blog


    August 13

    Listing Site Collections with any User - RunWithElevatedPermissions is your friend

    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);

    }

                   }

                 }

              });

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks (2)

    The trackback URL for this entry is:
    http://gourangaland.spaces.live.com/blog/cns!135E00A751103A8A!274.trak
    Weblogs that reference this entry