Click here to Skip to main content
15,868,340 members
Articles / Productivity Apps and Services / Sharepoint / SharePoint 2013
Tip/Trick

Map Your Sharepoint Lists in Strongly Typed Entities

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
5 May 2014CPOL2 min read 23.7K   206   5   6
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:

C#
//
// 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

C#
// 
//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

C#
// 
//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

C#
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.

C#
City city = new City(); 
string caml = city.BuildCamlQuery<Uf>(p => p.Name == "New York");   
This article was originally posted at https://sennitspmapper.codeplex.com/SourceControl/latest

License

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


Written By
Architect Sennit
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLookupField [person ot group] Allow multiple selections Pin
Member 1104224415-Apr-19 4:07
Member 1104224415-Apr-19 4:07 
QuestionAssembly generation failed -- Referenced assembly 'Sennit.SharepointAutoMapper' does not have a strong name Pin
jeff9748-Mar-15 11:49
jeff9748-Mar-15 11:49 
Questionis it usable just for client object model ? Pin
MB Seifollahi12-May-14 9:22
professionalMB Seifollahi12-May-14 9:22 
AnswerRe: is it usable just for client object model ? Pin
Carlos A Bueno14-May-14 14:19
Carlos A Bueno14-May-14 14:19 
AnswerRe: is it usable just for client object model ? Pin
MB Seifollahi14-May-14 19:45
professionalMB Seifollahi14-May-14 19:45 
yes, i developed and published it on my website ( mb-seifollahi.ir[^] )

last night, i published it as a Nuget[^] package too.

Now, i'm working on a Mapper in Javascript (for COM in sharepoint) for managing CRUD action. may be next month, i'll release it (not ready yet).

Thanks for publishing your nice project.

good luck
mbs

GeneralRe: is it usable just for client object model ? Pin
jeff9746-Mar-15 4:47
jeff9746-Mar-15 4:47 

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

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