1
votes

I am creating a class library to manage files on sharepoint and I will be using CSOM library for that.

But I am bit confused like how to manage Client Context which is same as DbContext of Entity framework like whether to create it as Singleton object or use and dispose each time it is use.

and Also How do i create a wrapper over ClientContext object just like we create wrapper over DbContext of EF?

1

1 Answers

0
votes

There is nothing like a real Entity Framework in CSOM built-in or even available. Programming with CSOM is more like working with datasets when using pure CSOM. So storing data in SharePoint using CSOM looks like this:

 List courseList= clientContext.Web.Lists.GetByTitle("Course");
    clientContext.Load(courseList);
    clientContext.ExecuteQuery();

    var courseItemInformation = new ListItemCreationInformation();
    ListItem courseItem = courseList.AddItem(courseItemInformation);
    courseItem["Title"] = course.Title;
    courseItem["Coach"] = course.Coach;
    courseItem["Description"] = course.Description;
    courseItem["Audience"] = course.Audience.ToString();            
    courseItem.Update();

    clientContext.ExecuteQuery();
    return courseItem.Id;

There is however the Framework called "AweCsome-Framework" which tries to mimic the behavior of the Entity Framework. The same result above with the aweCsome framework would look like this:

 awecsomeTable = new AweCsomeTable(clientContext);
 return awecsomeTable.InsertItem(course);

In both examples "Course" is a simple Entity.

Be aware that the Awecsome Framework does not allow ALL SharePoint List operations, but standard CRUD does work. It also lacks features like Lazy loading but is the closest you could get to an Entity Framework on SharePoint right now.

You might also want to have a look at PnP (Patterns and practices) which helps in a few standard-operations.

In addition: The ClientContext is a bit more than a DbContext. It is not only about data, it also contains information about the site, the user and permissions. SharePoint simply is no database (and should be be seen as one, especially performance-wise)