AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=759&pId=-1
Working with SharePoint Templates
page
by Ameet Phadnis
Feedback
Average Rating: 
Views (Total / Last 10 Days): 30247/ 35

Introduction

As a SharePoint Developer, often I am tasked with creating SharePoint Templates for sub areas. The clients would like to use these templates throughout the sub areas. At times the client requirements for SharePoint Templates can be as easy as creating only some extra Web Part Zones or even adding Web Part Zones to the header for images to be posted at a later stage. But sometimes, the clients might ask for some challenging templates to display multiple document libraries on the same page with default field names displayed. This article will try to clarify some of the concepts of how the SharePoint templates work.

With the help of this article, you will learn how to

1. Create a new template.

2. Add Web Part Zones to a template in default.aspx page.

3. Add Web Parts by default while create Sub Area Pages using Onet.xml.

4. Create default Content Lists using Onet.xml.

5. Create multiple lists of the same list type in Onet.xml.

6. Change the view using Schema.xml.

Folder Structure

Let's look at the folder structure and the key elements of a template. All the folders and files for SharePoint are located under C:\Program Files\Common Files\Microsoft Shared\web server extensions\60.

The templates for SharePoint are stored under C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\TEMPLATE\1033.

Please note that all the folders related to SharePoint start with SPS. The folder that starts with STS is for Windows SharePoint Services (WSS). The differences between SharePoint Portal and WSS are beyond the scope of this article.

The template information is stored under these folders. The explanation of the elements under this folder will be explained later. There is one more folder called xml folder. This folder contains various xml files. But the main important xml file which ties all the elements together is WEBTEMPSPS.xml. This file has all the information that relates the template to its respective folder. If you open this file you will notice the following.

WEBTEMPSPS.xml

<Template Name="SPSTOC"
ID="30">
  <Configuration ID="0"
Title="Contents area Template" Type="0"
Hidden="TRUE" 
    ImageUrl="../images/spshome.gif"
Description="Area Template.">
      <!--
_locID@Title="webtemp_title_spstoc0" 
        _locComment="{StringCategory=HTX}"
-->
      <!--
_locID@Description="webtemp_desc_spstoc0" 
        _locComment="{StringCategory=HTX}"
-->
  </Configuration>
</Template>    

The Name attribute in the Template represents the folder name.  In the above code, SPSTOC is the folder under which you will find all the files related to the Contents Area Template.

So, when you assign a template to be used on the Sub Area in the following screen, the dropdown list box is populating information from this XML file.

 

Figure 1

FYI: SharePoint will pull information regarding templates from the xml files starting with WEBTEMPSPS*.xml.

Once SharePoint is aware of all the templates that are installed on your server, it has all the necessary information regarding the folders to be used to design the template page. For example, if you have selected Contents Area Template then it is aware that the template is pointing to SPSTOC folder.

The SPSTOC folder has the following folders.

DOCTEMP - This folder stores the file templates to be used for Document Library Templates.

LISTS - The default lists provided by SharePoint.

XML - The XML files that are used to design the pages.

Detailed explanation of each and every folder is beyond the scope of this article. But, when we start looking at building our own template, I will highlight some files under these folders.

The default.aspx page is the page which will be displayed when some sub area is created using this template.

Under XML folder, you will notice that there is one file called Onet.xml. Onet.xml file will assist SharePoint with building the default.aspx page and the contents list initially. We will look into the Onet.xml file when we are building the template example.

Building Your Own SharePoint Template

What Do You Need?

In order to start building your own template, you can either start from scratch or start from using a previously built template. For this article, I will just use an existing template - SPSTOC. SPSTOC template is for Contents Area. Also, you will have to decide on some unique description for your template.

The second information you need is the zones needed for the template and the locations of the zones. It's important to note that you are not restricted to having zones in specific areas. You can even have zones on the headers. For this article, I will create a zone in the header area on the left of Sub Area Title.

The next step is to decide what web parts will be included by default on the Templates page.

Finally, if you plan on displaying default lists that are provided by SharePoint, you need to decide the fields needed to be displayed by default.

