Pokemon Exception Handling

Repeated exposure to sub-optimal code and a 7 year old son obsessed with Pokemon and here’s what you get:

(If you don’t have a 7 year old boy and aren’t familiar with the song you can read and listen to YouTube’s offering in all it’s delicious cheesiness.)

Exception! (Gotta Catch em all)

 (Pokemon theme song)

I wanna code the very best

Like no bug ever was

To catch them is my real test

To suppress them is my cause

I will travel across the land

Searching far and wide

Each exception to just ignore

The message that’s inside

Exception! (gotta catch em all) it’s you and me

I know it’s my destiny,

Exception! Oh you’re my best friend

But our code we must defend

Exception! (gotta catch em all) it’s sad but true

Our catch block will pull us through,

You breach me and I’ll teach you,

Exception! gotta catch’em all

Gotta catch em all

Yeah…

Every challenge along the way

With courage I will face.

I will battle every day

To claim my bug-free space.

“Finally”,

Don’t feel neglected

There’s no better theme.

We can expect the unexpected

A project manager’s dream!

Exception! (gotta catch em all) it’s you and me

I know it’s my destiny,

Exception!  Oh you’re my best friend

But our code we must defend

Exception! (gotta catch em all) it’s sad but true

Our catch block will pull us through,

You breach me and I’ll teach you,

Exception! gotta catch’em all

Gotta catch em all

Gotta catch em all

Gotta catch em all

Gotta catch em all

Yeah…

(Guitar Solo)

Exception! (gotta catch em all) it’s you and me

I know it’s my destiny,

Exception!  Oh you’re my best friend

But our code we must defend

Exception! (gotta catch em all) it’s sad but true

Our catch block will pull us through,

You breach me and I’ll teach you,

Exception! gotta catch’em all

Gotta catch em all

Exception!

Advertisements

Proxy Layer

Proxy Layer

(To the tune Foxy Lady by Jimi Hendrix)

Proxy

Proxy

You know you’re a cute little mediator

Proxy

You know you’re a sweet little interfacer

Proxy

I want a good response

From your sweet service farm, oh

You’ve got to be so light, so light

Ooh, proxy layer

I see you, heh, gettin’ in between

Proxy

You make me wanna do it asynchronously

Proxy

Ah, baby listen now

I’ve made up my mind yeah

I’m tired of wasting all my precious RAM

You’ve got to be so light, so light

Proxy layer

Here I come

I want a good response

From your sweet service farm, oh

You’ve got to be so light, so light

Ooh, proxy layer

Here I come baby

I’ll dependency inject ya

Ow proxy layer

You broker so good

Yeah, proxy

Yeah, give us some

Proxy

Mystery Solved! (SPDisposeCheck Missing Memory Leaks?)

Evidently the reason some memory leaks from Roger Lamb’s blog didn’t appear in my SPDisposeCheck test have to do with some offending sample code being optimized out when compiled to release mode. When compiling to debug all the offending samples are identified by SPDisposeCheck. Note the differences identified in Reflector.

 
 

Release Version:

 
 

 
 

Screen clipping taken: 3/20/2009, 2:32 PM

 
 

 
 

 
 

Debug Version:

 
 

 
 

Screen clipping taken: 3/20/2009, 2:33 PM

 
 

 
 

 
 

Thanks Matt Ranlett and Paul Andrew for solving the mystery.

SPDisposeCheck Missing Memory Leaks?

Checking SharePoint memory leaks I came across this blog entry http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_300 describing different types of coding no-nos that can cause memory leaks. As an exercise I created an assembly with all these samples and ran SPDisposeCheck against the assembly and discovered several of the examples were NOT identified by SPDisposeCheck. I wonder If I could be missing something?
 

The following examples were not identified:

 
 

CreatingSPSiteLeak

AllWebsForEachLeak

AllWebsIndexerLeak

SPLimitedWebPartManagerLeak

WebsLeak

PublishingWebCollectionLeak

GetVariationLeak

PersonalSiteLeak

SPSiteCollectionIndexerLeak

SPSiteCollectionForEachLeak

CrossMethodLeak.MethodB

CrossMethodLeak.MethodC

 
 

 
 

SPDispose output text file:

 
 

Note: This tool may report errors which are not actually memory leaks, otherwise known as false positives.

