Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharepoint c# retrieve all site and subsite

Tags:

c#

sharepoint

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!

like image 447
sergio Avatar asked Apr 12 '11 15:04

sergio


People also ask

What is SharePoint used for?

Organizations use Microsoft SharePoint to create websites. You can use it as a secure place to store, organize, share, and access information from any device. All you need is a web browser, such as Microsoft Edge, Internet Explorer, Chrome, or Firefox.

What is my SharePoint com?

The URL https://domain-my.sharepoint.com is SharePoint Online My Site host. It's related to users' OneDrive for Business libraries. When user log in this URL, they will be redirected to their OneDrive for Business libraries.

Is SharePoint A Microsoft Office?

Microsoft SharePoint is a cloud-based service that helps organizations share and manage content, knowledge, and applications to: Empower teamwork.

What is SharePoint coding?

The SharePoint Framework (SPFx) is a page and web part model that provides full support for client-side SharePoint development, easy integration with SharePoint data, and support for open source tooling.


2 Answers

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);
            }
        }
    }
});
like image 68
Darien Ford Avatar answered Sep 21 '22 05:09

Darien Ford


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!

like image 42
joe_coolish Avatar answered Sep 24 '22 05:09

joe_coolish