Sharepoint c# retrieve all site and subsite

22,064

Solution 1

You will probably need to call SPSecurity.RunWithElevatedPrivileges(delegate())

You could do an inline delegate if you wish, something like:

SPSecurity.RunWithElevatedPrivileges(delegate() 
{
    foreach (SPWebApplication webApp in SPWebService.ContentService.WebApplications)
    {                    
        foreach (SPSite siteCollection in webApp.Sites)
        {
            foreach(SPWeb web in siteCollection.RootWeb.GetSubwebsForCurrentUser())
            {
                 dropDownSite.Items.Add(web.Url);
            }
        }
    }
});

Solution 2

Just to make sure, is this code running on the server that is hosting the Sharepoint portal? What version of Sharepoint are you running?

Also, would it make sense to use the Webservices that Sharepoint exposes? http://msdn.microsoft.com/en-us/library/aa979690(v=office.12).aspx

If you are running this code on the same server as SP, then make sure that your credentials have access to SP. If you are calling this from a website, also make sure that you are not running as "Anonymous".

Over all, I think taking advantage of the webservices is the easiest way to get it working. But make sure that you have the right permissions being sent and that that user has access (in the SP configurations) to that data.

Hope that helps!

Share:
22,064

Related videos on Youtube

sergio
Author by

sergio

Updated on July 27, 2020

Comments

  • sergio
    sergio over 3 years

    I'am trying to retrieve all site and subsite in sharepoint but i get access denied.

    I read about to use GetSubwebsForCurrentUser() property, but i get the same message.

    My code is the next

                foreach (SPWebApplication webApp in SPWebService.ContentService.WebApplications)
                {                    
                    foreach (SPSite siteCollection in webApp.Sites)
                    {
                        foreach(SPWeb web in siteCollection.RootWeb.GetSubwebsForCurrentUser())
                        {
                           dropDownSite.Items.Add(web.Url);
                        }
    
    
                    }
                }
    

    please i need help! Thanks!

    • pstrjds
      pstrjds over 12 years
      Just a heads up, there is now a SharePoint stack exchange site. Its in beta but you should post over there (or moderator could move this question there). It will help the site to get more traction :)

Related