Export (0) Print
Expand All
Expand Minimize

Creating SharePoint 2010 Site Definitions in Visual Studio 2010

SharePoint 2010

SharePoint Visual How To

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

Last modified: September 12, 2012

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

Provided by:  Ben Hedges, Point8020

Overview

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

  1. In Solution Explorer, under the SiteDefinition node, open the onet.xml file.

  2. Add the following markup between the <NavBars> </NavBars> tags.

    <NavBar Name="$Resources:core,category_Lists;" 
    Prefix="&lt;table border='0' cellpadding='4' cellspacing='0'&gt;" 
    Body="&lt;tr&gt;&lt;td&gt;&lt;table border='0' cellpadding='0' cellspacing='0'
    &gt;&lt;tr&gt;&lt;td&gt;&lt;img src='/_layouts/images/blank.gif' id='100' alt='' border='0'
    &gt;&amp;nbsp;&lt;/td&gt;&lt;td valign='top'&gt;&lt;a id='onetleftnavbar#LABEL_ID#' href='#URL#'
    &gt;#LABEL#&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;" Suffix="&lt;/table
    &gt;" ID="1003" />
    
  3. Replace the <Lists/> tag with the following code:

    <Lists>
    <List FeatureId="00BFEA71-A83E-497E-9BA0-7A5C597D0107" Type="107" 
    Title="Project Tasks" 
    Url="$Resources:core,lists_Folder;/$Resources:core,tasks_Folder;" 
    QuickLaunchUrl="$Resources:core,lists_Folder;/$Resources:core,tasks_Folder;/AllItems.aspx" />
    </Lists>
    

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.

    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(filterMessage);
                this.Controls.Add(new LiteralControl("<br />"));
                this.Controls.Add(filterDate);
                this.Controls.Add(new LiteralControl("<br />"));
                this.Controls.Add(MyCustomView);            
            }
            
            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

  1. In Solution Explorer, expand the SiteDefinition node, and then open default.aspx.

  2. At the top of the page, under the WebPartPages tag, add the following code.

    <%@ Register  TagPrefix="MyWebPartControls" Namespace="FilteredTaskSite.VisualWebPart1" 
      Assembly="$SharePoint.Project.AssemblyFullName$" %>
    
  3. Locate the <asp:content> tag that contains ContentPlaceholderId = "PlaceHolderMain", and replace the tag with the following.

    <asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderMain" runat="server">
    <MyWebPartControls:VisualWebPart1 runat="Server" />
    </asp:Content>
    

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

Watch video

Length: 00:05:24

Click to grab code

Grab the Code

Explore It
Show:
© 2014 Microsoft