.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

LINQ to SharePoint 2010

Posted By:Sunil Yadav       Posted Date: September 23, 2010    Points: 50    Category: SharePoint    URL: http://www.dotnetspark.com  

In SharePoint 2007 the developer used to write a CAML (Collaborative Markup Language) query for querying SharePoint lists. Now querying SharePoint list is easy with SharePoint 2010 integration with Linq. SharePoint 2010 lets you work with SharePoint list.

In SharePoint 2007 the developer used to write a CAML (Collaborative Markup Language) query for querying SharePoint lists. Now querying SharePoint list is easy with SharePoint 2010 integration with Linq.  SharePoint 2010 lets you work with SharePoint list.

Displaying List Data using Linq

Here will create a Visual WebPart which displays the data in Grid View from SharePoint list.

Create a Visual WebPart

Open Visual studio 2010 and navigate to File->New->Project->SharePoint->2010->Visual Web Part

Refer to my blog for Creating Visual WebPart in SharePoint 2010.

Add WebPart SharepointLinqUserControl and add the following code in the SharepointLinqUserControl.ascx

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
    Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SharepointLinqUserControl.ascx.cs"
    Inherits="SharepointLinqDemo.SharepointLinq.SharepointLinqUserControl" %>
<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>



using System;
using System.Web.UI;
using Microsoft.SharePoint.Linq;
using System.Linq;
namespace SharepointLinqDemo.SharepointLinq
    public partial class SharepointLinqUserControl : UserControl
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                DataContext dc = new DataContext("http://mossportal");

                // Display data where % complete is 100
                var task = dc.GetList("Tasks").Where(p => p.Complete == 1);
                GridView1.DataSource = task;


Build and publish the WebPart to the site.

In the above code DataContext class provides Linq access to SharePoint list and document libraries.

SPMetal tool can also be used to generate entity classes. The classes generated from the tool provides object oriented interface to the content database and are used in querying SharePoint data using Linq.

Syntax to generate entity class is give below.

PS C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin>.\SPMetal  /web:http://mossportal  /code:LinqDemo.cs /language:csharp

Consider below image to generate entity class.

For more info: http://msdn.microsoft.com/en-us/library/ee538255.aspx

Adding, Modifying and Deleting list item using Linq.

Use the entity generated by SPMetal.EXE

LinqDemoDataContext dc = new LinqDemoDataContext("http://mossportal");

Add List Item

Following code shows how we can add item in SharePoint list using linq.
Task mytask = new Task()
Title = "Querying Sharepoint list data using linq.",
AssignedTo = @"mossportal\sunil",
StartDate = DateTime.Now,
DueDate = DateTime.Now.AddDays(20),
TaskStatus = TaskStatus.NotStarted

Update List item

var uptask = dc.Tasks.SingleOrDefault(p => p.Title == "Querying Sharepoint list data using linq.");

if (uptask != null)
    uptask.Title = "Querying Sharepoint list data using linq updated.";

Delete List item

foreach (Task item in dc.Tasks)
    if (item.Title == "Querying Sharepoint list data using linq.")


Happy SharePointing!!!!!

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend