How can we provision page template and page instances?

 Posted by ArticlesMaint on 9/23/2009 | Category: SharePoint Interview questions | Views: 5789

How can we provision page template and page instances?

Before we move ahead with this question let’s first define provisioning. Provisioning is a fancy name of making something available. So let’s first create a page template using the ‘Default.Master’ and the see how we can provision this page on the website.. So below is the code snippet where we can create a master page from the ‘Default.Master’.

<%@ Page MasterPageFile="~masterurl/default.master"%>

<asp:Content runat="server" ContentPlaceHolderID="PlaceHolderMain">
<h3>Hi this is a Page made from Page template</h3>

In order to use the template we need to create an instance in the SharePoint runtime. Creating a instance of the template in the SharePoint runtime is termed as provisioning.

We need to use the ‘Module’ and ‘File’ tag to provision an instance of the template page. This is done in the ‘ElementManifest.XML’ file. Below is the code snippet of ‘ElementManifest.XML’ file which has the module tag with a file tag specifying what the URL page name is? This is the same template which we had shown on the top. ‘PageA.aspx’ is the instance name and ‘MyPage.aspx’ is the physical page name of the template page.

The type attribute has two values ‘Ghostable’ and ‘GhostableInLibrary’. If you want to provision a resource inside a document library then you need to specify ‘GhostableInLibrary’. If you do not want to provision a resource inside a document library then you need to specify ‘Ghostable’ value. For the current exercise we have given the value as ‘Ghostable’.

A note the path attribute has the page in the ‘MyPath’ folder.

<Elements xmlns="">

<Module Path="MyPage" Url="SitePages">
<File Url="MyPage.aspx" Name="PageA.aspx" Type="Ghostable" />

As said previously we also need to define the feature.xml file which has the receiver class and the element manifest pointing to the elementmanifest.xml file.

<Feature Id="701B7EA3-0816-4a5f-8FFE-AD15F0E5B562" Title="Provision"

Description="This features enables us to goto Custom Page"
ReceiverAssembly="ClassLibrary1, Version=, Culture=neutral, PublicKeyToken=52bd1db23825a2e4"
<ElementManifest Location="ElementManifest.xml" />

In the receiver class we will add menu items in the quick launch nodes. So in the activation event we will add a link which will activate this feature and in the deactivation event we will remove this feature.

So in the activation event we first need to get reference to the SpSite object using the URL.

SPSite ObjQuickLaunch = new SPSite("http://mum2815dz:2/sites/LearnSharePoint");

From the SpSite object we get the ‘SpWeb’ object.

SPWeb ObjSpWeb = ObjQuickLaunch.OpenWeb();

From the ‘SpWeb’ object we get reference to all node collection of the Quick launch menu.

SPNavigationNodeCollection QuickLauncNodes = ObjSpWeb.Navigation.QuickLaunch;

Now we create a menu of the page.

SPNavigationNode objMenuItem = new SPNavigationNode("Quest", "SitePages/PageA.aspx");

Add the menu to the quick launch node.



In the deactivation event we will remove the link.

Now that we have done with creating the two xml files and the class which will handle the feature events , use stsadm utility to register the feature. Once you have registered it you should see the feature in the feature list display. We have named the feature as ‘Provision’ so you can see the ‘Provision’ feature.

Once you activate the feature you should see the ‘Quest’ link which points to the template instance page ‘PageA.aspx’. If you disable the feature you will not be able to browse the page instance.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response