Saving a Business Entity to a List

Typical Goals

In this scenario, you want to use a repository to update a list item. A business entity is the source of the update.

Solution

In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SPG.Common.dll. Use the FillSPListItemFromEntity method of the ListItemFieldMapper<T> class to perform the conversion.

Saving a Business Entity to a List

When developing your repository, remember that you may need to save information from the business entity back to the SharePoint list as users update and add business information. The following code demonstrates how the SubSiteCreationRequestsRepository class implements an AddSubSiteCreationRequest method. This method adds a new list item that is based on the business entity SubSiteCreationRequest.

public void AddSubSiteCreationRequest(SubSiteCreationRequest request)
{

     // ...

   string adminWebUrl = // ...
   using (SPSite site = new SPSite(adminWebUrl))
   {
      using (SPWeb adminWeb = site.OpenWeb())
      {
          SPList subSiteCreationRequests = 
             adminWeb.Lists[Constants.SubSiteRequestsListName];
          SPListItem subSiteCreationRequest = subSiteCreationRequests.Items.Add();
          this.listItemFieldMapper.FillSPListItemFromEntity(
                                  subSiteCreationRequest, request);
          subSiteCreationRequest.Update();
      }
   }
}

For implementation details, see SubSiteCreationRequestsRepository.cs in the Microsoft.Practices.SPG.SubSiteCreation\SubSiteCreationRequests directory.

Home page on MSDN | Community site