New Template Folder

I will be calling my folder SPSArticle. Please note that for SharePoint templates you must follow the standard naming convention of starting the folder name with SPS.

I will copy SPSTOC and paste it under C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\TEMPLATE\1033.

Rename the Copy of SPSTOC folder to SPSArticle. The SPSArticle folder will now have 3 folders under it called DocTemp, List, and XML. It will also contain a default.aspx page.

Default.aspx Page

Default.aspx page is the page that will be used to build your sub area page.

Default.aspx page is similar to any page build in ASP.NET, except that the default.aspx page is inherited from Microsoft.SharePoint.Portal.WebControls.WebPartPage.

At the top, you will see some registration definitions of different Web User Controls.

<%@ Register Tagprefix="SharePoint"
Namespace="Microsoft.SharePoint.WebControls" 
  Assembly="Microsoft.SharePoint, Version=11.0.0.0,
Culture=neutral,
    PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="WebPartPages"
  Namespace="Microsoft.SharePoint.WebPartPages"
  Assembly="Microsoft.SharePoint,
Version=11.0.0.0, Culture=neutral, 
    PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SPSWC"
Namespace="Microsoft.SharePoint.Portal.WebControls" 
  Assembly="Microsoft.SharePoint.Portal,
Version=11.0.0.0, Culture=neutral, 
    PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SPSSUBWC" 
  Namespace="Microsoft.SharePoint.Portal.WebControls.Alerts"

  Assembly="Microsoft.SharePoint.Portal,
Version=11.0.0.0, Culture=neutral, 
    PublicKeyToken=71e9bce111e9429c" %>

If you have built some web part which you would like to add as static web part on this page, or, in other words, you would like to add a web part somewhere outside the web part zones, then you will need to register it as above, give it some TagPrefix, and use it as you would use a normal Web User Control. The SPSWC Web Control library has information on various SharePoint Web Controls used on the default.aspx page, including the Web Navigation Controls or Web Menus on SharePoint.

The remainder of the page is just an HTML page using different Web User Controls. I would like to highlight some key information related to Web Part zones before I proceed and add my own Web Part Zone to this default.aspx page. Please scroll to the Table Structure for Zones. The code will look similar to this.

<table border="0"
cellpadding="0" cellspacing="0" ID="ZoneTable" 
  width="100%" class="ms-tztable">

  <tr ID="TopRow" > 
    <td valign="top"
ID="TopCell" width="100%" colspan="2"
class="ms-tztop"> 
      <WebPartPages:WebPartZone
runat="server" AllowPersonalization="false" 
        ID="TopZone" Title="Top
Zone" Orientation="Vertical"/> 
        <!-- _locID@Title="L_TopZone_Title"
--> 
    </td> 
  </tr> 
  <tr ID="MiddleRow"> 
    <td valign="top"
ID="MiddleLeftCell" width="100%"
class="ms-tzmidleft"> 
      <WebPartPages:WebPartZone
runat="server" AllowPersonalization="false" 
        ID="MiddleLeftZone" Title="Middle
Left Zone" Orientation="Vertical"/>
        <!--
_locID@Title="L_MiddleLeftZone_Title" --> 
    </td> 
    <td valign="top"
ID="MiddleRightCell" width="0%"
class="ms-tzmidright">
      <div class="ms-tzmrdiv">
        <WebPartPages:WebPartZone runat="server"
AllowPersonalization="false" 
          ID="MiddleRightZone"
Title="Middle Right Zone" Orientation="Vertical"/> 
          <!--
_locID@Title="L_MiddleRightZone_Title" -->
      </div>
    </td> 
  </tr> 
  <tr ID="BottomRow"
class="ms-tzbottom"> 
    <td valign="top"
ID="BottomCell" width="100%" colspan="2">
      <WebPartPages:WebPartZone
runat="server" AllowPersonalization="false" 
        ID="BottomZone"
Title="Bottom Zone" Orientation="Vertical"/>
        <!--
