SharePoint 2010 Recommended practices for disposing objects–part3

Home | SharePoint Development | SharePoint 2010 Recommended practices for disposing objects–part3
This post is the continuation of my previous post SharePoint 2010 Recommended practices for disposing objects – part2.

1. Automatic Disposal of SPWeb objects created when accessing SPWeb.ParentWeb property

It is not recommended to explicitly dispose SPWeb objects created by accessing SPWeb.ParentWeb property. The SharePoint 2010 Framework (both the SharePoint Foundation 2010 and SharePoint Server 2010) takes care of automatically disposing this object.

using (SPSite oSPSite = new SPSite("http://yoursitecollection")) 

{

using (SPWeb oSPWeb = oSPSite.OpenWeb())

{

SPList oSPList =oSPWeb.Lists ["Shared Documents"];

SPWeb oSPWebNew = oSPList.ParentWeb; //Do not explicitly dispose this

}

}
2. Explicit Disposal of SPWeb objects created when accessing SPWeb.ParentWeb property

It is recommended to explicitly dispose SPWeb objects created by accessing SPWeb.Webs property. In the following code-snippet explicitly dispose oSPWebNew object by an explicity try-finally clause. Because this is not handled by the framework

Using (SPSite oSPSite = new SPSite ("http://yoursitecollection"))

{

Using (SPWeb oSPWeb = oSPSite.OpenWeb ())

{

foreach (SPWeb oSPWebNew in oSPSite.Webs)

{

try //use a try-finally block to explicitly dispose every instance of oSPWebNew

{

// ...

}

finally

{

if(oSPWebNew != null)

oSPWebNew.Dispose ();

}

}

} // Dispose is automatically called for oSPWeb

}
3. Explicit Disposal of SPWeb objects created by when invoking SPWeb.Webs.Add method

It is recommended to explicitly dispose SPWeb objects created by invoking SPWeb.Webs.Add method. In the following code snippet, the SPWeb object created by invoking SPWeb.Webs.Add method need to be explicitly disposed using ‘Using’ clause.

using (SPSite oSPSite = new SPSite("http://yoursitecollection"))

{

using (SPWeb oSPWeb = oSPSite.OpenWeb())

{

//Explicitly use the Using Clause to dispose SPWeb objects

using (SPWeb oSPWebNew = oSPWeb.Webs.Add(strWebUrl))

{

}

} 

}
4. Explicit Disposal of SPWeb objects when invoking SPWeb.Webs[] index operator

It is recommended to explicitly dispose SPWeb objects created by accessing SP.Webs[] index operator in web collection. In the following snippet, the object oSPWeb2 needs to be explicitly disposed.

int i;

SPWeb oSPWeb, oSPWebNew;

SPSite oSPSite = new SPSite ("//yoursitecollection");

using (oSPWeb = oSPSite.OpenWeb())

{

for(i = 0;i < oSPWeb.Webs.Count;i++)

{

oSPWebNew = oSPWeb.Webs[i];

//do some arbitrary operation

oSPWeb2.Dispose();

}

}
5. Explicit Disposal of SPWeb objects when invoking Microsoft.SharePoint.Portal.SiteData.Area.Web Property

It is recommended to explicitly dispose SPWeb objects created by accessing SharePoint.Portal.SiteData.Area. Though Area and AreaManager is obsolete in SharePoint 2010 and it exists more in legacy code. This should be considered while migrating legacy SharePoint code to SharePoint 2010

int i;

SPWeb oSPWeb, oSPWebNew;

SPSite oSPSite = new SPSite("//yoursitecollection");

using(oSPWeb = oSPSite.OpenWeb())

{

for(i = 0;i < oSPWeb.Webs.Count;i++)

{

oSPWebNew = oSPWeb.Webs[i];

//do some arbitrary operation

oSPWeb2.Dispose();

}

}
6. Explicit Disposal of SPWeb objects when accessing Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager property

It is recommended to explicitly dispose SPLimitedWebPartManager object while accessing Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager property. Because SPLimitedWebPartManager internally creates an instance of SPWeb object .

using (SPSite oSPSite = new SPSite("http://youroSPSite"))

{

using (SPWeb oSPWeb = oSPSite.OpenWeb())

{

SPFile oPage = web.GetFile("Folder_Name/Source_Page");

SPLimitedWebPartManager oSPLimitedWebPartManager =

oPage.GetLimitedWebPartManager(PersonalizationScope.Shared);

//Explicity dispose oSPLimitedWebPartManager object

oSPLimitedWebPartManager.Web.Dispose();

} 

}
7. Handling Microsoft.SharePoint.Publishing.PublishingWeb objects

The PublishingWeb and PublishingWebCollection come into picture when we develop using SharePoint Server 2010. It is recommended to explicitly close the each instance of PublishingWeb object accessed inside PublishingWebCollection.

Using (SPSite oSPSite = new SPSite("http://youroSPSite"))

{

Using (SPWeb oSPWeb = oSPSite.OpenWeb())

{

PublishingWeb oPublishingWeb = PublishingWeb.GetPublishingWeb(oSPWeb);

PublishingWebCollection oPublishingWebCollection = oPublishingWeb.GetPublishingWebs();

foreach (PublishingWeb oPublishingWebNew in oPublishingWebCollection)

{

try

{

// ...

}

finally

{

if(oPublishingWebNew != null)

oPublishingWebNew.Close();

}

}

}

}
This post completes the 3 part series of guidance on disposing SharePoint objects.

 Subscribe to my blog

One thought on “SharePoint 2010 Recommended practices for disposing objects–part3

Leave a Reply