Creating Visual Web Parts for Sharepoint 2010 using Visual Studio 2010 – Part 1

Shameem Ahmed 3 February 2012 2




Creating Visual Web Parts for Sharepoint 2010 using Visual Studio 2010 – Part 1

Visual Studio 2010 development tools for Sharepoint 2010 provide an easy way to develop custom solutions for Sharepoint with minimum effort.  Developing for Sharepoint 2010 is now as easy as developing ASP.NET web applications.

Development tools like project template specific for Sharepoint 2010, Workflow templates and Visual Web Parts templates greatly enhances developer productivity and saves lot of development time.

In this article, we’re going to see how to use visual web part template and develop a custom solution in Sharepoint 2010 with minimum code and effort.

Case Study

We’re going to create a web part that will display a list of available SharePoint lists in the current web site. Once the web part is developed and deployed into the site, it can be used in any Sharepoint page.

Solution

Follow these steps to create a new project in Visual Studio 2010.

Open Visual Studio 2010, Select File, New, Project from the menu.

Select Visual C#, SharePoint, 2010 from Installed Templates

Select Visual Web Part and type name and location for your project files


Select where you want to deploy your project

Once project is successfully created, Visual Studio 2010 will provide you with a default web part called VisualWebPart1.  We can add as many web parts as we want for our project.

Customizing Visual Web Part

Design the web part to your requirements, in this case we’ve added a Label and Listbox control from the Toolbox.


Goto the Page_Load event of the control and add the following code accordingly:

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

namespace VisualWebPartProject1.vwpList
{
    public partial class vwpListUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            lbLists.Items.Clear();
            foreach (SPList list in web.Lists)
            {
                 lbLists.Items.Add(new ListItem(list.Title,
                 list.ID.ToString()));
             }
       }
   }
}

The code is simple, we’re creating a SPWeb object for the current sharepoint site and looping through the lists and populating the list control with sharepoint list name and id.

That’s it, run the project (press F5) from Visual Studio 2010 to deploy and test the solution.

The Run command will deploy the custom solution to the sharepoint site we’ve mentioned while creating the project.

Insert the web part into sharepoint page by doing the following:

Select Edit to edit the sharepoint page.

Goto Insert tab and click Web Part

Select Custom from Categories and web part name Web Parts section

Click Add to insert our new web part into the page.

Summary

In just about minutes we’ve developed a fully functional web part that is ready to be used across sharepoint sites. Though we’ve started simple, in the coming articles we’ll explore more options about how to work with sharepoint object like lists, document libraries, workflows etc. and create custom solutions with minimum effort.

 

 

 




Shameem Ahmed (370 Posts)

2 Comments »

  1. Ashish 17 October 2012 at 2:21 am - Reply

    Thanks to u….i have created my first visual web part and used in SPT2010

Leave A Response »