Archive

Posts Tagged ‘Visual Web Part’

Demo of adding SharePoint 2010 Project Items to a project in Visual Studio 2010

This demo video includes a walkthrough of creating a visual webpart using Visual Studio 2010.

Deploying the webpart using the Visual Studio 2010 IDE is also included.

Using Visual Studio 2010 you have project templates.
These templates are known as SharePoint Project Items (SPI).

If you are used to using the VSeWSS tools with Visual Studio 2005 – 2008 then you are already used to the idea of creating projects using templates.

The templates that are included in Visual Studio 2010 include:

Application Page
Business Data Connectivity Model
Content Type
Empty Element
Event Receiver
List Definition
List Definition from Content type
List Instance
Module
Sequential Workflow
State Machine Workflow
User Control
Visual Web Part
Web Part
Workflow Association Form
Workflow Initiation Form

 

Get the Flash Player to see this content.

 

Demo of adding SharePoint 2010 Project items

March 14th, 2010 Neal McFee No comments

Walkthrough 1: Using LINQ to SharePoint from within a Visual web part

Exercise Duration: 10 minutes

Exercise Overview

This example demonstrates how to use the SharePoint LINQ provider to read data from a SharePoint list and render the data using the SPGrid web control. It shows these created in the Visual Web Part designer in Visual Studio 2010.

Task 1: Create a new Empty Project and add a Visual Web Part

1. Open Visual Studio 2010 from the Start | Programs | Visual Studio 2010 menu.

2. Create a new project by using File | New Project.

3. Pick the SharePoint | 2010 templates.

4. From the SharePoint | 2010 templates select the Visual Web Part project template.

5. Use SPLinqSolutionDemo as the name of the project.

6. Use C:\SPHOLs as the location.

7. Press OK to create the project.

image

 

8. When the SharePoint Customization Wizard dialog appears, select the site you want to deploy the Web Part to. Click the Finish button to complete the project creation process.

image

 

9. Within Solution Explorer expand VisualWebPart1 and open VisualWebPart1.webpart.

a. Set the title element to: SPLinqDemoPart Title

b. Set the description element to: SPLinqDemoPart Description

image

 

Task 2: Generate LINQ to SharePoint proxy class to access list data.

1. Start a Visual Studio 2010 Command Prompt from the Start | All Programs | Visual Studio 2010 | Visual Studio Tools |Visual Studio Command Prompt (2010) menu

2. Change the directory to C:\SPHOLS\SPLinqSolutionDemo  

cd C:\SPHOLS\SPLinqSolutionDemo

3. spmetal.exe is a command line tool that can generate C# or VB.NET classes from a SharePoint site’s list definitions. It is similar to wsdl.exe that generates a proxy from a Web Service’s WSDL file. 

4. Run the following command to generate the LINQ proxy code.

spmetal /web:http://<YourServerName> /namespace:Projects /code:Projects.cs

5. Go back to Visual Studio 2010.

6. Right-click on the SPLinqSolutionDemo project in the Solution Explorer and select Add | Existing Item.

7. Browse to C:\SPHOLS\SPLinqSolutionDemo and select the Project.cs file that was generated when you ran spmetal.

8. Right-click on the SPLinqSolutionDemo in the Solution Explorer and select Add Reference.

 

9. Click on the Browse tab, navigate to the C:\Program Files\Common Files\Microsoft Shared\Web Server Extension\14\ISAPI folder, and select Microsoft.SharePoint.Linq.dll and select OK.

 

image

  

Task 3: Write the code for the Visual WebPart User to access the list data.

1. In Visual Studio 2010 open the SPLinqDemoPartUserControl.ascx file from the Solution Folder by double-clicking on it.

2. Add the following markup to SPLinqDemoPartUserControl.ascx under the <%@ Control… declaration

 

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

 

<SharePoint:SPGridView id="spGridView" runat="server" AutoGenerateColumns="false">

  <HeaderStyle HorizontalAlign="Left" ForeColor="Navy" Font-Bold="true" />

  <Columns>

    <SharePoint:SPBoundField  DataField="Title" HeaderText="Title"></SharePoint:SPBoundField>

    <SharePoint:SPBoundField DataField="JobTitle" HeaderText="JobTitle"></SharePoint:SPBoundField>

    <SharePoint:SPBoundField DataField="ProjectTitle" HeaderText="ProjectTitle"></SharePoint:SPBoundField>

    <SharePoint:SPBoundField DataField="DueDate" HeaderText="DueDate"></SharePoint:SPBoundField>

  </Columns>

</SharePoint:SPGridView>

 

3. In the Solution Explorer, expand the carat icon next to the SPLinqDemoPartUserControl.asx, and then double-click on the SPLinqDemoPartUserControl.ascx.cs file to open it.

 

image

4. Add the following using statements to SPLinqDemoPartUserControl.ascx.cs  file after the existing using statements.

 

using System.Linq;

using Projects;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Linq;

 

 

5. Add the following code to the SPLinqDemoPartUserControl.ascx.cs  file (replace the existing Page_Load method).

 

protected void Page_Load(object sender, EventArgs e)

{

    ProjectsDataContext dc = new

    ProjectsDataContext(SPContext.Current.Web.Url);

 

    EntityList<EmployeesItem> Employees = 

    dc.GetList<EmployeesItem>("Employees");

 

    var empQuery = from emp in Employees

                   where emp.Project.DueDate <                    DateTime.Now.AddMonths(6)

                   select new { emp.Title, emp.JobTitle, 

                   ProjectTitle = emp.Project.Title, 

                   DueDate =                    emp.Project.DueDate.Value.ToShortDateString() };

 

    spGridView.DataSource = empQuery;

    spGridView.DataBind();

}

 

Task 4: Deploy and Test the WebPart

1. Right click on your project and select Deploy. 

2. Add the web part to a web part page:

Click the Edit button to put the page in edit mode. 

Click Insert

Click Web Part

Click Custom

Click SPLinqDemoPart Title

Click Add 

 

image

The rendered web part will look like this.

image

Exercise Summary

In this walkthrough you built and deployed a Visual Web Part that uses LINQ to SharePoint to gather data from a SharePoint list.

  

February 10th, 2010 Neal McFee No comments