I'm working on a Custom Workflow activity. In the activity I use the WorkflowContext-object. From this object I get the current SPSite or SPWeb via the Site and Web properties of the WorkflowContext. Which I use in combination with the ListId and ItemId.

Do I need to dispose these SPSite and SPWeb objects?

My gut feeling says no, since they come from a 'context'. But I can't find a authoritative answer.

  • I tried SPDisposeCheck with and without disposing. Neither resulted in a warning/error.
    – Dribbel
    Sep 24, 2013 at 7:11
  • It seems logical to not dispose context objects, since they are used by the workflow per se Sep 24, 2013 at 7:12
  • But how can I be sure?
    – Dribbel
    Sep 24, 2013 at 7:15
  • If SPDisposeCheck is on your side, and you see no ULS logs about undisposed objects, you should be fine Sep 24, 2013 at 7:19

1 Answer 1


yes you need to sometimes ;)! context is NOT using the current context if that makes sense! I belive the workflow runs on a seperate thread to sharepoint context! The context is the workflow context to get the values where the workflow is running under.

getting the context to get the spweb and/or spsite means sharepoint will NOT handle the disposing, depending on what method you use!

You only need to dispose of it, if you create the spsite or spweb (new thread) or if its stated in msdn as somthing you have to do manually! like creating a new workflow context.

it is stated in msdn to using the disposing! , you can use the using method or call the disposable method:

protected override void Dispose(bool disposing)
    if (disposing)

// Dispose the workflow properties and workflow context when disposing the workflow

if you dont it would lead to memory leaks!


if it implements the IDisposable interface. Microsoft .NET requires objects that implement the IDisposable interface to properly cleanup the unmanaged resources by explicitly calling the Dispose() method when you are finished using them. That is if your creating like i said new ;)

having said that just to confuse you! ;) you do not need to dispose of the following:

workflowProperties.Web, workflowProperties.Site and workflowProperties.Item.

so somthing like:

SPContext context = SPContext.GetContext(workflowProperties.Web);

SPSite currentSite = context.Site;

as you can see im not using new, as im using the workflow properties context, so no need to dispose!

doing this tho:

WorkflowContext wfContext = new WorkflowContext();

you do need to dispose as your creating a new context workflow ;)

or somthing on the lines of:

SPSite site = new SPSite(workflowProperties.SiteId)

the above will need to implement the IDisposable method or use the using! Reason being, your creating a new thread (spsite) but only using the current workflow context for the site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.