_locID@Title="L_BottomZone_Title" --> 
    </td> 
  </tr> 
</table>

The above table structure defines the Zone table. The WebPartZone definition is as follows.

<WebPartPages:WebPartZone
runat="server" AllowPersonalization="false" 
  ID="MiddleLeftZone" Title="Middle
Left Zone" Orientation="Vertical"/>

The important properties that needs to be highlighted are:

AllowPersonalization - When set to false this zone cannot be altered in Personal View.

ID - Unique name given to the zone. This name is used in Onet.xml to add web parts to the zone by default.

Title - Some kind of description that will be displayed in design mode on SharePoint.

Orientation - This determines how new web parts are added to the web part zone. When set to Vertical, web parts are added vertically.

AllowCustomization - This property is not displayed in the code. When you do not want anyone to modify a specific zone, you can set this property to false. If the property is set to false, then no one can change anything in this web part zone in Shared View. This is extremely useful when you would like to add a web part during the creation of the template through Onet.xml.

Now, we will add one web part zone in place of the spsPageTitleIcon image. Find the <img element for spsPageTitleIcon and remove it.  We will add a new web part zone for users to add an image Web Part in place of spsPageTitleIcon image.

Add the following code in place of the img element.

<WebPartPages:WebPartZone
runat="server" AllowPersonalization="false" 
  ID="ImageZone" Title="Image Zone"
Orientation="Vertical"/>

With the above code, when you view this page in design mode, you will see the title area as follows.

Figure 2

Notice a new web part zone called Image Zone has been added at the top.

Summary about Default.aspx Page

You can do the following in the default.aspx page:

1. Add Web Part Zones.

2. Create look and feel of the template.

3. Add Web User Controls that needs be displayed as static.

Onet.xml

This file is located under the XML folder under your templates folder. Onet.xml file determines what contents are created when your sub area is created. This article will discuss the following elements in Onet.xml:

1. ListTemplates

2. Configurations

3. Modules

4. DocumentTemplates in case of Document Library only

Finally, in this section we will do the following:

1. Add multiple document libraries to the contents.

2. Add links list to the contents.

3. Display the document libraries in different Web Part Zones.

4. Display the links list in a Web Part Zone.

ListTemplates section in Onet.xml: This section explains the different lists to be used with the template. For the purpose of this article I will look at the Document Library list and Links list. Please note that anything in SharePoint is a list.

<ListTemplate Name="doclib"
DisplayName="Document Library" Type="101"
BaseType="1" 
  OnQuickLaunch="TRUE"
SecurityBits="11" 
  Description="Create a document library when
you have a collection of documents 
    or other files that you want to share.
Document libraries support features 
    such as sub-folders, file versioning, and
check-in/check-out." 
  Image="/_layouts/images/itdl.gif"
DocumentTemplate="101">
</ListTemplate>
<ListTemplate Name="favorite"
DisplayName="Links" Type="103" BaseType="0" 
  OnQuickLaunch="TRUE"
SecurityBits="11" 
  Description="Create a links list when you
have links to Web pages or other 
    resources that you want to share."
Image="/_layouts/images/itlink.gif">
</ListTemplate>

Things to note in the above code are the Name, DisplayName, and Type.

Name - Name is the folder name given to the list under the Lists folder under your template folder. The Document Libraries folder is called doclib, and the links folder is called favorite as shown above.

Display Name - This name is a unique name given to the list. In case you are going to have multiple document libraries, you create another entry of the same element and change the Display Name and Type.

Type - This is a unique identifier given to the list. You can assign your own identifiers to the list, but then you have to use that ID in the rest of the xml file.

Another important attribute to note under Document Library element is DocumentTemplate. There are different document templates that are supported in the document library. The ID attribute points to the exact DocumentTemplate element. DocumentTemplates is explained later.

I am going to make a few changes to the document library element to allow us to add multiple document libraries to my contents for the sub area.

Let's make a copy of the document library element and paste it below the original doclib entry. Your code will look like this.

<ListTemplate Name="doclib"
DisplayName="Document Library" Type="101"
BaseType="1" 
  OnQuickLaunch="TRUE"
SecurityBits="11" 
  Description="Create a document library when
you have a collection of documents 
    or other files that you want to share.
Document libraries support features 
    such as sub-folders, file versioning, and
check-in/check-out." 
  Image="/_layouts/images/itdl.gif"
DocumentTemplate="101">
</ListTemplate>
<ListTemplate Name="doclib"
DisplayName="Document Library 2" Type="1001" 
  BaseType="1" OnQuickLaunch="TRUE"
SecurityBits="11" 
  Description="Create a document library when
you have a collection of documents 
    or other files that you want to share.
Document libraries support features 
    such as sub-folders, file versioning, and
check-in/check-out." 
  Image="/_layouts/images/itdl.gif"
DocumentTemplate="101">
</ListTemplate>

Note that the first Document Library is as it is while I changed the DisplayName and Type for the second document library.

Configurations Section in Onet.xml - This section will allow us to define what lists are created on pages. You can have multiple ASPX pages under your template. You have to define the pages in the modules section of the Configurations Section. The typical Modules Section will look like this.

<Modules>
  <Module Name="Default" />
  <Module Name="WebPartPopulation"
/>
</Modules>

The lists for the default page are defined under the Lists section of the Configuration Section. Typical Lists section will be as follows.

<Lists>
  <List Title="Document Library" 
    Description="Provides a place to store
documents for this area." Type="101" 
    Url="Document Library"
QuickLaunchUrl="Document Library/Forms/AllItems.aspx">
    <!--
_locID@Title="L_DocumentLibrary_Title" -->
    <!-- _locID@Description="L_DocumentLibrary_Description"
-->
  </List>
  <List Title="Image Library" 
    Description="Provides a place to store
images for this area." Type="109" 
    Url="Image Library"
QuickLaunchUrl="Image Library/Forms/AllItems.aspx">
    <!-- _locID@Title="L_ImageLibrary_Title"
-->
    <!--
_locID@Description="L_ImageLibrary_Description" -->
  </List>
  <List Title="Site Template Gallery"
Type="111" Url="_catalogs/wt" 
    RootWebOnly="TRUE">
    <!--
_locID@Title="L_WebTemplateCatalog_Title" -->
  </List>
  <List Title="Web Part Gallery"
Type="113" Url="_catalogs/wp"
RootWebOnly="TRUE">
    <!--
_locID@Title="L_WebPartCatalog_Title" -->
  </List>
</Lists>

In the above scenario, you are creating Document Library list and Image Library List by default.

For this article, I am going to create another document library list called Article Document Library. Insert the following code before the Image Library list.

<List Title="Article Document Library"

  Description="Provides a place to store
article documents for this area." 
  Type="1001" Url="Document Library
2" 
  QuickLaunchUrl="Document Library
2/Forms/AllItems.aspx">
  <!--
_locID@Title="L_DocumentLibrary_Title" -->
  <!-- _locID@Description="L_DocumentLibrary_Description"
-->
</List>

Note the Type attribute is set to 1001 the list template we had created in the earlier section. With all the above changes done, you will have 2 document libraries under the Contents on the Sub Area as follows.

Figure 3

Finally, to create a link list, you will insert the following code under the Lists section

<List Title="Article Sites"
Description="Article Site Links" Type="103">
</List>

Note the Type is pointing to the ID defined in the ListTemplates section.

Modules section in Onet.xml - The modules section decides which web part displays on the different web part zones. Typical code will look like this.

<Modules>
  <Module Name="Default"
Url="" Path="">
    <File Url="default.aspx"
Type="Ghostable">
      <AllUsersWebPart
