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:
[SharepointListName("CityList")]
public class City : IEntitySharepointMapper
{
[SharepointFieldName("ID")]
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:
ListitemCollection
to the Entity
List ListItemCollction
for Entity
Entity
to ListItem
1. ListitemCollection to the Entity List
ListItemCollection listCollection;
List<City> litofCities = listCollection.ProjectToListEntity<City>();
2. ListItemCollection for Entity
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);
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");