articles

Home / DeveloperSection / Articles / Using LINQ to access SharePoint list Data

Using LINQ to access SharePoint list Data

Chris Anderson13303 09-Dec-2011

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 asLINQ to SharePoint.

In order to work with LINQ in SharePoint 2010, we need use a tool calledSPMetal.exewhich resides in theC:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BINfolder.
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 toC:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin

Using LINQ to access SharePoint list Data

Run the following command in command prompt:

Syntax:SPMetal.exe /web:http://sharepointsiteaddress /code:d:\YourEntityFile.cs

Using LINQ to access SharePoint list Data

  • Open Visual Studio 2010.
  • Go to File Ã  New Ã  Project.
  • Select Console Application from the installed templates.

Using LINQ to access SharePoint list Data

  • Right click on the solution, select "Add an existing item".
  • Add the MyEntities.cs class to the solution.

Using LINQ to access SharePoint list Data

·        Add References by right click on theReference 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.

Using LINQ to access SharePoint list Data

I have a following Products list in the SharePoint Server.

Using LINQ to access SharePoint list Data

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());
                }
            }
        }
    }
}

 Output:

Using LINQ to access SharePoint list Data

Thanks for reading this article. I think this will help you a lot.


Updated 04-Mar-2020
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By