Tuesday, May 8, 2012

SharePoint Best Practices when use object model

When we run SharePoint Site Without any code customization we will notice big difference in performance. There are many common things affect performance with object model:

  • Dispose SharePoint Objects:
    SharePoint object model contains objects that implement the IDisposable interface. You must take precautions when using these objects to avoid their long-term retention in memory in the Microsoft .NET Framework.
    Specifically, you should explicitly dispose of those SharePoint objects that implement IDisposable when you are finished using them.
    The following unusual behaviors occurred when you did not disposing of SharePoint objects when you are finished with them

    1. Frequent recycles of the Windows SharePoint Services application pool, especially during peak usage.
    2.  Application crashes that appear as heap corruption in the debugger.
    3.  High memory use for Microsoft Internet Information Services (IIS) worker processes.
    4. Poor system and application performance.

    There are 2 articles contains best practice for disposing objects:
    http://msdn.microsoft.com/en-us/library/ee557362.aspx

    http://blogs.msdn.com/b/rogerla/archive/2009/11/30/sharepoint-2007-2010-do-not-dispose-guidance-spdisposecheck.aspx

You can use SharePoint Dispose Checker Tool:
This tool helps developers and administrators check custom SharePoint solutions that use the SharePoint Object Model helping measure against known Microsoft dispose best practices.
http://archive.msdn.microsoft.com/SPDisposeCheck

  • Using SharePoint Data and Objects Efficiently

    1. Caching Data and Objects 
    2. Using Objects in Event Receivers
    3. Working with Folders and Lists
    4. Deleting Multiple Versions of a List Item
    5. Writing Applications That Scale to Large Numbers of Users

    There is article take about Common Coding Issues When Using the SharePoint Object Model:  http://msdn.microsoft.com/en-us/library/bb687949%28v=office.12%29.aspx
  • Decrease round trips with the database:|
    When you access SharePoint using object model every time access . retrieve data from the database for example:
    If we loop over list items like:
    for(int i=0;i<web.list["News"].Items.Count;i++)         
    {
          SPListItem item = web.list["News"].Items[i];
    }

    The best practice assign SharePoint Objects in variables and use these variable:
    SPList list = web.list["News"];
    SPListItemCollection itemCol= list.Items;                       
    for(int i=0;i<itemCol.Count;i++)
    {
          SPListItem item = itemCol[i];
    }
    This role must be applied in most objects of SharePoint.
I found good engine called FASTSPWeb in codeplex help you to solve some problems:
  • Decrease database round trip by caching SharePoint objects automatically in the web application process and allowing multithreaded access to these objects.
  • Correct disposing of opened SPWeb and SPSite object is also not an issue because FASPSPWeb is handling all instantiating and disposing automatically.
This tool link is:
http://fastspweb.codeplex.com/




No comments: