SharePoint Best Practices : COM Objects Disposal

While writing the code with SharePoint Server Side Object Model I often stuck with a question “What all objects should I be responsible to get disposed off?”

Though this seems to be a simple question but I found it worth researching upon to extract the list of all the Objects for which a developer is responsible to get disposed off in order to avoid any memory leaks in the farm.

In order to simplified the story I have divided SharePoint Objects following three categories:

List of Objects that must be Disposed explicitly

  1. SPWeb Object created with SPSite.OpenWeb
  2. SPSite objects created with SPSite.SelfServiceCreateSite
  3. SPWeb created by SPLimitedWebPartManager
  4. SPSite created by SPSiteCollection.Add
  5. SPSite created by SPSiteCollection[] index operator
  6. SPWeb created by SPWebCollection.Add
  7. SPWeb created by SPWebCollection[] index operator
  8. SPSite created with UserProfiles.PersonalSite

List of Objects that must be Closed (Not Disposed) explicitly:

  1.  PublishingWeb created by PublishingWeb.GetPublishingWebs[] index operator
  2.  PublishingWeb created by PublishingWeb.GetVariation
  3.  PublishingWeb created by PublishingWebCollection.Add

List of Objects that must Never be Disposed explicitly

  1. SPListEventProperties.Web
  2. SPWebEventProperties.Web
  3. SPItemEventProperties.Web
  4. SPItemEventProperties.ListItem.Web
  5. SPItemEventProperties.Web.Site
  6. SPItemEventProperties.ListItem.Web.Site
  7. SPFeatureReceiverProperties.Feature.Parent
  8. SPSite.RootWeb
  9. SPWeb.ParentWeb
  10. SPList.ParentWeb
  11. SPSite returned by SPControl.GetContextSite
  12. SPWeb returned by SPControl.GetContextWeb
  13. SPSite returned by SPContext.Current.Site
  14. SPWeb returned by SPContext.Current.Web

How to ensure proper disposal of object after the job is done:

There are following two Constructs available that can ensure the disposal of SharePoint Objects even if any unforeseen runtime exception occur in the code.

1. Use “Using Block”

using(SPWeb oWeb = osite.RootWeb)
{
}//oWeb will get disposed automatically as soon as using block ends

2. Use “Try, Catch & Finally Block”

try
{
SPWeb oWeb = osite.RootWeb;
}
catch(Exception ex)
{
throw;
}
finally
{
oWeb.Dispose();  //oWeb will get disposed as soon as Dispose() method called.

}

Hope this will help someone in need…

Advertisements

SharePoint URL Tokens

SharePoint 2013 supports the tokens listed in the following tables for use in apps for SharePoint.

The StartPage column specifies whether the token can be used in the StartPage element of an app manifest.

The Custom Action column specifies whether the token can be used in the URL of a custom action on a host web.

The App Part column specifies whether the token can be used in the Src property of the app part.

Tokens that can be used at the beginning of a URL in an app for SharePoint.

Token Resolves to Start Page Custom Action App Part Remarks
~appWebUrl The URL of the app web of an app for SharePoint. Yes Yes Yes Should be used only outside an app web. Within the app web itself, use ~site for the URL of the app web.
~controlTemplates The URL of the ControlTemplates virtual folder for the current website. No No No
~hostUrl The URL of the host web. No No Yes
~hostLogoUrl The URL of the logo of the host web. No No No
~layouts The URL of the Layouts virtual folder for the current website. No No No
~remoteAppUrl The URL of a remote web application in an app for SharePoint. Yes Yes, in the host web, but No in the app web. Yes If you are not using Visual Studio and Microsoft Office Developer Tools to develop your app for SharePoint, you can use ~remoteAppUrl only in the StartPage URL of autohosted apps. However, when you are using Visual Studio and the tools, you can use this token for any provider-hosted app and it is resolved when Visual Studio packages the app. In this usage, it is really more of a Visual Studio token than a SharePoint token. It can be used outside the app manifest for both provider-hosted and autohosted apps.
~site The URL of the current website. No No Yes
~sitecollection The URL of the parent site collection of the current website. No No Yes

Except where indicated otherwise, none of the tokens in the next table can be used in the path portion of the Src property value of the app part. The App Part column refers to their use in the query string portion of the value.

Tokens that can be used inside a URL:
Token Resolves to Start Page Custom Action App Part Remarks
{AppContextToken} The OAuth context token for the app. No No No
{AppWebUrl} The URL of the app web in an app for SharePoint. Yes Yes Yes This token should be used only outside an app web. Within the app web itself, use {Site} for the URL of the app web.
{ClientTag} The client cache control number (client tag) for the current website. Yes Yes Yes
{HostLogoUrl} The logo for the host web of an app for SharePoint. Yes Yes Yes
{HostTitle} The title of the host web of an app for SharePoint. Yes Yes Yes
{HostUrl} The URL of the host web of an app for SharePoint. Yes Yes Yes
{ItemId} The ID of an item in a list or library (an integer). No Yes No
{ItemUrl} The URL of the item being acted upon. No Yes No
{Language} The current language/culture of the host web of an app for SharePoint. Yes Yes Yes
{ListId} The ID of the current list (a GUID). No Yes No
{ProductNumber} The full build version number of the SharePoint farm. Yes Yes Yes An example value is “15.0.4433.1011”.
{RecurrenceId} The recurrence index of a recurring event. No Yes No This token is not supported for use in the context menus of list items.
{RemoteAppUrl} The URL of a remote web application in an app for SharePoint. Yes Yes Yes
{Site} The URL of the current website. No Yes Yes
{SiteCollection} The URL of the parent site of the current website. No Yes Yes
{SiteUrl} The URL of the current website. No Yes No
{Source} The HTTP Request URL. No Yes No
{StandardTokens} See Remarks. Yes Yes Yes This combines five other tokens. It initially resolves to SPHostUrl={HostUrl}&SPAppWebUrl={AppWebUrl}&SPLanguage={Language}&SPClientTag={ClientTag}&SPProductNumber={ProductNumber}. Then each of these tokens resolves. If there is no app web, the portion &SPAppWebUrl={AppWebUrl} is not present.