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
- http://msdn.microsoft.com/en-us/library/gg276356.aspx
- http://msdn.microsoft.com/en-us/library/ms434313.aspx
- http://www.devx.com/dotnet/Article/27673
Summary
In this article we have explored Site Definition creation using Visual Studio 2010 and SharePoint developer tools.
No comments:
Post a Comment