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 } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
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 }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
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)) { } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
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(); } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
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(); } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
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(); } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
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.
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Pingback: SharePoint 2010 Recommended practices for disposing objects–part3 - My experiments with SharePoint, Azure and .NET using Visual Studio