articles

home / developersection / articles / using linq to access sharepoint list data

Using LINQ to access SharePoint list Data

Chris Anderson 13402 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 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

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 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.

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