WebPartZoneID="TopZone" WebPartOrder="0"><![CDATA[
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
  <Assembly>Microsoft.SharePoint.Portal,
Version=11.0.0.0, Culture=neutral,
    PublicKeyToken=71e9bce111e9429c</Assembly>
  <TypeName>Microsoft.SharePoint.Portal.WebControls.CategoryDetail</TypeName>
  <Title>Area Detail Part</Title>
  <Description>Area Detail Part</Description>
  <PartOrder>0</PartOrder>
  <FrameType>None</FrameType>
  <AllowMinimize>true</AllowMinimize>
  <AllowRemove>true</AllowRemove>
  <IsVisible>true</IsVisible>
</WebPart>]]></AllUsersWebPart>
        <AllUsersWebPart
WebPartZoneID="TopZone" WebPartOrder="1"><![CDATA[
<WebPart
xmlns="http://schemas.microsoft.com/WebPart/v2">
  <Assembly>Microsoft.SharePoint.Portal,
Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
  <TypeName>Microsoft.SharePoint.Portal.WebControls.TOCPart</TypeName>
  <Title>Browse Topics By</Title>
  <Description>TOC Part</Description>
  <PartOrder>1</PartOrder>
  <FrameType>None</FrameType>
  <AllowMinimize>true</AllowMinimize>
  <AllowRemove>true</AllowRemove>
  <IsVisible>true</IsVisible>
  <NumberOfTopLevelColumns 
    xmlns="urn:schemas-microsoft-com:tocpart">2</NumberOfTopLevelColumns>
  <CategoryCutOffLength 
    xmlns="urn:schemas-microsoft-com:tocpart">20</CategoryCutOffLength>
  <ListingCutOffLength 
    xmlns="urn:schemas-microsoft-com:tocpart">20</ListingCutOffLength>
  <VerticalLayout
xmlns="urn:schemas-microsoft-com:tocpart">false</VerticalLayout>
  <HorizontallyAligned 
    xmlns="urn:schemas-microsoft-com:tocpart">false</HorizontallyAligned>
  <ShowRootListings 
    xmlns="urn:schemas-microsoft-com:tocpart">false</ShowRootListings>
  <ShowTitle
xmlns="urn:schemas-microsoft-com:tocpart">true</ShowTitle>
  <ShowListings
xmlns="urn:schemas-microsoft-com:tocpart">false</ShowListings>
  <ShowIcon
xmlns="urn:schemas-microsoft-com:tocpart">false</ShowIcon>
  <ShowDescription 
    xmlns="urn:schemas-microsoft-com:tocpart">false</ShowDescription>
</WebPart>]]></AllUsersWebPart>
    </File>
  </Module>
  <Module Name="WebPartPopulation"
List="113" Url="_catalogs/wp"
    Path="lists\wplib\dwp"
RootWebOnly="TRUE">
    <File Url="MSContentEditor.dwp"
Type="GhostableInLibrary" />
    <File Url="MSImage.dwp"
Type="GhostableInLibrary" />
    <File Url="MSPageViewer.dwp"
Type="GhostableInLibrary" />
  </Module>
</Modules>

Note that the WebPartZoneID is pointing to the ID Property on the WebPartZone control as defined in the default.aspx section above. For this article, I am going to display the document libraries defined and the link list on the default.aspx page.

I will display the document libraries on the left zone and the link list on the right zone.

Replace the above code for Default Module section as follows.

<Module Name="Default"
Url="" Path="">
  <File Url="default.aspx"
Type="Ghostable">
    <View List="103"
BaseViewID="0" 
      WebPartZoneID="MiddleRightZone"
WebPartOrder="0" />
    <View List="101"
BaseViewID="0" WebPartZoneID="MiddleLeftZone" 
        WebPartOrder="0" />
    <View List="1001"
BaseViewID="0" WebPartZoneID="MiddleLeftZone" 
        WebPartOrder="1" />
  </File>
</Module>

Note: To display the link list, I have set the List Property to 103, the ID defined in the ListTemplates section. Also, note that for Document Library, we have used the IDs 101 and 1001 as we have defined in ListTemplates section above. The WebPartZoneIds are pointing to the ID property defined in the default.aspx page. Also, the WebPartOrder determines the order in which  the web parts are displayed on the Page.

After completion of the above section, you will have a Sub Area as follows when you create a new Template.

Figure 4

Now you have two document Libraries, called Document Library and Article Document Library, and one link list called Article Sites.

DocumentTemplates in Onet.xml - I would like to mention this section for those readers who might be wondering how to create a document library for Excel Documents. The Document Templates section lists all the templates allowed for Document Library section. Please note the attribute documenttemplate in the listtemplate element refers to the Type Attribute in the DocumentTemplate element. Typical definitions for document template are as follows.

<DocumentTemplate DisplayName="Microsoft
Office Word document" Type="101" 
  Default="TRUE" Description="A
blank Microsoft Office Word document.">
  <DocumentTemplateFiles>
    <DocumentTemplateFile
Name="doctemp\word\wdtmpl.doc" 
      TargetName="Forms/template.doc"
Default="TRUE" />
  </DocumentTemplateFiles>
</DocumentTemplate>
<DocumentTemplate DisplayName="Microsoft
Office Excel spreadsheet" Type="103" 
  Description="A blank Microsoft Office Excel
document.">
  <DocumentTemplateFiles>
    <DocumentTemplateFile
Name="doctemp\xl\xltmpl.xls" 
      TargetName="Forms/template.xls"
Default="TRUE" />
  </DocumentTemplateFiles>
</DocumentTemplate>

Also, note that you can specify the document template file to be used for the document library. These document template files are stored under the doctemp folder under your templates folder.

Schema.xml

Before wrapping up this article, I would like to highlight one more file that is located under each and every lists folder. This schema.xml document has all the information required for displaying the lists to the user. Complete explanation of this file could take one full article, but I would like to demonstrate how you can change the default view of the document library. Going back to our previous image, by default the document library displays the Type, Name, and Modified By fields. I would like to change the default view to display the Created Date instead of the Modified By field.

First, I open the schema.xml file under doclib folder under my SPSArticle Template. Then, I find the BaseViewID="0". Please note this is the ID that was used to display the document library on the page by using this code.

<View List="103"
BaseViewID="0" WebPartZoneID="MiddleRightZone" 
  WebPartOrder="0" />

Under this view, I will find the ViewFields section. The section will look like this.

<ViewFields>
  <FieldRef
Name="DocIcon"></FieldRef>
  <FieldRef
Name="LinkFilenameNoMenu"></FieldRef>
  <FieldRef
Name="Editor"></FieldRef>
</ViewFields>

Based on the above code, the default view displays the Type, Name, and Modified By fields. Change the editor field to Created_x0020_Date. The code will look like this.

<ViewFields>
  <FieldRef
Name="DocIcon"></FieldRef>
  <FieldRef
Name="LinkFilenameNoMenu"></FieldRef>
  <FieldRef
Name="Created_x0020_Date"></FieldRef>
</ViewFields>

Now the screen should be similar to Figure 5.

Figure 5

WEBTEMPSPS*.xml

Now that all the files are in place, we need to let the SharePoint know that we have a new template for it to use. We can make a copy of WEBTEMPSPS.xml file and create a new file. I created a new file called WEBTEMPSPSARTICLE.xml. I inserted the following code.

<?xml version="1.0"
encoding="utf-8" ?>
<!-- _lcid="1033"
_version="11.0" _dal="1" -->
<!-- _LocalBinding -->
<Templates xmlns:ows="Microsoft SharePoint">
 <Template Name="SPSArticle"
ID="10001">
    <Configuration ID="0"
Title="Article area Template" Type="0"
Hidden="TRUE" 
      ImageUrl="../images/spshome.gif"
Description="Article Area Template.">
      <!--
_locID@Title="webtemp_title_spstoc0" 
        _locComment="{StringCategory=HTX}"
-->
      <!--
_locID@Description="webtemp_desc_spstoc0" 
      _locComment="{StringCategory=HTX}"
-->
    </Configuration>
 </Template>    
</Templates>

Make sure that the ID assigned to this template is above 10000.

Summary

In this article, you have learned the important aspects of creating a SharePoint Template with the help of examples.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 1:47:26 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search