Walkthrough on using LINQ to SharePoint from within a SharePoint 2010 custom visual web part

Objective

In this article we will see,

1. How to create custom WebPart.

2. How to use SPGridView on visual WebPart.

3. How to use LINQ to SharePoint

4. How to deploy the WebPart in SharePoint site.

Step 1

Open Visual studio 2010 and create new project. From SharePoint 2010 project template select Visual Web Part project.

clip_image002

Step 2

Provide the SharePoint site URL, where you want to debug and deploy the Visual Web Part.

clip_image004

Before clicking on Finish, click on Validate button to validate whether you are able to successfully connect SharePoint site or not.

clip_image005

Step 3

Create DataContext class or LINQ to SharePoint proxy class.

Open the command prompt and change directory to

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

Type command CD C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN

clip_image007

Now we need to create the context class for corresponding list definitions.

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN> spme

tal.exe /web:http://dhananjay-pc/my/personal/Test1 /namespace:nwind /code:Product.cs

In above command we are passing few parameters to spmetal.exe, they are as below

a. /web:Url

Here we need to provide URL of SharePoint site

/web:http://dhananjay-pc/my/personal/Test1 /

://dhananjay-pc/my/personal/Test1 / is URL of SharePoint site, I created for myself. You need to provide your SharePoint site URL here.

b. /namespace:nwind

This would be the namespace under which class of the list will get created. In my case name of the namespace would be nwind.

c. /code:Product.cs

This is the file name of the generated class. Since we are giving name Product for the file then class generated will be ProductDataContext

Step 4

Add created DataContext class to the Project or LINQ to SharePoint proxy class.

Now add this class to the project. To do this, right click on the visual web part project and select Add existing item. Then browse to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN and select Product.cs

Add references to the Project

Microsoft.SharePoint

Microsoft.SharePoint.Linq

Right click on Reference and select Add Reference. To locate Microsoft.SharePoint and Microsoft.SharePoint.Linq dll browse to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI. All the SharePoint dll are here in this location.

clip_image008

Step 5

Add SPGridView on the Web Part

Now open the Visual Web Part project and open VisualWebPart1Usercontrol.ascx

clip_image009

Add the below markup to create one SPGridView

clip_image011

In markup in DataField we need to provide column name of SharePoint list to bind the list items to SPGridView column

VisualWebPart1UserControl.ascx

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="VisualWebPartProject1.VisualWebPart1.VisualWebPart1UserControl" %>
<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<SharePoint:SPGridView ID="sp1" runat="server" AutoGenerateColumns="false" >
<HeaderStyle HorizontalAlign="Left" ForeColor="Blue" Font-Bold="true" />
<Columns>
<SharePoint:SPBoundField DataField="ProductId" HeaderText="Product ID" ></SharePoint:SPBoundField>
<SharePoint:SPBoundField DataField="ProductName" HeaderText="Product Name" ></SharePoint:SPBoundField>
<SharePoint:SPBoundField DataField="ProductPrice" HeaderText="Product Price" ></SharePoint:SPBoundField>
</Columns>
</SharePoint:SPGridView>

Step 6

Now write code behind for SPGridView

Add the namespaces

clip_image012

Write the below code to fetch the List items from SharePoint list using LINQ to SharePoint

VisualWebPart1UserControl.ascx.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Linq;
using nwind;
using System.Linq;

namespace VisualWebPartProject1.VisualWebPart1
{
 public partial class VisualWebPart1UserControl : UserControl
 {
 protected void Page_Load(object sender, EventArgs e)
 {

ProductDataContext context = new ProductDataContext(SPContext.Current.Web.Url);
 EntityList<Test1_ProductItem> products = context.GetList<Test1_ProductItem>("Test1_Product");
 var result = from r in products select r;
 sp1.DataSource = result;
 sp1.DataBind();
 }
 }
}

In above code Test1_Product is name of the SharePoint list.

Step 7

Deploy the WebPart to SharePoint site. For that we need to right click on the Visual Web Part project and click Deploy.

clip_image013

Step 8

Open the SharePoint site where you have deployed the visual web part and select the Edit page option .

After that select Insert

clip_image015

After clicking Insert select WebPart to insert

clip_image017

From the custom category select visual web Part.

clip_image018

Note: Name of the project we created in first step. Name I gave was VisualWebPart1 . In custom category you will find name you gave of the project in step1.

After selecting the WebPart click on Add button. And after clicking button on the home page you will get the custom WebPart populated with Test_Product list items.

clip_image020

8 responses to “Walkthrough on using LINQ to SharePoint from within a SharePoint 2010 custom visual web part”

  1. Thank you for this helpful article but i have a prpblem ,, when i used the command “C:\Users\Administrator>”C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\SPMETAL.EXE” /web:http: /namespace:SPLinqTest /code:SPLinqTest.cs” it worked fine and i was able to view the .cs file and then i deleted it accedentaly ,,, so i wanted to generate it again by using the same command but ooops i get this hell msg “C:\Users\Administrator>”C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\SPMETAL.EXE is not a valid Win32 Application” ,, so please if you could help?????

  2. Islam,

    I had the same problem, turns out that the SPMetal.exe file was 0Kb on my server, therefore I looked at another install I had and found SPMetal with 146Kb. I copied the file to my server and everything worked.

  3. Andrew,
    Thank you for your answer , that was actually what I found but I don’t know why this file gone to a ZERO byte :S, anyway that solution realy worked.

  4. Dhananjay Kumar

    SO SPMetal.Exe reinstallation worked :):) nice you people got the solution 🙂

  5. hi ,thank you for this tutorial
    but i have Problem
    in this line of Code
    EntityList products = context.GetList(“Test1_Product”);
    it ive me Blue line Under Test1_ProductItem
    Please Help Me Thank You
    i wait for your reply

  6. again i’m sorry i will rewrite my problem
    first of all in cmd i wrote this line of code
    spmetal.exe /web:http://win-g1cnadmkv5s/ /namespace:nwind /code:Product.cs
    and added Product.cs To my project
    and this is the code
    ProductDataContext context = new ProductDataContext(SPContext.Current.Web.Url);
    EntityList products = context.GetList(“Test1_Product”);
    var result = from r in products select r;
    sp1.DataSource = result;
    sp1.DataBind();
    it give me Red Line Under this
    EntityList products = context.GetList(“Test1_Product”);
    in this Test1_ProductItem
    and this
    var result = from r in products select r;
    under select i have another red line
    thank you

  7. Dhananjay Kumar

    Hi ,

    Test1_Product is name of the SharePoint list so please replace here with name of SP LIst you are working on

  8. thank you very much
    what i was needed adding new list first Called Product
    and add columes to this list
    so it working now
    thank you
    great site…………….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com