Skip to content
Tags

When not to dispose SharePoint objects

December 14, 2010

Whenever you create a new instance of the SPSite and SPWeb objects you need to make sure that these objects are disposed of properly when the are no longer needed.

However there are situations when you don need to (actually you mustn’t) dispose these objects. It is not a well documented area of sharepoint, the SPDisposeCheck tool also doesn’t fully cover these cases. In general it is true that you mustn’t dispose of the context and parent site or web objects.

So you mustn’t dispose the following objects:
(not a working example, just for demonstration…)

//demo objects
SPSite site;
SPWeb web;
SPList list;
SPListItem li;
SPListEventProperties propList;
SPFeatureReceiverProperties propFeature;
SPWebEventProperties propWeb;
SPItemEventProperties propItem;

//don't dispose this object
object o;

// Current or parent Web
o = SPControl.GetContextWeb(null); 
o = SPContext.Current.Web ;
o = site.RootWeb;
o = web.ParentWeb;
o = list.ParentWeb;
o = li.Web;
o = propList.Web;
o = propList.List.ParentWeb;
o = propWeb.Web;
o = propItem.ListItem.Web;

// Current or parent site
o = SPControl.GetContextSite(null);
o = SPContext.Current.Site;
o = web.Site;

// Parent site or web or farm
o = propFeature.Feature.Parent;
Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: