How to use Language Integrated Query (LINQ) in SharePoint 2013/2010

This SharePoint 2010 tutorial, we will discuss, how to use LINQ in SharePoint 2013 or SharePoint 2010 to insert items to list. By using LINQ we can access add and read items into a SharePoint list. LINQ to SharePoint, it can query SharePoint data with a fully-typed approach. LINQ to SharePoint targets to SharePoint data.

LINQ in SharePoint 2013/2010

SharePoint uses SPMetal.exe to generate entities class. SPMetal presents in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN directory.

To use LINQ to SharePoint we need to give reference to Microsoft.SharePoint.Linq and then we have to generate entity classes by using a command-line tool named SPMetal (SPMetal.exe).

SPMetal takes a lot of arguments like
/Web:{URL} , /useremoteapi , /code:{file name} , /language:{language name like csharp or vb} , /namespace etc.

By default SPMetal.exe generates code to the console. But if you want to generate code to a file then

SPMETAL.EXE /web:{URL} /code:codefilename.cs

Here is an example to add an item to a SharePoint Foundation list using LINQ. (Followed from MSDN)

DataContext data = new DataContext(SPContext.Current.Web.Url);
EntityList<Customer> Customers = data.GetList<Customer>(“Customers”);
//Customers is the custon list name.
Customer newCustomer = new Customer() { CustomerId=2, City=”enjoysharepoint” };
Customers.InsertOnSubmit(newCustomer);
data.SubmitChanges();

LINQ to SharePoint 2010 will have all the support of IntelliSense.

You may like following SharePoint tutorials:

Hope this SharePoint tutorial explain, How to use Language Integrated Query (LINQ) in SharePoint 2013 or SharePoint 2010?

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>