Recently I figured out one terrible thing I was not supposed to do when doing SharePoint development:
using (var spSite = SPContext.Current.Site)
{
// do something
}
After some hair pulling I figured out that the code above was actually disposing my current SharePoint context.
This urges me to question if there are any other such common mistakes that I or any other SharePoint developer should not be making?
- Not disposing SPSite & SPWeb objects you "new" up in your code.
- Disposing Context.SPSite & SPWeb objects
- Disposing SPWeb.ParentWeb objects (this is done for you)
See Best Practices: Using Disposable Windows SharePoint Objects
- Not using Output Cache with Publishing sites
- Not using WSP solutions to deploy custom code changes.
For checking your code against these kind of mistakes (What to dispose and what not to), I would suggest using SPDisposeChecker.
http://archive.msdn.microsoft.com/SPDisposeCheck
Also, you can read article by Roger Lamb (on which this tool is based on) for understanding more about this:
http://blogs.msdn.com/b/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx
Hope it helps.
DG
精彩评论