Advertisment

SharePoint 2010: Visual Web Parts and LINQ

author-image
PCQ Bureau
New Update

Web parts are one of the most important features of SharePoint. In SharePoint

2010 along with Visual Studio 2010, you get the ability to create new webparts

visually rather than completely in code. We will take a look at doing this in

this article. We will also explore the new LINQ to SharePoint capability —

allowing developers to query SharePoint data using Language Integrated Query

rather than writing failry obtuse CAML code.

Advertisment

Visual Web Parts



Visual Web Parts are a project type in Visual Studio 2010 that allow you to

create Web parts quickly and easily. Open up VS2010 and create a new “Visual Web

Part” solution. When prompted, enter the URL of the SharePoint site to which

this Web Part will belong. Once the project is created, expand the

“VisualWebPart1” element in Solution Explorer. Open the VisualWebPart1.webpart

file. Change the Title property to “My LINQ Web Part” or something appropriate.

Now open up the VisualWebPart1.ascx file, add some appropriate SharePoint or

ASP.NET controls in it, add some code behind in the .cs file and compile the

project. For instance, I added a SharePoint DateTimeControl and a label and

added some code to display the selected date in the label every time.

Direct Hit!

Applies To: Web Developers



USP:
Learn about new features in the upcoming SharePoint release

Primary Link:



http://sharepoint2010.microsoft.com




Search Engine Keywords: SharePoint 2010

Right click the solution in VS2010 and select “Deploy”. This will build the

solution and deploy it to the SharePoint site you had configured in the

beginning. Browse over to this site ,and on a page click the “Edit page” button

on the Ribbon. Select your Webpart from the “Custom” folder and click the Add

button. You will see the Web part showing up on the page. Simply save the page

and then interact with your Web part. It should work just fine.

Advertisment

For instance in my Web Part, the .ascx file looks like this:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"

%>

<%@ Assembly Name="Microsoft.Web.CommandUI,

Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Advertisment

<%@ 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" %>

Advertisment

<%@ 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="LinqVWP.VisualWeb

Part1.VisualWebPart1UserControl" %>

Advertisment

ID="DateTimeControl1" runat="server" OnDateChanged="Date_Changed" AutoPostBack="true"

/>

Text="Label">asp:Label>

Advertisment

The last two lines are the ones that specify the SharePoint

date time control and a label. The code behind for this looks like this:

protected void Page_Load(object sender, EventArgs e)

{ DateTime dt = DateTime.Now;

Advertisment

if (!String.IsNullOrEmpty(Request.QueryString<"date">))

dt =

DateTime.Parse(Request.QueryString<"date">);

DateTimeControl1.SelectedDate = dt;

}

protected void Date_Changed(object

sender, EventArgs e)

{

Label1.Text =

DateTimeControl1.SelectedDate.ToString();

}

When this WebPart is run, it displays the selected Date and

Time in the label.

LINQ To SharePoint



This is a new feature in SharePoint 2010 that allows developers to query

data within SharePoint “elements” such as Lists and Libraries and manipulate

them using the easy to learn and use LINQ. If you know LINQ, it becomes

extremely easy to work with this. To try this out, first create a simple List —

such as a Task list in the site where the above Web part was created. Now you

need to create the LINQ to SharePoint classes to be used in the WebPart. For

this open up a command prompt and type in the following commands:

set path=%path%;c:\program

files\common files\microsoft shared\web server extensions\14\bin

spmetal.exe /web:http://Win08R2/Demo02

/namespace:LinqVWP.VisualWebPart1 /code:SPLinq.cs

An example of a simple VWP where the selected date and time

is being displayed.

These commands will create a SPLinq.cs file that contains

the entities for the objects in the site. Back in VS2010, add this file into the

visual web part project created above. Open the VisualWebPart1.ascx file and

replace the date control and the label with the following:

<%@ Import Namespace="Microsoft.SharePoint.WebControls"

%>

runat="server" AutoGenerateColumns="false">

ForeColor="Navy" Font-Bold="true" />

HeaderText="Title">SharePoint:SPBoundField>

HeaderText="Starting">SharePoint:SPBoundField>

HeaderText="Description">SharePoint:SPBoundField>

Columns>

SharePoint:SPGridView>

This adds a SharePoint data grid to the webpart. Now in the

code behind replace the existing code with this:

SPLinqDataContext dc = new

SPLinqDataContext(SPContext. Current.Web.Url);

EntityList Tasks = dc.GetList("Tasks");

var tQuery = from task in Tasks

select new

{

task.Title,

task.StartDate,

task.Body

};

spGridView.DataSource = tQuery;

spGridView.DataBind();

A custom visual Web part using LINQ to SharePoint to

display data.

You will need to add reference to the

Microsoft.SharePoint.Linq  assembly as well as add these using statements:

using Microsoft.SharePoint;

using Microsoft.SharePoint.Linq;

using System.Linq;

Build and deploy the Web Part. Add some items to the Tasks

list and see that they appear in the Datagrid as well. You can also add the

DateTimeControl back and modify the WebPart to query only for the selected tasks

for that day.

As you can see, creating Web parts and query data from the

SharePoint 2010 installation is very easy with the new Visual Web Part and LINQ

to SharePoint features of VS2010.

Advertisment