Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've wrote here a code for SharePoint 2007 to modify few items through out of all site collections in web app, my question is the code written good, or is there a leak somewhere, and i could improve my code?

SPSiteCollection siteCollection = webApp.Sites;
foreach (SPSite site in siteCollection)
{
    SPWebCollection webCollection = site.AllWebs;
    foreach (SPWeb web in webCollection)
    {
        SPFeature feature = web.Features[new Guid(strGuid)];

        if (feature == null)
        {
            continue;
        }

        SPListCollection listCollection = web.Lists;
        foreach (SPList list in listCollection)
        {
            if (!(list is SPDocumentLibrary))
            {
                continue;
            }
            SPListItem item = list.GetItemById("someid");
            item[strFieldName]="new value";
            item.SystemUpdate(false);
        }     
    }
}

So can anyone comment if my code is leak safe?

Thanks

share|improve this question

1 Answer 1

up vote 5 down vote accepted

No your code isn't leak safe. It'll leak all of the SPSites and SPWebs

You need to add a dispose of each SPWeb and SPSite like this:

SPSiteCollection siteCollection = webApp.Sites;
foreach (SPSite site in siteCollection)
{
    SPWebCollection webCollection = site.AllWebs;
    foreach (SPWeb web in webCollection)
    {
        SPFeature feature = web.Features[new Guid(strGuid)];

        if (feature == null)
        {
            web.Dispose(); // Added
            continue;
        }

        SPListCollection listCollection = web.Lists;
        foreach (SPList list in listCollection)
        {
            if (!(list is SPDocumentLibrary))
            {
                continue;
            }
            SPListItem item = list.GetItemById("someid");
            item[strFieldName]="new value";
            item.SystemUpdate(false);
        }     
        web.Dispose(); // Added
    }
    site.Dispose(); // Added
}

or by using Using like this:

SPSiteCollection siteCollection = webApp.Sites;
foreach (SPSite site in siteCollection)
using (site) // Added
{
    SPWebCollection webCollection = site.AllWebs;
    foreach (SPWeb web in webCollection)
    using (web) // Added
    {
        SPFeature feature = web.Features[new Guid(strGuid)];

        if (feature == null)
        {
            continue;
        }

        SPListCollection listCollection = web.Lists;
        foreach (SPList list in listCollection)
        {
            if (!(list is SPDocumentLibrary))
            {
                continue;
            }
            SPListItem item = list.GetItemById("someid");
            item[strFieldName]="new value";
            item.SystemUpdate(false);
        }     
    }
}

Unfortunately there is no easy way of getting all the SPWebs with a feature enabled in SP2007. When you upgrade to SP2010/SP2013 you'll get the SPWebApplication.QueryFeatures method to help you.

But depending on what you're doing my guess would be that the handling of lists could be improved. Don't you know the URL of the list? Currently you're looping through them all and what happens if the users has added their own Document Libraries?

share|improve this answer
    
can i somehow implement it with using instead of site.dipose() or try ... catch –  Shkipper Jun 25 '13 at 6:52
    
See updated answer for how to use using –  Per Jakobsen Jun 25 '13 at 7:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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