Click here to Skip to main content
Click here to Skip to main content
Go to top

Map Your Sharepoint Lists in Strongly Typed Entities

, 5 May 2014
Rate this:
Please Sign up or sign in to vote.
Map your Sharepoint lists in Strongly Typed Entities

Introduction

In this tip, we will learn how to convert our Sharepoint lists to Entities that are strongly typed, so it will use the Framework Sennit Sharepoint AutoMapper.

To download Sharepoint AutoMapper, please visit the project on CodePlex or the nuget.

Sennit Sharepoint Automapper

Sennit Sharepoint AutoMapper is in the initial stage, but it is possible to design ListItemCollection Entities previously configured in the future can automatically map a ListItemCollection to an Entity.

In our sources, we provide an example of usage and are working to create a good documentation for using the Framework. In this example, we use another framework (http://ssoh.codeplex.com) for connection with our App Sharepoint Online in an easy way.

Create Your Entity

We will create our entity using Sharepoint Sennit AutoMapper. With it, we just need to implement IEntitySharepointMapper interface and map them utilizing some attributes of this framework.

The SharepointListName attribute maps the name of the list in SharePoint. If you do not put this attribute, it uses the entity name as the name of the list.

We also have the SharepointFieldName and IgnorePropertyInSharepoint attributes we use in this example, the first informs the property name in Sharepoint list and the second is to not try to map the attribute to the list, if not define any attributes Mapper will use the name attribute in the entity to map to the list.

Below is an example of a mapped class:

//
// Creating your mapped entity with Sharepoint Automapper, very easy

[SharepointListName("CityList")]
public class City : IEntitySharepointMapper
{
        [SharepointFieldName("ID")] // Name of the 
        public Int32? Id { get; set; }

        [SharepointFieldName("Title")]
        public String Name { get; set; }

        [IgnorePropertyInSharepoint]
        public String InternalId { get; set; }            

        public LookupFieldMapper State { get; set; }
}  

How To Convert Your Mapped List in Entity

After we map our body can use some extensions provided by the framework that allows us to convert our entity list and vice versa.

We will show three forms of conversion using Sharepoint Mapper:

  1. ListitemCollection to the Entity List
  2. ListItemCollction for Entity
  3. Entity to ListItem

1. ListitemCollection to the Entity List

// 
//ListItemCollection any object can send your data to a mapped entity equal to the example.

ListItemCollection listCollection;
List<City> litofCities = listCollection.ProjectToListEntity<City>();  

2. ListItemCollection for Entity

// 
//ListItemCollection any object can send your data to a mapped entity equal to the example.

ListItemCollection listCollection;
City city = listCollection.ProjectToEntity<City>();  

3. Entity to ListItem

Web web = context.Web;

                
List lista = web.Lists.GetByTitle(entity.GetSharepointListName());
ListItemCreationInformation itemCreationInfo = new ListItemCreationInformation();
ListItem item = lista.AddItem(itemCreationInfo);

//Project your data from entity to ListItem, very simple :)
item.ProjectListItemFromEntity<T>(entity);

item.Update();
context.Load<List>(lista);
context.Load<Microsoft.SharePoint.Client.ListItem>(item);
context.ExecuteQuery(); 

Lambda Expression to Caml Query

Something that is extremely difficult to be created in Sharepoint queries are queries with the framework. This task becomes extremely simple, we just need to create our query using the lambda method BuildCamlQuery that it automatically converts the lambda expression for Caml.

City city = new City(); 
string caml = city.BuildCamlQuery<Uf>(p => p.Name == "New York");   

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Carlos A Bueno
Architect Sennit
Brazil Brazil
No Biography provided
Follow on   LinkedIn

Comments and Discussions

 
Questionis it usable just for client object model ? PinmemberMBSMBS12-May-14 9:22 
AnswerRe: is it usable just for client object model ? PinmemberCarlos A Bueno14-May-14 14:19 
AnswerRe: is it usable just for client object model ? PinmemberMBSMBS14-May-14 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 6 May 2014
Article Copyright 2014 by Carlos A Bueno
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid