Create a Basic Site definition Project in VS 2010

This walkthrough shows you how to create a basic site definition that contains a visual Web part with some controls on it.

In VS 2010, create a new sharepoint project using Site Definition template. Call it TestSiteDef. Deploy as farm solution.

Next, add a new item Visual Web Part to the project, use the defaul tname, VisualWebPart1.

Open VisualWebPart1UserControl.ascx in source code view, and copy the code in it: (with textbox, submit button and a label)

<asp:TextBox runat=”server” ID=”tbName”></asp:TextBox>
<asp:Button runat=”server” ID=”btnSubmit” Text = “Change Label Text” OnClick=”btnSubmit_Click”></asp:Button>
<asp:Label runat=”server” ID=”lblName”></asp:Label>

and paste this in the *.aspx.cs code behind

protected void btnSubmit_Click(object sender, EventArgs e)
lblName.Text = tbName.Text;

So now the Visual Web Part is completed. We want to add it to the defualt.aspx page of the new site definition.

Open defailt.aspx and add the following under the WebpartPages tag:

<%@ Register Tagprefix=”MyWebPartControls” Namespace=”TestSiteDef.VisualWebPart1” Assembly=”$SharePoint.Project.AssemblyFullName$” %>

On this MSDN walk-through page:

It says Namespace=”TestSiteDef.SiteDefinition.VisualWebPart1” but that’s incorrect. It should be TestSiteDef.VisualWebpart1

So you have this in default.aspx:

Replace the whole PlaceHolderMain secton withi this:

<asp:Content ID=”Content1″ ContentPlaceHolderId=”PlaceHolderMain” runat=”server”>
<MyWebPartControls:VisualWebPart1 runat=”server” />

Run and deploy the project to your server. Once done, now in Sharepoint server you have a new site difinition from which you can create a new site collection.

Now let‘s create a new site under the site collection. Go to your wb site, Site Actions à New Site.

Select Sharepoint Customizations
TestSiteDef as the site template.

Enter the name/url of the new site, and create it.

Once created, you will be taken to the home page of the new site, which has a custom web part on the first page.

Post a comment or leave a trackback: Trackback URL.

Leave a Reply

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

You are commenting using your 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: