Through this blog, it is my effort to share experiences with the community and make the journey enjoyable.

Thursday, September 10, 2009

Populating and Creating Business Entities from SharePoint List

Patterns and practices has released a new version of guidance for building collaborative applications.

One of the new functionality provided by the Microsoft group is very useful and generic, w.r.t creating and saving business entities. Concept is based on List based repositories by following below steps -

  1. Business entity class E.g. Customer
  2. Static class that contains the SharePoint field IDs for the list to be used.
  3. Repository class which creates instance of the ListItemFieldMapper class and adds mappings that associate the field IDs to the corresponding property of the business entity

    Please refer - http://msdn.microsoft.com/en-us/library/ee413898.aspx for more details

  4. After business entity and mapping classes are defines, generic functionality could be used to retrieve and save the business entity directly with SharePoint.

    Sample Code -

  • Creating collection for Customer Business Entity:

    public IList<Customer> GetAllCustomers()
    {
    // ...


    using (SPSite site = new SPSite(customerWebUrl))
    {
    using (SPWeb customerWeb = site.OpenWeb())
    {
    SPList customerList = customerWeb.Lists[Constants.CustomerListName];
    CAMLQueryBuilder camlQueryBuilder = new CAMLQueryBuilder();
    camlQueryBuilder.FilterByContentType(CustomerContentTypeName);
    SPListItemCollection items = customerList.GetItems(camlQueryBuilder.Build());

    IList<Customer> customers = new List<Customer>();
    foreach(SPListItem item in items)
    {
    Customer customer = listItemFieldMapper.CreateEntity(item);
    customers.Add(customer);
    }
    return customers;
    }
    }
    }

  • Save a Business Entity to a List

    public void AddCustomer(Customer customer)
    {
    using (SPSite site = new SPSite(customerWebUrl))
    {
    using (SPWeb customerWeb = site.OpenWeb())
    {
    SPList customerList = customerWeb.Lists[Constants.CustomerListName];
    SPListItem customerListItem = customerList.Items.Add();
    this.listItemFieldMapper.FillSPListItemFromEntity( customerListItem, customer);
    customerList.Update();
    }
    }
    }

Share:

Search This Blog

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway. All posts are provided "AS IS" with no warranties, and confers no rights. In addition, my thoughts and opinions often change, and as a weblog is intended to provide a semi-permanent point in time snapshot you should not consider out of date posts to reflect my current thoughts and opinions.

Popular Posts

Total Pageviews

Blogger Tutorials

Blogger Templates

Sample Text