SharePoint 2010 Recommended practices for disposing objects–Part2

This post is the continuation of the post titled SharePoint 2010 Recommended Practices for disposing objects – part1

1. Disposing Framework Created Objects

a)The SPSite objects created through ‘Add’ method of SPSiteCollection class should be disposed explicitly. This is not managed by the Framework. Only objects created from SPContext is managed by the Framework.

SPWebApplication oSPWebApplication = new SPSite("http://yoursitecollection").WebApplication;

SPSiteCollection oSPSiteCollection = webApp.Sites;

using (SPSite oSPSiteCollection = siteCollections.Add("sites/myNewSiteCollection", "DOMAIN\\User", 

"test.user@testdommain.com"))

{

}

b)Explicitly Dispose SPSite objects created through SPSiteCollection[Index] property. Because accessing SPSiteCollection[Index] property

returns a new instance of SPSite object. This needs to be explicitly disposed. In the following case, the oSPSiteNew object needs to be explicitly disposed either using try-finally or using clause.

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

{

SPSite oSPSiteNew = null;

try

{

SPWebApplication oSPWebApplication = oSPSiteInner.WebApplication;

SPSiteCollection oTempSiteCollections = webApp.Sites;

oSPSiteNew = siteCollections[0];

}

finally

{

if (oSPSiteNew != null)

oSPSiteNew .Dispose();

}

}

c)Explicitly dispose SPSite objects accessed inside for-each loop using try-finally clause.

foreach (SPSite oSPSiteTemp in siteCollections)

{

try

{

// ...

}

finally

{

if(oSPSiteTemp != null)

oSPSiteTemp.Dispose();

}

}

d)Explicitly dispose SPWeb objects created through Add method (SPSite.AllWebs.Add Method) of SPWebCollection class.

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

{

using (SPWeb oSPWeb = oSPSite.AllWebs.Add("site-relative URL"))

{

} // oSPWeb is automatically disposed by Using clause

} // oSPSite is automatically disposed by Using clause

e)Explicitly Dispose SPWeb objects accessed through SPWebCollection[Index] property. Because accessing SPWebCollection[Index] property returns a new instance of SPWeb object. This needs to be explicitly disposed. In the following the object oSPWebNew needs to be explicitly disposed by using clause or try-finally clause.

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

{

using (SPWeb oSPWeb = siteCollection.OpenWeb())

{

SPWebCollection webCollection = oSPSite.AllWebs; 

using (SPWeb oSPWebNew = webCollection.Add(strWebUrl))

{

//this will dispose oSPWebNew

}

} // 

}

f)Explicitly dispose SPWeb objects accessed inside for-each loop using ‘try-finally’ clause

foreach (SPWeb oSPWebNew in oSPSite.AllWebs)

{

try

{

// ...

}

finally

{

if(oSPWebNew != null)

oSPWebNew.Dispose();

}

}

2.Disposing SPWeb objects created through SPSite.OpenWeb()

Explicitly dispose SPWeb objects created through OpenWeb method. Because OpenWeb method creates a new instance of SPWeb object, which is not managed by the framework

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

{

using (SPWeb web = oSPSite.OpenWeb())

{

} // SPWeb object web.Dispose() automatically called.

}

3.Disposing SPSite objects accessed through SPSite.RootWeb property

a)Do not explicitly dispose SPSite.RootWeb property. This does not hold good for SharePoint Server 2010 and SharePoint Foundation 2010 anymore. The RootWeb property is internally disposed by SP Foundation 2010 or SP Server 2010

b)Do not explicitly call Dispose method on SPSite.RootWeb property

4.Disposing SPSite objects created through Microsoft.Office.Server.UserProfiles.PersonalSite

Explicitly dispose SPSite objects created by accessing that Microsoft.Office.Server.UserProfiles.PersonalSite property

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

{

UserProfileManager oUserProfileManager = new UserProfileManager(ServerContext.GetContext(siteCollection));

UserProfile oUserProfile = oUserProfileManager.GetUserProfile("domain\\username");

using (SPSite oSPSiteNew = oUserProfile.PersonalSite)

{

//enclosed using clause will take care of disposing SPSite objected created by accessing the PersonalSite property

}

}

 Subscribe to my blog

2 thoughts on “SharePoint 2010 Recommended practices for disposing objects–Part2

  1. Pingback: How to implement logging in SharePoint 2010–part 2 - My experiments with SharePoint, Azure and .NET using Visual Studio

  2. Pingback: SharePoint 2010 Recommended practices for disposing objects–Part2 - My experiments with SharePoint, Azure and .NET using Visual Studio

Add Comment Register



Leave a Reply