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.
This Visual How To describes the following steps for creating and deploying a site definition in Visual Studio 2010:
Creating a SharePoint 2010 site definition application in Visual Studio 2010.
Editing the onet.xml file to include a list.
Adding a Web Part to the Site Definition project.
Adding the Web Part to the default.aspx page.
Creating a new site based on the site definition.
To create a SharePoint 2010 Site Definition project in Visual Studio 2010
Start Visual Studio 2010. On the File menu, click New, and then click Project.
In the New Project dialog box, in the Installed Templates section, expand either Visual Basic or Visual C#, expand SharePoint, and then click 2010.
In the template list, click Site Definition.
In the Name box at the bottom, type FilteredTaskSite.
Leave the default values in the other fields, and click OK.
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
In Solution Explorer, right-click the project, point to Add, and then click New Item.
In the list of items, click Visual Web Part.
Ensure that the Web Part is named VisualWebPart1, and then click Add.
Open VisualWebPart1UserControl.ascx, right-click the design surface, and then click View Code.
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(filterDate)
Me.Controls.Add(New LiteralControl("<br />"))
Me.Controls.Add(MyCustomView)
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(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
To deploy and test the project
In Solution Explorer, right-click the project, and then select Deploy.
Open the SharePoint Home page.
Above the ribbon, click Site Actions, and on the menu, click New Site.
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.
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.
|
Watch the video
> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/efdb4d4d-1201-4a29-aa31-f63bfa1434b0]
Length: 00:05:24
Grab the Code
|