Tuesday, 13 March 2012

The wrong way to iterate through SharePoint SPList Items


Lets start by looking at a code snippet that can be used in a WebPart to access the first 100 items from the SharePoint list of the current context:
SPList activeList = SPContext.Current.List;
for(int i=0;i<100 && i<activeList.Items.Count;i++) {
  SPListItem listItem = activeList.Items[i];
  htmlWriter.Write(listItem["Title"]);
}
Assuming that there are at least 100 items in the list. How many roundtrips to the database is this code going to make in order to retrieve the 100 Title’s of the first 100 SharePoint list items? You might be surprised. Its a total of 200 database calls as you can see from the database view when analyzing the transaction executing the above code:
200 SQL Statements get executed when iterating through SPList.Items
200 SQL Statements get executed when iterating through SPList.Items
The reason for that is because in every loop we request a new SPListItemCollectionobject when accessing the Items property. The Items property is not cached and therefore always requests all items from the database again. Here is how the first loop iterations look like in the PurePath:
Every access to the Items property executes the same SQL statement again
Every access to the Items property executes the same SQL statement again
The CORRECT way to do it
The correct way to do it is of course to store the Items property return value in a SPListItemCollection variable. With this the database is only queried once and we will then iterate over the result set that is stored within the collection object. Here is the changed sample code:
SPListItemCollection items = SPContext.Current.List.Items;
for(int i=0;i<100 && i<items.Count;i++) {
  SPListItem listItem = items[i];
  htmlWriter.Write(listItem["Title"]);
}
Resulting in the following PurePath.
Storing the Items property in a variable elminiates 99.5% of the database calls
Storing the Items property in a variable elminiates 99.5% of the database calls
Conclusion
Many properties in SharePoint return new object instances every time you access them. In order to build good software based on the Microsoft SharePoint Platform its necessary to understand what is going on under the hood. This will eliminate “surprises” once your custom code is first executed with real life data.
There are additional ways to optimize access to data stored in SharePoint lists. I will cover that in my next posts.

Add item to splist

Here is the code snippet:


using(SpSite site=new SPSite("Server"))
{
using(SpWeb web=site.OpenWeb())
{
SpList list=web.Lists["ListName"];
SpListItem item=list.Items.Add();

item["Title"] = "testTitle";
item["Description"]="test"; 
item.Update();
}
}

Enjoy

When should I use SPSecurity.RunWithElevatedPrivileges?

It is very importent to understand why and when to use SPSecurity.RunwithElevatedPrivilages.When we want to retrive or update the data where a normal user don't have permission,there we use this code segment. How this exactly works is like SPSecurity.RunWithElevatedPrivileges runs current process under application pool identity and it allows users to add/update item in list,library or site.


Example 


SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(web.Site.ID))
    {


      // site will be based on the rights for the system account

    }

});


For more details click here

Base class used while creating a webpart

There are two base classes which are going to be consumed by sharepoint can inherit from, either the sharepoint base class or ASP.NET2.0 Webpart base class.When inheriting from sharepoint base class your derived webpart will inherit from Microsoft.Sharepoint.WebPartPages.WebPart base class. When ingeriting from ASP.Net 2.0 webpart base class, your derived webpart will use System.Web.UI.WebControls.WebParts.WebPart . It is preferable to use ASP.NET base class as old base class is meant of backward compatibility  with previous version of sharepoint.However there are four exception when it is better to leaverage functionality from sharepoint webpart base class.
These are the scenarios:

  1. Cross Page connection.
  2. Connection between webpart that are outside webpart zone.
  3. Client side conection.
  4. data-cachieng infrastructure
for any further detail,you can post your query.

Monday, 12 March 2012

Sharepoint 2010 enhancements

Sharepoint 2010 is very dynamic and more user friendly portal.

User interface:Microsoft has worked very hard on this control.This makes easy access to your need on List,Library or pages.For details click here

Developer view: from developer point of view also this has enlarged on very large scale.Like we have more classes,Web Parts and sevices.For more details click here.

Thursday, 15 December 2011

Use the SharePoint 2010 Managed MetaData Service?

In Sharepoint 2007 content type globalization was not possible.Content Type Globalization means we create content type in one site collection and we can use this content type in other site collection just by following small steps.
For detail information look This Video

Pages

Followers