Retrieve Selected Fields using getitembyidselectedfields in SharePoint 2016/2013/2010

This SharePoint tutorial will discuss, how to get the value of selected fields from SharePoint list using the SharePoint server object model. Here we will use the getitembyidselectedfields method of SPList in SharePoint.

getitembyidselectedfields SharePoint

getitembyidselectedfields method presented in SPList class which is presented in Microsoft.SharePoint.dll.

This method gets the list item with the specified identifier (ID) and includes only the values for the specified fields.

GetItemByIdSelectedFields(Int32, String[]) method took two parameters, the first parameter as Int32 which is the ID number of the item and 2nd parameter is an array of the internal names of the fields from the item.

Retrieve Selected Fields using getitembyidselectedfields in SharePoint

Below is an example, where we will retrieve selected fields from a list item using the getitembyidselectedfields method using the SharePoint server object model code.

using (SPSite site = new SPSite("http://SiteURL"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.GetList("Employees");
SPListItem item = list.GetItemByIdSelectedFields(5, "Name", "Age", "Department");
String employeeName = (String)item["Name"];
Int employeeAge = Convert.ToInt32( item["Age"]);
string employeeDept = (String)item["Department"];
}
}

Each time an item is added to a SharePoint list, it is given a 1-based integer ID that is one greater than the ID of the previous item added. When an item is deleted from a list, its ID number is not reused.

Here the list item whose id is 5 will be retrieved but only 3 defined fields (Name, Age, Department).

Retrieve all the fields:

If you want all the fields of the SharePoint list item, use the GetItemByIdAllFields(Int32) method. Its only parameter is the item ID.

SPListItem item = list.GetItemByIdAllFields(5);

Here it will retrieve all the fields whose ID is 5.

You may like following SharePoint server object model tutorials:

This SharePoint tutorial, we learned how to use getitembyidselectedfields method in SharePoint 2010/2013/2016. Also, we saw how to get selected fields from a SharePoint list using the SharePoint server object model code.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>