0

I am new to sharepoint programming I have requirement to loop all the sites of site collection which should also loop including sub-sites.

Can any one help me how can I do this via Server Object Model

1

Use AllWebs property to returns all the Web sites within a site collection, including the top-level site and all subsites.

    using (SPSite oSiteCollection = SPContext.Current.Site)
        {
            SPWebCollection collWebsite = oSiteCollection.AllWebs;
            foreach(SPWeb owebsite in collWebsite)
            {
                string webTitle = owebsite.Title;
            }                
        }

Use Webs property to return a list of all the first-tier subsites beneath a specific Web site

    using (SPSite oSiteCollection = SPContext.Current.Site)
        {                
            SPWebCollection collWebsite = oSiteCollection.AllWebs;
            foreach (SPWeb owebsite in collWebsite)
            {
                string webTitle = owebsite.Title;
                foreach(SPWeb subweb in owebsite.Webs)
                {
                    string subwebTitle = subweb.Title;
                }

            }
        }
0

You could try below code:

string webUrl = "http://Server/sites/SiteCollection";

using (SPWeb oWebsite = new SPSite(webUrl).OpenWeb())
{
    SPWebCollection collWebsite = oWebsite.Webs;

    foreach (SPWeb subSite in collWebsite)
    {
        Label1.Text += SPEncode.HtmlEncode(subSite.Title) + "<BR>";
        subSite.Close();
    }
}

You could check below link for more details.

How to: Enumerate Sites and Site Collections

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.