Introduction to the SPLinq in SharePoint2010

No.of Views1966
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  11 Nov 2010 08:11:40
Tag : SharePoint , Development and Programming
In this article, we will see how to work with LINQ to SharePoint. I have tried to address all the common errors we encounter when first time we start using LINQ against SharePoint.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

In this article, we will see how to work with LINQ to SharePoint.   I have tried to address all the common errors we encounter when first time we start using LINQ against SharePoint.

Assumption

We have a custom list

1.    Name of the list is Test_Product.
2.    Columns of  the list is as below , 

Image Loading

3.    There are two items in the list

Image Loading

4.    URL of the SharePoint  site is   

http://dhananjay-pc/my/personal/Test1

Now we need to fetch the list items in a managed console application using Linq to Sharepoint or SPLinq. Follow the below steps,

Step1

Open the command prompt and change directory to

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

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

Image Loading

Step2

Now we need to create the class for corresponding list definitions.

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN> spme
tal.exe /web:http://dhananjay-pc/my/personal/Test1 /namespace:nwind /code:Product.cs

In above command we are passing few parameters to spmetal.exe, they are as below

1.    /web:Url   
Here we need to provide URL of SharePoint site
/web:http://dhananjay-pc/my/personal/Test1 /

://dhananjay-pc/my/personal/Test1 / is  URL of SharePoint site, I created for myself. You need to provide your SharePoint site URL here.

2.    /namespace:nwind

This would be the namespace under which class of the list will get created. In my case name of the namespace would be nwind.

3.    /code:Product.cs
This is the file name of the generated class.  Since we are giving name Product for the file then class generated will be ProductDataContext

Step3

Open visual studio and create a new project of type console.  Right click on the Project and select Properties

Image Loading

Click on the Build tab and change Platform Target to Any CPU.

Image Loading

Click on the Application tab and change the Target framework type to .Net Framework 3.5

Image Loading

Step4

The class we created in Step2 will by default get saved in the same folder with SPMetal.exe. So to see where the class got created we need to navigate to folder
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

Add created class to the Project
Now add this class to the project. To do this, right click on the project and select Add existing item.  Then browse to above path and select Product.cs
 
Add references to the Project

Microsoft.SharePoint 
Microsoft.SharePoint.Linq 

Right click on Reference and select Add Reference. To locate Microsoft.SharePoint and Microsoft.SharePoint.Linq dll browse to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI. All the SharePoint dll are here in this location.

Step5

Add the namespace 

Image Loading

Nwind is the name of the namespace of the class we created in Step2.

Step6

Now we will write the code to access the SharePoint list using Linq to SharePoint.

a.    First we need to create instance of ProductContext class

Image Loading

Here we need to provide URL of the SharePoint site as parameter to constructor.

b.    Now we can apply simple LINQ to access the list, like below. 

Image Loading

c.    There is one more way to access the list

Image Loading

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;
using nwind;

namespace SPLinqTest1
{class Program
    {static void Main(string[] args)
        {
            
            ProductDataContext context = new ProductDataContext("http://dhananjay-pc/my/personal/Test1");//EntityList<Test1_ProductItem> products = context.GetList<Test1_ProductItem>("Test1_Product");var res = from r in context.Test1_Product   select r;foreach (var r in res)
            {
                Console.WriteLine(r.ProductId + ":" + r.ProductName + ":" + r.ProductPrice);
            }
            Console.ReadKey(true);
        }
    }
}

 Output 

Image Loading

Conclusion

In this article, we learned how to work with LINQ to SharePoint.   I have tried to address all the common errors we encounter when first time we start using LINQ against SharePoint.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top