Creating a Visual Web Part in Visual Studio for SharePoint 2010

I started classes at SetFocus (http://www.setfocus.com/) several months back to continue improving my skill set as a developer. I am always trying to discover new innovative ways to develop and understand new development strategies. I wanted to share this one with everyone since I thought it was somewhat difficult to implement at first but very simple and elaborate when complete.

I started with a empty SharePoint project and then added a Visual Web Part to the project. One thing you have to have is a SharePoint on the machine that you are using the Visual Studio to view and use the SharePoint project software. However SharePoint 2010 and Visual Studio 2010  has come along ways compared to previous versions. The functionality and ability has increased and the simplicity for developers like me (must be simple for little brains like mine) is really awesome.

The first thing you need to do is open Visual Studio 2010 on the machine that you have the SharePoint installed on.

Then select File\New Project ==> Then Choose Empty SharePoint Project.

I named my Empty SharePoint Project: CreateSolutionSiteWebPart. Then add to that project a Visual Web Part.

By selecting the Empty SharePoint Project and right clicking and selecting Add ==> New Item you will get a add new item list box. Select the Visual Web Part.

Once the Visual Web Part is added to the SharePoint Project you will see it is really nothing more then a .ascx user control. So you can add a div tag on the source view of the .ascx page. This is an example of the code I added to that form.

<div>

    <asp:Label ID="lblSiteName" Text="Site Name: " runat="server" Width="100" />
    <asp:TextBox ID="txtSiteName" runat="server" />

    <asp:Label ID="lblSiteDescription" Text="Site Description: " runat="server" />
    <asp:TextBox ID="txtSiteDescription" runat="server" />

    <asp:Label ID="lblIntendedURL" Text="Intended URL: " runat="server" />
    <asp:TextBox ID="txtIntendedURL" runat="server" />

    <asp:Label ID="lblSiteTemplate" Text="Site Template: " runat="server" />
    <asp:DropDownList ID="ddlSiteTemplate" runat="server"
        onselectedindexchanged="ddlSiteTemplate_SelectedIndexChanged">
    </asp:DropDownList>

    <asp:CheckBox ID="chkUniquePermissions" Text="Unique Permissions " runat="server" />

    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
        Text="Submit" />

</div>

Next I created the code behind that is required for the application. That looks like this.

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

namespace CreateSolutionSiteWebPart.CreateSolutionSiteWP
{

    public partial class CreateSolutionSiteUC : UserControl
    {
        SPWebTemplateCollection wt;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtSiteName.Text == string.Empty && txtIntendedURL.Text == string.Empty)
                return;

            using (SPSite site = new SPSite(@"http://URLName:PortNumber/sites/Solutions"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    try
                    {

                        SPSecurity.RunWithElevatedPrivileges(() => web.Webs.Add(
                            txtIntendedURL.Text,
                            txtSiteName.Text,
                            txtSiteDescription.Text,
                            1033,
                            ddlSiteTemplate.SelectedValue,
                            chkUniquePermissions.Checked,
                            false));

                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "alert('Success');", true);

                    }
                    catch (Exception ex)
                    {
                        string error = ex.Message;
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "alert('" + error + "');", true);

                    }
                    finally
                    {
                        txtSiteName.Text = string.Empty;
                        txtSiteDescription.Text = string.Empty;
                        txtIntendedURL.Text = string.Empty;
                    }
                }
            }

        }

        protected override void OnLoad(EventArgs e)
        {

            EnsureChildControls();

            if (!IsPostBack)
            {
                using (SPSite site = new SPSite(@"http://URLName:PortNumber/sites/Solutions"))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        try
                        {
                            wt = web.GetAvailableWebTemplates(1033);
                            foreach (SPWebTemplate template in wt)
                            {
                                ddlSiteTemplate.Items.Add(new ListItem(template.Title, template.Name));
                            }
                            ddlSiteTemplate.DataBind();

                        }
                        catch (Exception ex)
                        {
                            string error = ex.Message;
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), Guid.NewGuid().ToString(), "alert('" + error + "');", true);
                        }
                    }
                }
            }

        }

    }
}

I removed the real URL and Port Number for security however all the other code is exact.

This code gives the ability to add a Visual Web Part to your SharePoint 2010 that will allow for a user with permissions to create a new site on the SharePoint Portal. This is really not that amazing but it does show the simplicity and power of what Visual Studio 2010 and SharePoint 2010 is capable of doing. I am very impressed with the functionality and collaboration that can be accomplished is a very small amount of time.

This is an example of the simple look and feel of the Visual Web Part on the web site.



	
	
	

	

One Comment on “Creating a Visual Web Part in Visual Studio for SharePoint 2010

  1. Got this error:
    An error occurred during the compilation of the requested file, or one of its dependencies. ‘ASP._controltemplates_createsolutionsitewebpart_visualwebpart2_visualwebpart2usercontrol_ascx’ does not contain a definition for ‘ddlSiteTemplate_SelectedIndexChanged’

    Seems as though your code behind didn’t handle the ddlSiteTemplate_SelectedIndexChanged event?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: