Skip to content

Accessing item data

misonou edited this page Sep 14, 2016 · 3 revisions

The library provides consistent and robust way to access values to list items.

Encapsulate data formats

The default data access APIs is actually type-less in the sense that one has to guess the returned types and try to deserialize oneself with a handful of tricky exceptions.

The different data access adapter classes encapsulate this and let develop write less repetitive error-prone codes and make them more reusable.

The simplest case would be an integer field where you may have to write depending on the source object:

SPListItem listItem = GetListItem();
object value1 = listItem["IntField"]; // Integer: 1
object value2 = listItem["DateField"]; // DateTime: 2016/01/01

DataTable table = GetResultFromSiteQuery();
object value3 = table.Rows[0]["IntField"]; // String: "1"
object value4 = table.Rows[0]["DateField"]; // String: "2016-01-01T00:00:00Z"

For some field types, Taxonomy, the serialization format could be a lot different, especially when querying through standard SSOM and search APIs.

The library solves this by introducing different adapter classes for each type of data sources:

Source SharePoint SSOM Type Data Adapter Type
List query SPListItem SPListItemAdapter
Cross list query DataRow DataRowAdapter
Search result DataRow KeywordQueryResultAdapter
List item event receivers SPItemEventDataCollection SPItemEventDataCollectionAdapter

So the above example you can just write:

SPListItem listItem = GetListItem();
ISPListItemAdapter listItemAdapter = new SPListItemAdapter(listItem);
int value1 = listItemAdapter.GetInteger("IntField");
DateTime? value2 = listItemAdapter.GetDateTime("DateField");

DataTable table = GetResultFromSiteQuery();
ISPListItemAdapter dataRowAdapter = new DataRowAdapter(site, table.Rows[0]);
int value3 = dataRowAdapter.GetInteger("IntField");
DateTime? value4 = dataRowAdapter.GetDateTime("DateField");

Value collections

There are field types that store multiple values like UserMulti and LookupMulti. The adapter classes deserialize the formatted string into an observed collection. Any changes to the collection will cause serialization and write it to the list item.

SPListItem listItem = GetListItem();
ISPListItemAdapter adapter = new SPListItemAdapter(listItem);
IList<SPPrincipal> users = adapter.GetMultiUserFieldValue("AssignedTo"); // Count: 1
users.Add(anotherUser);
Console.Write(listItem["AssignedTo"]); // String: "1;#User1;#2;#AnotherUser"