Further investigation should be done to identify and correct real errors.

It is designed to assist developers in making sure their code adheres to best practices for memory allocation when using SharePoint APIs.

Please see the following for more information:

http://blogs.msdn.com/rogerla/

http://msdn2.microsoft.com/en-us/library/aa973248.aspx

http://msdn2.microsoft.com/en-us/library/bb687949.aspx

———————————————————-

 
 

 
 

ID: SPDisposeCheckID_110

Module: SPDisposeTest.dll

Method: SPDisposeTest.Class1.OpenWebLeak

Statement: local0 := new Microsoft.SharePoint.SPSite(Microsoft.SharePoint.SPContext.get_Current().{Microsoft.SharePoint.SPContext}get_Web().{Microsoft.SharePoint.SPWeb}get_Url()).{Microsoft.SharePoint.SPSite}OpenWeb()

Notes: Constructor called for Microsoft.SharePoint.SPSite but not assigned. This type should be assigned and subsequently disposed

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110

———————————————————-

 
 

 
 

ID: SPDisposeCheckID_150

Module: SPDisposeTest.dll

Method: SPDisposeTest.Class1.AllWebsAddLeak

Statement: OP_POP local0.{Microsoft.SharePoint.SPSite}get_AllWebs().{Microsoft.SharePoint.SPWebCollection}Add(“site-relative URL”)

Notes: Call to SPWebCollection.Add without capturing return value which should be disposed or closed

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_150

———————————————————-

 
 

 
 

ID: SPDisposeCheckID_150

Module: SPDisposeTest.dll

Method: SPDisposeTest.Class1.WebsAddLeak(System.String)

Statement: OP_POP local1.{Microsoft.SharePoint.SPWeb}get_Webs().{Microsoft.SharePoint.SPWebCollection}Add(strWebUrl)

Notes: Call to SPWebCollection.Add without capturing return value which should be disposed or closed

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_150

———————————————————-

 
 

 
 

ID: SPDisposeCheckID_150

Module: SPDisposeTest.dll

Method: SPDisposeTest.Class1.SPWebCollectionAddLeak(System.String)

Statement: OP_POP local2.{Microsoft.SharePoint.SPWebCollection}Add(strWebUrl)

Notes: Call to SPWebCollection.Add without capturing return value which should be disposed or closed

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_150

———————————————————-

 
 

 
 

ID: SPDisposeCheckID_110

Module: SPDisposeTest.dll

Method: SPDisposeTest.Class1.SPSiteCollectionAddLeak

