SharePoint 2010 - LINQ and SPMetal

In this article we can explore LINQ to SharePoint and SPMetal, some of the advanced programming areas.

In this article we can explore some of the advanced programming areas:

  • LINQ to SharePoint
  • SPMetal

LINQ to SharePoint is a new feature of SharePoint 2010. LINQ stands for Language Integrated Query which is a part of the .NET Language. The purpose of LINQ is to support various data sources using the same Typed Query Syntax. Presently it supports Objects, Datasets, SQL, Entities, XML etc.

Why we need LINQ?

You might have noted that the previous List Programming examples did not use proper column name access. LINQ allows us to access the List in a Typed manner. Adding more clarity, we can access the list items based on the column names which we usually do with the databases.

Example: var result = from c in Citizen where c.Name == "John" select c;

What is SPMetal?

As we will be creating custom lists having custom column names, we need to generate the Entity Model. The SPMetal.exe is the tool which helps in generating the Model classes.

Although we can create the Model classes manually, it will be a tedious job and prone to error. Using SPMetal would be the right approach to model classes.

Activities

The following are the activities performed in this article:

  1. Manager List Creation
  2. Entity Creation
  3. Read using LINQ
  4. Insert Entity
  5. Update Entity
  6. Delete Entity

Experimenting with LINQ and SPMetal

To begin, create a custom list inheriting from a Custom List and name it Manager. Add the following custom columns and data:

LinqSPM1.jpg

Generating the Entity Models

Now we can generate the Entity Model for the above List.

You can get the SPMetal.exe inside the following folder:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

Open a command prompt and go to the specified folder:

LinqSPM2.jpg

Now run the following command:

SPMetal.exe /web: http://YOURSITE /code: SiteEntities.cs

Wait for a while and you will be ready with the new file. Open the file SiteEntities and you can see the Manager class is contained inside.

LinqSPM3.jpg

Create Application

Create a new SharePoint > 2010 > Console Application (targeting .Net 3.5 framework) and add the SiteEntities.cs file into it.

LinqSPM4.jpg

Add a reference to the following assembly:

LinqSPM5.jpg

You can try with the following operations: Read, Insert, Update and Delete using LINQ to SharePoint.

Selecting an Item

Now we are trying to select the managers with the country "USA":

using (SiteEntitiesDataContext context = new SiteEntitiesDataContext("http://appes-pc"))
{
    var result = context.Manager.Where(m => m.Country == "USA");

    foreach (ManagerItem manager in result)
    {
        Console.WriteLine(manager.Name);
    }
}


Note: You can use LINQ or Lambda Expressions to do the query. In the above example I have used Lambda.

On executing the application you can see the following results:

LinqSPM6.jpg

Inserting an Item

For inserting a new item into the Manager list, use the following code:

using (SiteEntitiesDataContext context = new SiteEntitiesDataContext("http://appes-pc"))
{
    ManagerItem manager = new ManagerItem();
    manager.Name = "New Manager";
    manager.Address = "New Address";
    manager.Country = "New Country";

    context.Manager.InsertOnSubmit(manager);
    context.SubmitChanges();
}


After executing the application, open the Manager list inside SharePoint as shown below:

LinqSPM7.jpg

Updating an Item

For updating an item inside SharePoint use the following code:
 

using (SiteEntitiesDataContext context = new SiteEntitiesDataContext("http://appes-pc"))

{

    ManagerItem manager = context.Manager.Where(m => 

                          string.IsNullOrEmpty(m.Title)).FirstOrDefault();

    if (manager != null)

        manager.Title = "New Title";

 

    context.SubmitChanges();

}

You will see the updated entity inside SharePoint:
 

LinqSPM8.jpg

Deleting an Item

For deleting an item inside SharePoint use the following code:

using (SiteEntitiesDataContext context = new SiteEntitiesDataContext("http://appes-pc"))

{

    ManagerItem manager = context.Manager.Where(m => m.Title.Length > 3).FirstOrDefault();

    if (manager != null)

        context.Manager.DeleteOnSubmit(manager);

 

    context.SubmitChanges();

}

You can see that the item is deleted inside SharePoint:

LinqSPM9.jpg

This concludes our Read, Insert, Update and Delete operations using LINQ to SharePoint. Hope the topics are understood by the reader.

References

http://msdn.microsoft.com/en-us/library/ee535491.aspx
http://msdn.microsoft.com/en-us/library/ee538255.aspx

Summary

In this article we have explored LINQ and SPMetal tool. This information is necessary in real-world programming scenarios. The attachment contains the source code we discussed.