.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson
Eminent IT

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Creating Site Definition using Visual Studio in SharePoint 2010

Posted By:Jean Paul       Posted Date: February 19, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

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

In this article we can explore the 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 folder 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 users can create customized web templates.  This will create the modified page inside database and thus arise performance issues.  So Site Definitions provide a better alternative in case there are multiple reuse of the instance.

Creating 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 detailed analysis.  Now we are ready with the site definition project which lists the current tasks that are In Progress.

Deploying Site Definition to SharePoint

For deploying the site definition you can right click on the project and select Deploy.

The project get 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 one can use site definition to manipulate other lists or libraries.  The attachment contains the source code which we discussed.




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

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend