Click here to Skip to main content
Click here to Skip to main content

SharePoint 2010 – Creating a Site Definition Using Visual Studio

, 31 May 2012
Rate this:
Please Sign up or sign in to vote.
Site Definition creation using Visual Studio 2010 and SharePoint developer tools.

Introduction

In this article we will explore Site Definition creation using Visual Studio 2010. Inside SharePoint the user has to create a site using Site Definition or a Web Template.

What is Site Definition?

Site Definition consists of XML and ASPX pages stored in the 14 Hive folder. You can locate them in the following folder:

C:\Program Files\Common Files\Microsoft Shared\Web Server
   Extensions\14\TEMPLATE\SiteTemplates

(Please note to use your front end server machine and installation drive letter.)

Inside the Folder

You can see n number of folders inside SiteTemplates.

Some of the folders which we interact with are:

  • MPS – Meeting Workspace
  • Blog – Blog Site

Why we need Site Definitions instead of Web Templates?

The user can create customized web templates. This will create the modified page inside the database and thus creates performance issues. So Site Definitions provide a better alternative in case there is multiple reuse of the instance.

Creating the Site Definition from Visual Studio 2010

Open Visual Studio in Elevated Permissions mode and use File > New Project > SharePoint > Site Definition.

(You need to install http://sharepointdevtools.codeplex.com/ to get the above template.)

Once the site definition project is created, add a new Visual Web Part into it. Our idea is to list the current Tasks in progress.

Add the following code into the web part load event:

protected void Page_Load(object sender, EventArgs e)
{
    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
    {
        using (SPWeb web = site.OpenWeb())
        {
            // Get data from a list.
            string listUrl = SPContext.Current.Site.Url + "/lists/tasks";
            SPList list = web.GetList(listUrl);
            SPView view = list.Views["All Tasks"];
            SPListItemCollection items = list.GetItems(view);
            foreach (SPListItem info in items)
                if (info["Status"].ToString() == "In Progress")
                {
                    this.Controls.Add(new Label() { Text = "Title: " + 
                         info.Title, ForeColor = System.Drawing.Color.Navy });
                    this.Controls.Add(new LiteralControl("<br/><br/>"));
                }
        }
    }
}

Now we need to specify the Visual Web Part in the Default.aspx of the site definition file.

Add the following lines in the SiteDefinition > default.aspx file.

<%@ Register  TagPrefix="MyWebPartControls" Namespace="SiteDefinitionProject1.VisualWebPart1" 
              Assembly="$SharePoint.Project.AssemblyFullName$" %>
<MyWebPartControls:VisualWebPart1 runat="Server" />

You can see the attached source code for a detailed analysis. Now we are ready with the site definition project which lists the current tasks that are In Progress.

Deploying the Site Definition to SharePoint

For deploying the site definition, you can right click on the project and select Deploy. The project gets deployed to the SharePoint site selected while project creation.

Wait for a few seconds to see the succeeded message.

Creating the Demo Data

Before proceeding with the next step, I recommend you to create a Tasks list with some tasks In Progress. This list will be used by our site definition project.

Accessing the New Site Definition

Inside SharePoint, use the Site Actions > New Site menu item.

In the appearing dialog, you can see the new site definition which we created.

Select the Item, enter a Title, URL, and click the Create button. You will see the following page with the In Progress tasks listed.

So this concludes our site definition creation using Visual Studio. This article explores the basics of site definition.  In real world scenarios, we can use a site definition to manipulate other lists or libraries.  The attachment contains the source code which we discussed.

References

Summary

In this article we have explored Site Definition creation using Visual Studio 2010 and SharePoint developer tools.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Jean Paul V.A
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.
 
In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.
 
Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.
 
You can find some of his work over here. He blogs at http://jeanpaulva.com
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 31 May 2012
Article Copyright 2012 by Jean Paul V.A
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid