Tuesday, April 8, 2008

Optimize custom Task Form or WebPage in WSS3.0 – Step By Step Approach

To optimize task form or web page in WSS,

  1. In Task form or web page, use only one SpWeb and SPSite object throught out the page. Avoide creating SPSite or SPWeb objects at multiple places.


     

  2. Always make sure to Dispose the SPSite and SPWeb objects after finish use of it. For this we can follow following coding practice as per situation.
    1. "Using" technique : This is the best technique.

      Here is the sample example for this.

void PublishingWebCollectionBestPractice()

{

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

{

using (SPWeb web = siteCollection.OpenWeb())

{

PublishingWeb outerPubWeb = PublishingWeb.GetPublishingWeb(web);

PublishingWebCollection pubWebCollection = outerPubWeb.GetPublishingWebs();

foreach (PublishingWeb innerPubWeb in pubWebCollection)

{

// innerPubWeb referenced so we must call outerPubWeb.Close()

// otherwise, we will leak each and every innerPubWeb object

innerPubWeb.Close();

}

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

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

}

  1. "Finally" – Alternative way.

    Dispose objects in finally clause.

    Here is the sample example.

String str;

SPSite oSPSite = null;

SPWeb oSPWeb = null;

try

{

oSPSite = new SPSite("http://server");

oSPWeb = oSPSite.OpenWeb(..);

str = oSPWeb.Title;

}

catch(Exception e)

{

}

finally

{

if (OurObject != null)

{

OurObject.Dispose();

GC.SuppressFinalize(OurObject);

}

if (OurSiteObject != null)

{

OurSiteObject.RootWeb.Dispose();

OurSiteObject.Dispose();

GC.SuppressFinalize(OurSiteObject);

}

}


 

  1. Whenever required to pass the SPWeb or SPSite Objects, pass by reference (ref parameter). Avoid to pass it by value. It creates multiple objects each time they pass.

        Like

SPWeb sourceWeb = sourceSite.OpenWeb();

string smtpServerName = GetSettingsValue(ref sourceWeb, "SMTPServerName", "String Value");


 


 

Other good references,


 

  1. http://www.webdeveloper.com/forum/archive/index.php/t-154151.html
  2. http://blog.gemsolution.co.uk/2007/06/15/disposing-of-spsite-and-spweb-objects/
  3. http://msdn2.microsoft.com/en-us/library/yh598w02(VS.80).aspx
  4. http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx
  5. http://support.microsoft.com/default.aspx?scid=901259
  6. http://www.bluedoglimited.com/SharePointThoughts/ViewPost.aspx?ID=73
  7. http://209.85.173.104/search?q=cache:jZ-GHkoMd9oJ:blog.developers.ie/cgreen/archive/2006/09/26/2274.aspx+SPWeb.ParentWeb+%2B+Dispose&hl=en&ct=clnk&cd=8&gl=us

No comments: