LINQ to SharePoint 2010
Wow! LINQ to SharePoint 2010 is awesome! You have to work with it to see how it simplifies development tasks. I especially love the ability to see what underlying CAML that’s generated on the fly and passed to the server for execution. With LINQ to SharePoint, I believe, we need to have our business logic in place and know how to design “efficient queries” to get our job done! No more worries about missing a “ or confusion as to whether it’s ‘ or “ and so on…
So, I worked on a simple scenario very recently. And I used LINQ to SharePoint to achieve that task. The scenario is I have 2 lists. One of those list looks up the other list along with a few “project fields” (new in SharePoint 2010). I had to write a sample to extract data from these joined lists and to add new items to the list that looks up the other list. Confusing?? Well, read the above again and it might be clearer :)
This is my “Courses” list
This is my “Students” list
The students list uses the course title from courses list and also uses duration & credit points as projected fields. A quick screenshot on how to setup projected fields below.
First step is to generate the entity class file for the site using SPMetal.exe (should be located at: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN).
Add it to the VS2010 project and below is the code that helps achieving this task.
static void Main(string[] args)
{
using (EntityDataContext dataContext =
new EntityDataContext("https://spdevdemo"))
{
var query = from c in dataContext.Students
orderby c.Course.Title
where c.Course.Duration.Value.Equals(6)
select new
{
StudentName = c.Title,
Course = c.Course.Title,
Duration = c.Course.Duration,
CreditPoints = c.Course.CreditPoints
};
// what's the equivalent CAML for this???
dataContext.Log = Console.Out;
foreach (var student in query)
Console.WriteLine("{0} - {1} - {2} - {3}",
student.StudentName, student.Course, student.Duration, student.CreditPoints);
// adding sample
// first we create an instance of coursesitem in our entity file
// and assign values we need to be present in the lookup field
// there obviously could be a better way to do this instead of hardcoding values
// will this post a little later on how that's done
CoursesItem newCourseItem = new CoursesItem()
{
Title = "History",
CreditPoints = 100,
Duration = 4,
Id = 7
};
// first attach this object to the data context
dataContext.Courses.Attach(newCourseItem);
// now create an instance of studentsitem
StudentsItem newItem = new StudentsItem()
{
Title = "Student101",
Course = newCourseItem
};
// Call InsertOnSubmit() and then SubmitChanges() to commit
dataContext.Students.InsertOnSubmit(newItem);
dataContext.SubmitChanges();
Console.WriteLine("Wrote new record successfully!");
}
}
The output of this code below:
Notice the CAML dump which fires in the background. Obviously, we can appreciate the power of LINQ to SharePoint 2010 after seeing the huge CAML block that we’ll need to otherwise construct and do this simple task. I personally feel LINQ to SharePoint 2010 will be highly leveraged by developers because of its ease of use and enhanced developer productivity. Hope this post was useful and stay tuned for more.
Comments
Anonymous
February 09, 2010
I am trying to use SPMetal. When I enter the command and run it I keep getting Object Reference Not Set to an instance of an Object". I do not know how to get past this?Anonymous
August 12, 2011
Hi sridhar I am having a custom document library named 'Product' and I am having custom content type named 'ProductSet' and this content type had been set as the default content type for the 'Product' document library . When I produced Linq to SP entities using SPMetal.exe it does not generate me any class named 'ProductProductSet' , so that I can query through the document library (Product)to get the document sets of content type (Product Set). Can you give me your suggestion on how to query only the document sets (of a certain content type) that are present inside a custom document libraryAnonymous
February 21, 2012
Should i get all version of list item using LINQ? if yes then how ?Anonymous
October 28, 2013
@Oscar Bautista - run the command prompt with a userid having RW access to the content DB of your app. Then run SPMetal in it..Anonymous
October 28, 2013
@Oscar Bautista - run the command prompt with a userid having RW access to the content DB of your app. Then run SPMetal in it.. (it was me who commented the same thing earlier, anonymously :P)