0
votes

I have an Image List on each and every web (SPWeb) of a SiteCollection. I want to set a specific property of this List. I am iterating through all the Sites withing a SiteCollection and finding the List and setting its properties. My problem is that I can set the properties of a List present at first level Sites, but can't set the properties of Lists, present at 2nd or 3rd level Sites. For example,

Here is site hierarchy:

Home (Rootweb) 1st level

Home-> Aboutus (subsite) 2nd level

Home->Aboutus->Our Mission (subsite) 3rd level

here is the code for that!

using (SPSite oSPsite = new SPSite(http://spdev/))
{                
    foreach (SPWeb web in oSPsite.AllWebs)
    {
        SPList list = web.GetList("PublishingImages");
        if (list != null)
        {
            foreach (SPContentType contentType in list.ContentTypes)
            {
                if (contentType.Name == "Publishing Picture")// but id is better
                {
                    list.EnableModeration = false;
                    list.Update();
                }
            }
        }
        web.Dispose();
    } 
}                 

Is it because I'm disposing the parent first?

1
Try the Lists collection instead of GetList(). GetList() takes a URL not just the list name so if it works with the Lists collection then the URL is your problem. In fact, web.Lists.TryGetLists("listName") is even better if you're in the 2010 family. - knight0323
list.EnableModeration is not supported by image libraries and this is a dublicate question - Ashutosh Singh-MVP SharePoint

1 Answers

1
votes

Assuming the list name is the same on every site (PublishingImages) and you're on WSS 3.0 or MOSS07 here is the sample code:

using (SPSite oSPsite = new SPSite("yourSiteUrlHere"))
{
    SPWebCollection siteWebs = oSPsite.AllWebs;
    foreach (SPWeb web in siteWebs)
    {
        try
        {               
            SPList list = null;
            try
            {
                list = web.Lists["PublishingImages"];
            }
            catch {}

            if (list != null)
            {
                // todo: update list properties here
                list.Update();
            }
        }
        finally
        {
            if(web != null)
                web.Dispose();
        }
    }  
}

As Ashutosh mentioned, there are some properties that don't work on all list types but I'm assuming since you've already stated it works on some of them you aren't setting any of those.