Statement: local0 := new Microsoft.SharePoint.SPSite(“http://moss”).{Microsoft.SharePoint.SPSite}get_WebApplication()

Notes: Constructor called for Microsoft.SharePoint.SPSite but not assigned. This type should be assigned and subsequently disposed

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110

———————————————————-

 
 

 
 

ID: SPDisposeCheckID_240

Module: SPDisposeTest.dll

Method: SPDisposeTest.Class1.SPSiteCollectionAddLeak

Statement: OP_POP local1.{Microsoft.SharePoint.Administration.SPSiteCollection}Add(“sites/myNewSiteCollection”, “DOMAIN\\User”, “roger.lamb@litwareinc.com”)

Notes: Call to SPSiteCollection.Add without capturing return value which should be disposed or closed

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_240

———————————————————-

 
 

 
 

ID: SPDisposeCheckID_110

Module: SPDisposeTest.dll

Method: SPDisposeTest.Class1.SPSiteCollectionAddNoLeak

Statement: local0 := new Microsoft.SharePoint.SPSite(“http://moss”).{Microsoft.SharePoint.SPSite}get_WebApplication()

Notes: Constructor called for Microsoft.SharePoint.SPSite but not assigned. This type should be assigned and subsequently disposed

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110

———————————————————-

 
 

 
 

ID: SPDisposeCheckID_110

Module: SPDisposeTest.dll

Method: SPDisposeTest.Class1+CrossMethodLeak.MethodA

Statement: this._siteCollection := new Microsoft.SharePoint.SPSite(“http://moss”)

Notes:          Disposable type not disposed: Microsoft.SharePoint.SPSite

***This may be a false positive depending on how the type was created or if it is disposed outside the current scope

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110

———————————————————-

 
 

 
 

ID: SPDisposeCheckID_120

Module: SPDisposeTest.dll

Method: SPDisposeTest.Class1+CrossMethodLeak.MethodA

Statement: this._web := this._siteCollection.{Microsoft.SharePoint.SPSite}OpenWeb()

Notes:          Disposable type not disposed: Microsoft.SharePoint.SPWeb

***This may be a false positive depending on how the type was created or if it is disposed outside the current scope

More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_120

———————————————————-

 
 

Total Found: 9

 
 

———————————————————-

 
 

Modules Checked: 1

———————————————————-

spdisposetest.dll

———————————————————-

 
 

Modules Ignored: 0

———————————————————-

———————————————————-

 
 

Methods Ignored: 0

———————————————————-

 
 

 
 

 
 

The source code for my test assembly:

 
 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.Office.Server;

using Microsoft.Office.Server.UserProfiles;

using Microsoft.SharePoint.Utilities;

using Microsoft.SharePoint.Publishing;

using Microsoft.SharePoint.Portal.WebControls;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

using Microsoft.SharePoint.Administration;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web;

 
 

 
 

namespace SPDisposeTest

{

public
class
Class1

{

void CreatingSPSiteLeak()

{

SPSite siteCollection = new SPSite(http://moss);

// siteCollection leaked

}

 
 

void CreatingSPSiteExplicitDisposeNoLeak()

{

SPSite siteCollection = null;

try

{

siteCollection = new SPSite(http://moss);

}

finally

{

if (siteCollection != null)

siteCollection.Dispose();

}

}

 
 

void CreatingSPSiteWithAutomaticDisposeNoLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void OpenWebLeak()

{

using (SPWeb web = new SPSite(SPContext.Current.Web.Url).OpenWeb())

{

// SPSite leaked !

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

}

 
 

void OpenWebNoLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void AllWebsForEachLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb outerWeb = siteCollection.OpenWeb())

{

foreach (SPWeb innerWeb in siteCollection.AllWebs)

{

// explicit dispose here to avoid OOM’s with large # of webs

}

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void AllWebsForEachNoLeakOrMemoryOOM()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb outerWeb = siteCollection.OpenWeb())

{

foreach (SPWeb innerWeb in siteCollection.AllWebs)

{

try

{

// …

}

finally

{

if (innerWeb != null)

innerWeb.Dispose();

}

}

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void AllWebsIndexerLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

SPWeb web = siteCollection.AllWebs[0];

// SPWeb web leaked

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void AllWebsIndexerNoLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.AllWebs[0])

{

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void AllWebsAddLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

SPWeb web = siteCollection.AllWebs.Add(“site-relative URL”);

// SPWeb web Leaked

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void AllWebsAddNoLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.AllWebs.Add(“site-relative URL”))

{

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void SPLimitedWebPartManagerLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

SPFile page = web.GetFile(“Source_Folder_Name/Source_Page”);

SPLimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);

// SPWeb object webPartManager.Web leaked

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void SPLimitedWebPartManagerNoLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

SPFile page = web.GetFile(“Source_Folder_Name/Source_Page”);

using (SPLimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))

{

try

{

// …

}

finally

{

webPartManager.Web.Dispose();

}

}

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void WebsLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb outerWeb = siteCollection.OpenWeb())

{

foreach (SPWeb innerWeb in outerWeb.Webs)

{

// SPWeb innerWeb leak

}

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void WebsNoLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb outerWeb = siteCollection.OpenWeb())

{

foreach (SPWeb innerWeb in outerWeb.Webs)

{

try
//should be 1st statement after foreach

{

// …

}

finally

{

if (innerWeb != null)

innerWeb.Dispose();

}

}

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void WebsAddLeak(string strWebUrl)

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

SPWeb addedWeb = web.Webs.Add(strWebUrl); // will leak

 
 

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void WebsAddNoLeak(string strWebUrl)

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

using (SPWeb addedWeb = web.Webs.Add(strWebUrl))

{

//..

}

 
 

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void SPWebCollectionAddLeak(string strWebUrl)

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb outerWeb = siteCollection.OpenWeb())

{

SPWebCollection webCollection = siteCollection.AllWebs; // no AllWebs leak just getting reference

SPWeb innerWeb = webCollection.Add(strWebUrl); // must dispose of innerWeb

// innerWeb Leak

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void SPWebCollectionAddNoLeak(string strWebUrl)

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb outerWeb = siteCollection.OpenWeb())

{

SPWebCollection webCollection = siteCollection.AllWebs; // no AllWebs leak just getting reference

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

{

//…

}

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void SPControlBADPractice()

{

HttpContext Context = null;

SPSite siteCollection = SPControl.GetContextSite(Context);

siteCollection.Dispose(); // DO NOT DO THIS

SPWeb web = SPControl.GetContextWeb(Context);

web.Dispose(); // DO NOT DO THIS

}

 
 

void SPControlBestPractice()

{

HttpContext Context = null;

SPSite siteCollection = SPControl.GetContextSite(Context);

SPWeb web = SPControl.GetContextWeb(Context);

// Do NOT call Dispose()

}

 
 

void SPContextBADPractice()

{

SPSite siteCollection = SPContext.Current.Site;

siteCollection.Dispose(); // DO NOT DO THIS

SPWeb web = SPContext.Current.Web;

web.Dispose(); // DO NOT DO THIS

}

 
 

void SPContextBestPractice()

{

SPSite siteCollection = SPContext.Current.Site;

SPWeb web = SPContext.Current.Web;

// Do NOT call Dispose()

}

 
 

void PublishingWebCollectionLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

// passing in web you own, no dispose needed on outerPubWeb

PublishingWeb outerPubWeb = PublishingWeb.GetPublishingWeb(web);

 
 

PublishingWebCollection pubWebCollection = outerPubWeb.GetPublishingWebs();

foreach (PublishingWeb innerPubWeb in pubWebCollection)

{

// innerPubWeb leak

}

// PublishingWeb will leak for each innerPubWeb referenced

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void PublishingWebCollectionNoLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

// passing in web you own, no dispose needed on outerPubWeb

PublishingWeb outerPubWeb = PublishingWeb.GetPublishingWeb(web);

PublishingWebCollection pubWebCollection = outerPubWeb.GetPublishingWebs();

foreach (PublishingWeb innerPubWeb in pubWebCollection)

{

try

{

// …

}

finally

{

if (innerPubWeb != null)

innerPubWeb.Close();

}

}

// outerPubWeb.Close(); not needed and if called will log warning in ULS log

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void GetPublishingWebNoLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

// passing in web you own, no dispose needed on singlePubWeb

PublishingWeb singlePubWeb = PublishingWeb.GetPublishingWeb(web);

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void GetVariationLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web); // Passing in web so no Close() needed

VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];

PublishingWeb variationPublishingWeb = publishingWeb.GetVariation(variationLabel); // must be Closed()

// …

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void GetVariationNoLeak()

{

using (SPSite siteCollection = new SPSite(http://moss))

{

using (SPWeb web = siteCollection.OpenWeb())

{

PublishingWeb variationPublishingWeb = null;

try

{

PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web); // Passing in web so no Close() needed

VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];

variationPublishingWeb = publishingWeb.GetVariation(variationLabel); // must be Closed()

// …

}

finally

{

if (variationPublishingWeb != null)

variationPublishingWeb.Close();

}

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

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

void PersonalSiteLeak()

{

// open a site collection

using (SPSite siteCollection = new SPSite(http://moss))

{

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

UserProfile profile = profileManager.GetUserProfile(“domain\\username”);

SPSite personalSite = profile.PersonalSite; // will leak

}

}

 
 

void PersonalSiteNoLeak()

{

// open a site collection

using (SPSite siteCollection = new SPSite(http://moss))

{

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

UserProfile profile = profileManager.GetUserProfile(“domain\\username”);

using (SPSite personalSite = profile.PersonalSite)

{

// …

}

}

}

 
 

void SPSiteCollectionIndexerLeak()

{

using (SPSite siteCollectionOuter = new SPSite(http://moss))

{

SPWebApplication webApp = siteCollectionOuter.WebApplication;

SPSiteCollection siteCollections = webApp.Sites;

 
 

SPSite siteCollectionInner = siteCollections[0];

// SPSite siteCollectionInner leak

} // SPSite object siteCollectionOuter.Dispose() automatically called

}

 
 

void SPSiteCollectionIndexerNoLeak()

{

using (SPSite siteCollectionOuter = new SPSite(http://moss))

{

SPSite siteCollectionInner = null;

try

{

SPWebApplication webApp = siteCollectionOuter.WebApplication;

SPSiteCollection siteCollections = webApp.Sites;

 
 

siteCollectionInner = siteCollections[0];

}

finally

{

if (siteCollectionInner != null)

siteCollectionInner.Dispose();

}

} // SPSite object siteCollectionOuter.Dispose() automatically called

}

 
 

void SPSiteCollectionForEachLeak()

{

using (SPSite siteCollectionOuter = new SPSite(http://moss))

{

SPWebApplication webApp = siteCollectionOuter.WebApplication;

SPSiteCollection siteCollections = webApp.Sites;

 
 

foreach (SPSite siteCollectionInner in siteCollections)

{

// SPSite siteCollectionInner leak

}

} // SPSite object siteCollectionOuter.Dispose() automatically called

}

 
 

void SPSiteCollectionForEachNoLeak()

{

using (SPSite siteCollectionOuter = new SPSite(http://moss))

{

SPWebApplication webApp = siteCollectionOuter.WebApplication;

SPSiteCollection siteCollections = webApp.Sites;

 
 

foreach (SPSite siteCollectionInner in siteCollections)

{

try

{

// …

}

finally

{

if (siteCollectionInner != null)

siteCollectionInner.Dispose();

}

}

} // SPSite object siteCollectionOuter.Dispose() automatically called

}

 
 

void SPSiteCollectionAddLeak()

{

SPWebApplication webApp = new SPSite(http://moss”).WebApplication;

SPSiteCollection siteCollections = webApp.Sites;

SPSite siteCollection = siteCollections.Add(“sites/myNewSiteCollection”, “DOMAIN\\User”, “roger.lamb@litwareinc.com”);

// SPSite siteCollection leak

}

 
 

void SPSiteCollectionAddNoLeak()

{

SPWebApplication webApp = new SPSite(http://moss”).WebApplication;

SPSiteCollection siteCollections = webApp.Sites;

using (SPSite siteCollection = siteCollections.Add(“sites/myNewSiteCollection”, “DOMAIN\\User”, “roger.lamb@litwareinc.com”))

{

} // SPSite object siteCollection.Dispose() automatically called

}

 
 

public
class
CrossMethodLeak

{

private SPSite _siteCollection = null;

private SPWeb _web = null;

 
 

public
void MethodA()

{

_siteCollection = new SPSite(http://moss);

_web = _siteCollection.OpenWeb();

}

 
 

public
void MethodB()

{

if (_web != null)

{

string title = _web.Title;

}

}

 
 

public
void MethodC()

{

if (_web != null)

{

string name = _web.Name;

}

}

}

}

}

 
 

 UPDATE:  Mystery Solved! – https://stephenvick.wordpress.com/2009/03/20/mystery-solved-spdisposecheck-missing-memory-leaks/

CreateDate for List Items in Manage Site Content and Structure

Just discovered that when you Copy or Move content using Manage Site Content and Structure the target content has the EXACT same CreateDate +1 second. Not sure why this is but since the GUIDs are not preserved for either move or copy this could end up being very useful if you have data in SQL outside of SharePoint that references the ObjectID.

 
 

 
 

Screen clipping taken: 1/29/2009, 1:01 PM

 
 

Move or Copy

 
 

 
 

Screen clipping taken: 1/29/2009, 1:02 PM

 
 

Select destination

 
 

 
 

Screen clipping taken: 1/29/2009, 1:02 PM

 
 

Copy in progress

 
 

 
 

Screen clipping taken: 1/29/2009, 1:04 PM

 
 

CreateDate before Copy/Move

&

 
 

 
 

Screen clipping taken: 1/29/2009, 1:05 PM

 
 

Target is CreateDate is exactly the same +1 second.

o

Art Vandelay – Import/Export SharePoint Content in C#

Here is some handy dandy code for Exporting content from a SharePoint site and importing into another.

 
 

 
 

using System;

using System.Collections.Generic;

using System.Text;

 
 

namespace ContentMoveTest

{

class
Program

{

static
void Main(string[] args)

{

ExportImport(http://www.source.com, http://www.target.com);

}

 
 

 
 

static
void ExportImport(string sourceUrl, string destinationUrl)

{

string fileName = Export(sourceUrl);

Console.WriteLine(“Local filename for this export/import is {0}”, fileName);

Import(destinationUrl, fileName);

}

 
 

static
string Export(string siteURL)

{

Microsoft.SharePoint.Deployment.SPExportSettings exportSettings = new Microsoft.SharePoint.Deployment.SPExportSettings();

 
 

exportSettings.AutoGenerateDataFileName = true;

exportSettings.ExportMethod = Microsoft.SharePoint.Deployment.SPExportMethodType.ExportAll;

//exportSettings.ExportMethod = Microsoft.SharePoint.Deployment.SPExportMethodType.ExportChanges;

exportSettings.CommandLineVerbose = true;

exportSettings.SiteUrl = siteURL;

exportSettings.IncludeSecurity = Microsoft.SharePoint.Deployment.SPIncludeSecurity.All;

exportSettings.IncludeVersions = Microsoft.SharePoint.Deployment.SPIncludeVersions.All;

Microsoft.SharePoint.Deployment.SPExport export = new Microsoft.SharePoint.Deployment.SPExport(exportSettings);

Console.WriteLine(“Starting export of URL {0}”, siteURL);

export.Run();

return exportSettings.FileLocation + “\\” + exportSettings.BaseFileName;

}

 
 

static
void Import(string siteURL, string fileToImport)

{

Microsoft.SharePoint.Deployment.SPImportSettings importSettings = new Microsoft.SharePoint.Deployment.SPImportSettings();

 
 

importSettings.BaseFileName = System.IO.Path.GetFileName(fileToImport);

importSettings.FileLocation = System.IO.Path.GetDirectoryName(fileToImport);

importSettings.CommandLineVerbose = true;

importSettings.SiteUrl = siteURL;

importSettings.RetainObjectIdentity = true;

importSettings.IncludeSecurity = Microsoft.SharePoint.Deployment.SPIncludeSecurity.All;

importSettings.UpdateVersions = Microsoft.SharePoint.Deployment.SPUpdateVersions.Append;

importSettings.UserInfoDateTime = Microsoft.SharePoint.Deployment.SPImportUserInfoDateTimeOption.ImportAll;

Microsoft.SharePoint.Deployment.SPImport import = new Microsoft.SharePoint.Deployment.SPImport(importSettings);

Console.WriteLine(“Starting import to URL {0}”, siteURL);

import.Run();

}

 
 

}

}

Extending VariationRoot.aspx Redirection in SharePoint

Some code used to extend variation redirection logic based on a preferred locale setting in the users profile. If the property is not present or blank it falls back to default variations logic. A locale picker to set this value will be posted soon.

 
 

using System;

using Microsoft.SharePoint;

using CCE.SharePoint.UserControls;

using CCE.SharePoint.Utility;

using Microsoft.Office.Server.UserProfiles;

using Microsoft.SharePoint.Utilities;

 
 

namespace CCE.SharePoint.UserControls

{

public class VariationsRootLanding : CCEBaseUserControl

{

 
 

protected override void OnLoad(EventArgs e)

{

string targetUrl = this.GetRedirectTargetUrl();

 

if (!string.IsNullOrEmpty(targetUrl))

{

SPUtility.Redirect(targetUrl, SPRedirectFlags.Default, Context);

}

else

{

SPUtility.Redirect(“VariationRoot.aspx”, SPRedirectFlags.Default, Context);

}

}

 
 

private string GetRedirectTargetUrl()

{

String url = “”;

 
 

try

{

String loginName = SPContext.Current.Web.CurrentUser.LoginName;

 
 

UserProfile currentUserProfile = Utility.UserProfiles.GetUserProfile(loginName);

String preferredLocale = Utility.UserProfiles.GetSinglePropertyValue(currentUserProfile, “preferredLocale”);

 
 

using (SPSite _SPSite = new SPSite(SPContext.Current.Site.ID))

{

using (SPWeb rootWeb = _SPSite.RootWeb)

{

foreach (SPWeb subWeb in rootWeb.Webs)

{

if (subWeb.Locale.ToString() == preferredLocale)

{

url = “/” + subWeb.Name;

}

}

}

}

}

catch (Exception)

{

//Log error

}

 
 

return url;

}

}

}