sharepointsharepoint-2007sharepoint-object-model

How to get all sites and sub-sites in SharePoint and access an image library/list?


How to get all sites and sub-sites in SharePoint and access an image library/list?

I am looking forward to achieve this via the SharePoint object model. Inside each site or subsite I want to access an image library/list,

alt text

After getting to this list, how do I set the option of 'Required Content Approval for Selected Items' from 'Yes' to 'No'?


Solution

  • Use the SPFarm object to get all web applications then use SPWebApplication to get all sitecollection and then use SPSite to get all Webs.

    You have to loop through all three to get all sites under the site collection. If you want to find subsites under spweb please call all spwebs recursively until you don't find any webs under spweb for each spweb.

    SPFarm farm = SPFarm.Local;
    SPWebService service = farm.Services.GetValue<SPWebService>("");
    foreach (SPWebApplication webapp in service.WebApplications)
    {
        foreach (SPSite sitecoll in webapp.Sites)
        {
            foreach (SPWeb web in sitecoll.AllWebs)
            {
                <<Use recursion here to Get sub WebS>>
                web.Dispose();
            }
            sitecoll.Dispose();
        }
    }