Sharepoint C # gets the whole site and child node

I am trying to get the whole site and child node in sharepoint, but I am accessing it.

I read about using the GetSubwebsForCurrentUser () property, but I get the same message.

My code is as follows

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

I need help! Thanks!

+7
source share
3 answers

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

You can make an inline delegate if you want, 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); } } } }); 
+9
source

Just to make sure this code runs on the server hosting the Sharepoint portal? What version of Sharepoint are you using?

Also, does it make sense to use the web services provided by Sharepoint? http://msdn.microsoft.com/en-us/library/aa979690(v=office.12).aspx

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

All in all, I think using web services is the easiest way to make it work. But make sure that you have the correct permissions and that this user has access (in SP configurations) to this data.

Hope this helps!

+1
source

If you use the SPSite.AllWebs Access Denied message, a message appears! This statement is only valid if the current user is the site collection administrator.

So, for all users, this function receives all sites and subsites.

  public static IEnumerable<SPWeb> DescendantSites(this SPWeb input) { foreach (SPWeb web in input.GetSubwebsForCurrentUser()) { yield return web; foreach (var subnode in web.DescendantSites()) yield return subnode; } } 
0
source

All Articles