Populating a Business Entity from a List Item

Typical Goals

In this scenario, you want to convert the results of queried list items into one or more strongly-typed business entity objects. This scenario is possible after you create a repository and establish field mappings between a SharePoint list and a business entity class.

Solution

In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SPG.Common.dll. Use the CreateEntity method of the ListItemFieldMapper<T> class to create and populate the entity from a list item.

Populating a Business Entity from a List Item

The GetBusinessEventTypeConfiguration method demonstrates how to use the CreateEntity method. For more information about the GetBusinessEventTypeConfiguration method, see Creating a Repository.

public BusinessEventTypeConfiguration GetBusinessEventTypeConfiguration(
                                                                string businessEvent)
{
  // ...

  SPListItemCollection items = 
              businessEventSiteTemplateList.GetItems(camlQueryBuilder.Build());

  if (items.Count > 0)
  {
     return this.listItemFieldMapper.CreateEntity(items[0]);
  }
     else
  {
     throw new SubSiteCreationException(string.Format(CultureInfo.CurrentCulture, ConfigDataNotFoundMessage, businessEvent));
  }
}     

Usage Notes

To see how the mapper is configured, see Creating a Repository. The result is a new business entity instance whose properties have been set to contain the values of the mapped fields from the list item. The list item is the argument to the CreateEntity method.

Home page on MSDN | Community site