Translate

Sunday, September 16, 2012

SPList.GetItemById and SPList.GetItemByIdSelectedFields

Using SPList.GetItemById and SPList.GetItemByIdSelectedFields

Difference between SPList.GetItemById and SPList.GetItemByIdSelectedFields


It is very important to consider about the performance when we are query the SPList using SharePoint Object Model, it is not recommended to fetch all items when you are using only one column SharePoint has provided the GetItemByIdSelectedFields which takes the arguments as ID and Field Names which is required.

SPList.GetItemByIdSelectedFields(ID,"Field 1","Field 2","Field3"... so on)

GetItemById method fetches the full item from SPListItemCollection, with all its columns of metadata,even though it may be not required for the any purpose of extra columns still SPListItem hold that values too. So it is required to use in more better way of both GetItemByIdSelectedFields and GetItemById based on the requirements

The following example shows the Employee List which is being query by using GetItemByIdSelectedFields

Let us say the Employee List has three columns

Employee List

Column Name
Type
Employee Number
Number
Employee Salary
Number
Employee Designation
Single Line Of Text


ID
Employee Number
Employee Salary
Employee Designation
1
1123
24000
Senior Software Engineer
2
1124
12000
Trainee
3
1125
50000
Project Manager

The following code fetches

using (SPSite site = new SPSite("http://sampledemo /"))
{
using (SPWeb web = site.OpenWeb()) {
SPList employeeList = web.Lists["Employee List"];
SPListItem  employeeItem= employeeList.GetItemByIdSelectedFields (2,” Employee Salary”);
}
}

No comments:

Post a Comment