Using the LINQ to SharePoint provider is a way to add and read items from a Microsoft SharePoint 2010 list. In SharePoint 2010 we can also use LINQ syntax to fetch items from your lists instead of using the SPSiteDataQuery and SPQuery objects.
In this article I will give you a brief introduction to how you can get started using LINQ queries in SharePoint, also known as LINQ to SharePoint.
In order to work with LINQ in SharePoint 2010, we need use a tool called SPMetal.exe which resides in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN folder.
Using the tool called SPMetal, we generate our entity-classes that are needed to perform these object oriented queries toward our SharePoint server.
Open a cmd-window and navigate to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin
Run the following command in command prompt:
Syntax:SPMetal.exe /web:http://sharepointsiteaddress /code:d:\YourEntityFile.cs
- Open Visual Studio 2010.
- Go to File à New à Project.
- Select Console Application from the installed templates.
- Right click on the solution, select "Add an existing item".
- Add the MyEntities.cs class to the solution.
· Add References by right click on the Reference option:
· Choose Browse:
· Go to the following location: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI.
· Add Microsoft.SharePoint.dll and Microsoft.SharePoint.Linq.dll in Reference.
I have a following Products list in the SharePoint Server.
Add the following code in Program.cs file to perform DML operation in SharePoint list:
using System;
using System.Linq;
namespace LinqinSP
{
class Program
{
static void Main(string[] args)
{
using (MyEntitiesDataContext myEntitiesDataContext = new
MyEntitiesDataContext("http://rohit:34143/"))
{
var listItems = from items in myEntitiesDataContext.Products
where items.Title.StartsWith("H")
select new { items.Title };
foreach (var item in listItems)
{
Console.WriteLine(item.Title.ToString());
}
}
}
}
}
Thanks for reading this article. I think this will help you a lot.
Leave Comment