17 February 2012

No need to dispose in 2010 at SPWebInfo object

Who has not encountered during the development toolpart - create a dropdownlist with a list of all sites.

The 2007 version used with SPWeb object and it must to make a Dispose.
 
// 2007
using (SPSite site = new SPSite("http://mysite"))
{
    foreach (SPWeb web in site.AllWebs)
    {
        ddlWebs.Items.Add(new ListItem(web.Title,web.ID.ToString()));
        // ... Continuing operations ...
        web.Dispose();
    }
}

The 2010 version we use object SPWebInfo.
In SPWebInfo we do not we will dispose.

Here is an example (in this case a loop that sets the title and ID for dropdownlist )

// 2010
using (SPSite site = new SPSite("http://mysite"))
{
     foreach (SPWebInfo webInfo in site.AllWebs.WebsInfo)
     {
          ddlWebs.Items.Add(new ListItem(webInfo.Title, webInfo.Id.ToString()));
          // ... Continuing operations ...
     }
}

SPWebInfo object contains:
Properties: Configuration, CustomMasterUrl, Description, Id, Language, LastItemModifiedDate, MasterUrl, ServerRelativeUrl, Title, UIVersion, UIVersionConfigurationEnabled, WebTemplateId
Methods : Equals, Finalize, GetHashCode, GetType, MemberwiseClone, ToString


Yours,
Roi

No comments:

Post a Comment