How to create SharePoint 2010 Visual web part using Visual Studio 2010

In this article we are going to create SharePoint 2010 Visual web part using visual studio 2010.


Steps Involved:

Creating Empty SharePoint Project:

  • Open Visual Studio 2010. 

  • Go to File => New => Project. 

  • Select 2010 from the installed templates SharePoint and choose Empty SharePoint Project. 

  • Name it as HelloWorld.

  • Click Add. 

  • Enter the SharePoint server farm URL. 

  • Select 'Deploy it as a Farm Solution". 

  • Click Finish.


Creating Visual web part:

  • Right click the Solution Explorer and select Add a new item. 

  • Select Visual Web Part from the installed templates SharePoint and name it as HelloWorld.


  • Once you create Visual Web Part automatically all the files will be generated as shown in the below figure that is required to deploy your web part directly into the SharePoint 2010.

  • In the solution explorer you could see VisualWebPart1UserControl.ascx.

  • Open the .ascx file and add one asp label control.

  • In this web part I am just going to display a label with the text as "Hello World!!!!".

  • See the below code.

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

  2. <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

  3. <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

  4. <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities"Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

  5. <%@ Register Tagprefix="asp" Namespace="System.Web.UI"Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

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

  7. <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

  8. <%@ Control Language="C#" AutoEventWireup="true"CodeBehind="VisualWebPart1UserControl.ascx.cs"Inherits="HelloWorld.VisualWebPart1.VisualWebPart1UserControl" %>

  9. <asp:Label ID="lblHelloWorld" runat="server" Font-Bold="True" ForeColor="#0066FF" Text="Hello World!!!!"></asp:Label>

  • Build the solution. 

  • Deploy the solution.


Testing:

  • Go to the SharePoint site http://servername:2010.

  • Go to Site Actions=>Edit Page.

  • In the top ribbon Editing Tools => Insert => Web Part, click on the Web Part. 


  • Go to Categories => Custom, you could see VisualWebPart1 which we have developed using Visual Studio 2010.


  • Select VisualWebPart1 and click on Add.

  • Now you will able to see the label with the text "Hello World !!!!" as shown below.


Summary:

In this article my intension is to give some information about how to create a simple Visual Web part (a new template that is available in visual studio) and deploy directly into the SharePoint 2010.