Steps to create and deploy a Visual WebPart:
- Start Visual Studio 2010, click File -> New -> Project.
- Navigate to the Visual C# node in the Installed Templates section, click SharePoint, and then click 2010.
Select the Visual Web Part project template and provide a name (such as, SampleWebPart), a location for your project, and then click OK.
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();
}
}
}
}
For more details please check out this link...
http://mindstick.com/Articles/9e1d1fe1-7e51-4d01-86ec-f1f679631a7e/?Create%20and%20Deploy%20Visual%20WebPart%20in%20SharePoint%202010
I hope it might be useful for you.
IF you wish to deploy the solution to another system you will have to locate the WSP file produced by visual studio and manually deploy it using stsadm.
– James Love Aug 10 '10 at 16:28