Wednesday, September 7, 2011

How to create a Site Definition using Visual studio 2010 in SharePoint 2010?

Introduction:
In this article we will see What is Site Definition in SharePoint and also will discuss details steps to create a site definition using visual studio 2010 in SharePoint 2010.
Description:
What is Site Definition in SharePoint 2010?
A site definition defines a unique type of Microsoft SharePoint Foundation website according to MSDN. It mainly contains some configurations to make a web site. SharePoint provides various out of box site defination templates like STS which is responsible to make site like Team site, Blank Site, Document workspace. MPS site defination is responsible for creating sites like  Basic Meeting Workspace, Blank Meeting Workspace, Decision Meeting Workspace, Social Meeting Workspace, and Multipage Meeting Workspace etc. BLOG site defination is responsible creating sites like Blogs. CENTRALADMIN is responsible creating central administration websites.
There are two xml files needed for a site defination.
1. WebTemp*.xml file: This file identifies the site definitions and configurations and provide information about how they appear in the user interface (UI) to users who are creating new websites.
And it will found here in  %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\1033\XML  directory.

2. Onet.xml files: Define the navigation areas, specify the list definitions available in the UI, specify document templates and their files, define the base types for lists, and define configurations and modules for site definitions. And it will found in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteDefinitions\site_type\XML directory.

-->
Now lets us try to create a site defination. This will be a very simple site defination that will contain only one visual web part and that Visual web part will contain a Welcome message.
Step-1:
Open your Visual Studio 2010 and then go to File -> New -> Project from the New project dialog box Go to the SharePoint section under Visual C# installed templates and the click on 2010 under SharePoint. From there select the Site Definition project Template and give a name to that as shown below in the figure.
Step-2:
In the next screen it will ask you to give a local debugging site URL as shown below figure. And the click on Finish. Remember you can not deploy a site defination within a sandboxed environment. so the trust level is selected as Deploy as a farm solution by default.
Step-3:
Now look at the project structure very carefully. Here look at the 2 xml files onet.xml and webtemp_MyTestSiteDefination.xm file as I have described before. The webtemp file naming convention is also very much important, webtemp_[Site Defination Name].xml. You can see the figure below.
Step-4: Now let us add our visual web part. For this Right click on the site defination project -> Add -> New Item as shown in the below figure.

Step-5: After step-4 select the Visual web part template from the Add New Item dialoge box and give  a name and click on Add as shown in the figure below.
Step-6: Now we will add the wel come message to the user control like shown below in the figure.

Step-7: Now the whole project structure will be like the below shown in figure.
Step-8: Now our next step is to add the visual web part to the default.aspx page of the site defination, so that the content of the visual web part will appear in the default page. For this go to the default page and add the below line as shown below in the figure.
<%@ Register Tagprefix="MyWebPartControls" Namespace="MyTestSiteDefinition.MyVisualWebPart" Assembly="$SharePoint.Project.AssemblyFullName$" %>

Here carefully give your visual web part namespace name and tagname, no need to modify the assembly property.

Step-9:
Now we need to call the web part in the conent area. For this write
<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderMain" runat="server">
    <MyWebPartControls:MyVisualWebPart runat="server" />
</asp:Content>
 as shown in the figure below.

Step-10:
Now our simple site defination is ready and we can now run and deploy that. Thanks to the Visual studio inbuilt options. To deploy the site defination press F5, It will internally  compiles the code, adds its features, packages all of the files into a WSP file, and deploys the WSP file to SharePoint Server. SharePoint then installs the files and then activates the features.
You can check the status message like build the project,  Adding the solution, Activate the feature, Deploy succeeded etc.

Step-11:
Now our site defination is deployed successfully. This is the time to create a new site from out new site defination template. For this go to Site Action -> New Site -> Then in the Create dialog box you can see our custom site defination (MyTestSiteDefination) under the SharePoint Customization categoy as shown in the figure below.
Then you can select that and give the title and URL and click on create. The new web site will be using our custom site defination as shown below. And this content we have written in the visual web part.
To know more about how to deploy the wsp file to the production environment you can read this tutorial. You can also check an article on how to delete or uninstall a site definition.

No comments:

Post a Comment