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.
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.
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("http://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!");
}
}
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.
No comments:
Post a Comment