Beginner
    AJAX
    Android
    ASP.Net
    C#
    Cloud Computing
    Crystal Report
    Database
    Drupal
    HTML5
    JavaScript
    Joomla
    JQuery
    MVC
    php
    Reporting
    SharePoint
    SQL Server
    VB.Net
    Windows Phone
    WordPress
    WPF
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> SharePoint >> Create and Deploy Visual WebPart in SharePoint 2010




Create and Deploy Visual WebPart in SharePoint 2010 - MindStick

Create and Deploy Visual WebPart in SharePoint 2010

In this article I am going to explain how to create Visual WebPart by using Visual Studio 2010 and deploy it in the SharePoint 2010.

Steps to create and deploy a Visual WebPart:

  1. Start Visual Studio 2010, click File New Project.
  2. Navigate to the Visual C# node in the Installed Templates section, click SharePoint, and then click 2010.
  3. Select the Visual Web Part project template and provide a name (such as, SampleWebPart), a location for your project, and then click OK.

Create and Deploy Visual WebPart in SharePoint 2010

In the SharePoint Customization Wizard, select the site to use (such as http://localhost:portno/). Also select the Deploy as a farm solution option and then click Finish.

Create and Deploy Visual WebPart in SharePoint 2010

After the project is created, Solution Explorer contains the default Visual Web Part named VisualWebPart1. Also see in Solution Explorer the presence of the Features and Package nodes.
A feature organizes your application in a way that SharePoint Foundation understands. Features can be deployed to SharePoint Foundation at the site or Web level. The package contains features and other assets used when you deploy solutions to SharePoint Foundation.

Create and Deploy Visual WebPart in SharePoint 2010

In Solution Explorer, expand the VisualWebPart1 node, right-click the VisualWebPart1UserControl.ascx file, and then click View Designer. This action opens a view to drag-and-drop controls from the toolbox onto the Web Part designer surface.

Select the TreeView control and in the Properties panel in the lower-right corner of the Visual Studio screen, type the name siteStructure in the ID field.

Create and Deploy Visual WebPart in SharePoint 2010

In Solution Explorer, expand the VisualWebPart1UserControl.ascx node, right-click the VisualWebPart1UserControl.ascx.cs node, and then click View Code. Next, add the following C# code:

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

 

namespace SampleWebPart.VisualWebPart1

{

    public partial class VisualWebPart1UserControl : UserControl

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            SPWeb thisWeb = null;

            TreeNode node;

            thisWeb = SPContext.Current.Web;

            //Add the Web's title as the display text for the tree node, and add the URL
              as the NavigateUri.

            node = new TreeNode(thisWeb.Title, null, null, thisWeb.Url, "_self");

            //The Visual Web Part has a treeview control called siteStructure.

            siteStructure.Nodes.Add(node);

            //Get a reference to the current node, so child nodes can be added in the
              correct position.

            TreeNode parentNode = node;

            //Iterate through the Lists collection of the Web.

            foreach (SPList list in thisWeb.Lists)

            {

                if (!list.Hidden)

                {

                    node = new TreeNode(list.Title, null, null, list.DefaultViewUrl,
                                                                              "_self");

                    parentNode.ChildNodes.Add(node);

                }

            }

            foreach (SPWeb childWeb in thisWeb.Webs)

            {

                //Call our own helper function for adding each child Web to the tree.

                addWebs(childWeb, parentNode);

                childWeb.Dispose();

            }

            siteStructure.CollapseAll();

        }

        void addWebs(SPWeb web, TreeNode parentNode)

        {

            TreeNode node;

            node = new TreeNode(web.Title, null, null, web.Url, "_self");

            parentNode.ChildNodes.Add(node);

            parentNode = node;

            foreach (SPList list in web.Lists)

            {

                if (!list.Hidden)

                {

                    node = new TreeNode(list.Title, null, null, list.DefaultViewUrl,
                                                                               "_self");

                    parentNode.ChildNodes.Add(node);

                }

            }

            foreach (SPWeb childWeb in web.Webs)

            {

                //Call the addWebs() function from itself (i.e. recursively)

                //to add all child Webs until there are no more to add.

                addWebs(childWeb, parentNode);

                childWeb.Dispose();

            }

        }

    }

}

 

Now, you can build and deploy your solution by clicking the Build menu, selecting Build Solution, verifying that the solution builds without any errors, and then selecting Deploy Solution.

 

Now, you have to create a Web Parts page to contain the Web Part:

  1. Open the SharePoint site, click Site Actions, click View All Site Content, click Create, and select the Web Part Page option.
  2. In the Web Part Page screen, provide a name (MyWebPart) and layout template for the page, and then click Create. SharePoint creates and displays your Web Parts page.

Create and Deploy Visual WebPart in SharePoint 2010

On the Web Parts page, click into the Add a Web Part text in the zone where you want the Web Part displayed.

In the Categories list, click Custom. In the Web Parts box, click VisualWebPart1.

In the About the Web Part box at the top of the page, click Add. The Web Part is added to the zone that you selected as shown in the below figure:

Create and Deploy Visual WebPart in SharePoint 2010

Thanks for reading this article. I think this will help a lot while creating and deploying Visual WebPart in SharePoint 2010.



 Author Information
Name: Rohit Kesharwani
Member Since: 8/8/2011
Article Submitted Date: 12/15/2011
Location:     India
Report Abuse Form
Reason:    
 
Total Online Users: 7323
Advertisement
MindStick SurveyManager
Advertise with Us
  
Dudelabs
Copyright © 2013MindStick. All Rights Reserved.