Creating SharePoint 2010 Site Definitions in Visual Studio 2010

SharePoint Visual How To

Summary:  Learn how to create site definitions for Microsoft SharePoint 2010 by using Microsoft Visual Studio 2010.

Applies to: Office 2010 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010

Provided by:  Ben Hedges, Point8020


In Microsoft SharePoint 2010, you can create new sites from site definitions by clicking New Site on the Site Actions menu. You can create new site definitions in Microsoft Visual Studio 2010 and then deploy them to SharePoint 2010. This SharePoint Visual How To demonstrates how to create a new site definition and then add a Web Part to the site's default.aspx page. The Web Part filters tasks based on their due dates.

Code It

This Visual How To describes the following steps for creating and deploying a site definition in Visual Studio 2010:

  1. Creating a SharePoint 2010 site definition application in Visual Studio 2010.

  2. Editing the onet.xml file to include a list.

  3. Adding a Web Part to the Site Definition project.

  4. Adding the Web Part to the default.aspx page.

  5. Creating a new site based on the site definition.

To create a SharePoint 2010 Site Definition project in Visual Studio 2010

  1. Start Visual Studio 2010. On the File menu, click New, and then click Project.

  2. In the New Project dialog box, in the Installed Templates section, expand either Visual Basic or Visual C#, expand SharePoint, and then click 2010.

  3. In the template list, click Site Definition.

  4. In the Name box at the bottom, type FilteredTaskSite.

  5. Leave the default values in the other fields, and click OK.

  6. Under What local site do you want to use for debugging?, select your site. Click Finish.

To modify the onet.xml file

To create a Visual Web Part in Visual Studio

  1. In Solution Explorer, right-click the project, point to Add, and then click New Item.

  2. In the list of items, click Visual Web Part.

  3. Ensure that the Web Part is named VisualWebPart1, and then click Add.

  4. Open VisualWebPart1UserControl.ascx, right-click the design surface, and then click View Code.

  5. Replace the code with the following.

    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.WebControls
    Imports Microsoft.SharePoint.Utilities
    Partial Public Class VisualWebPart1UserControl
        Inherits UserControl
        Dim filterDate As DateTimeControl
        Dim MyCustomView As ListViewByQuery
        Dim query As SPQuery
        Dim thisWeb As SPWeb
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            filterDate = New DateTimeControl()
            filterDate.DateOnly = True
            filterDate.AutoPostBack = True
            AddHandler filterDate.DateChanged, AddressOf filterDate_DateChanged
            thisWeb = SPContext.Current.Web
            MyCustomView = New ListViewByQuery()
            Dim list As SPList = thisWeb.Lists("Project Tasks")
            MyCustomView.List = list
            query = New SPQuery(MyCustomView.List.DefaultView)
            query.ViewFields = _
              "<FieldRef Name='Title' /><FieldRef Name='AssignedTo' /><FieldRef Name='DueDate' />"
            MyCustomView.Query = query
            Me.Controls.Add(New LiteralControl("Tasks due on or before:"))
            Me.Controls.Add(New LiteralControl("<br />"))
            Me.Controls.Add(New LiteralControl("<br />"))
        End Sub
        Sub filterDate_DateChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim camlQuery As String
            camlQuery = "<Where><Leq><FieldRef Name='DueDate' />" _
              + "<Value Type='DateTime'>" _
              + SPUtility.CreateISO8601DateTimeFromSystemDateTime(filterDate.SelectedDate) _
              + "</Value></Leq></Where>"
            query.Query = camlQuery
            MyCustomView.Query = query
        End Sub
    End Class
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.Utilities;
    namespace FilteredTaskSite.VisualWebPart1
        public partial class VisualWebPart1UserControl : UserControl
            DateTimeControl filterDate;
            ListViewByQuery MyCustomView;
            SPQuery query;
            protected void Page_Load(object sender, EventArgs e)
                filterDate = new DateTimeControl();
                filterDate.DateOnly = true;
                filterDate.AutoPostBack = true;
                filterDate.DateChanged += new EventHandler(filterDate_DateChanged);
                SPWeb thisWeb = SPContext.Current.Web;
                MyCustomView = new ListViewByQuery();
                MyCustomView.List = thisWeb.Lists["Project Tasks"];
                query = new SPQuery(MyCustomView.List.DefaultView);
                query.ViewFields = 
                  "<FieldRef Name='Title' /><FieldRef Name='AssignedTo' /><FieldRef Name='DueDate' />";
                MyCustomView.Query = query;
                LiteralControl filterMessage = new LiteralControl("Tasks due on or before:");
                this.Controls.Add(new LiteralControl("<br />"));
                this.Controls.Add(new LiteralControl("<br />"));
            void filterDate_DateChanged(object sender, EventArgs e)
                string camlQuery = "<Where><Leq><FieldRef Name='DueDate' />"
                  + "<Value Type='DateTime'>"
                  + SPUtility.CreateISO8601DateTimeFromSystemDateTime(filterDate.SelectedDate)
                  + "</Value></Leq></Where>";
                query.Query = camlQuery;
                MyCustomView.Query = query;

To add the Visual Web Part to the default.aspx page

To deploy and test the project

  1. In Solution Explorer, right-click the project, and then select Deploy.

  2. Open the SharePoint Home page.

  3. Above the ribbon, click Site Actions, and on the menu, click New Site.

  4. In the list of templates, select FilteredTaskSite, and using the form on the right side, create a new site.

The new site appears with the Project Tasks list on the Quick Launch menu and the new Web Part on the default page. Add a few tasks with different due dates, and then use the Web Part on the home page to filter items that have a due date on or before the selected date.

Read It

The onet.xml file contains information about a new site definition. This example demonstrated the following tasks:

  • Creating a task list named Project Tasks and adding it to the Quick Launch navigation bar.

  • Adding a Web Part to the project that filters tasks based on their due dates.

  • Editing the default.aspx page so that it includes the new Web Part.

  • Deploying and testing the site definition.

See It

Watch the video

> [!VIDEO]

Length: 00:05:24

Click to grab code

Grab the Code

Explore It