Click here to Skip to main content
15,881,455 members
Articles / Programming Languages / C#
Tip/Trick

SharePoint 2010 – Creating a Site Definition Using Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
31 May 2012CPOL2 min read 52.7K   464   5   1
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.

Image 1

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.

Image 2

(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:

C#
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.

ASP.NET
<%@ 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.

Image 3

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.

Image 4

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

Image 5

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

Image 6

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)


Written By
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

Comments and Discussions

 
BugError while working on above code Pin
Member 1100655311-Aug-14 17:20
Member 1100655311-Aug-14 17:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.