Data View Web Part: Simple Case XSLT View

27 11 2009

The Data View Web Part is a very useful web part, especially as it allows you to easily customize the display using XLST.
Below is some sample XSLT showing a very simple view of items in a document library. The view shows documents using the Title column and creates a clickable link to the document concerned using the FileRef column.

<xsl>
  <xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
    <xsl:output method="html" indent="no"/>
    <xsl:template match="/">
      <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
      <div class="ms-vb" style="margin-top:5px;padding-left:20px;">
         <table border="0" cellpadding="0" cellspacing="0">
            <xsl:for-each select="$Rows">
               <tr class="ms-vb">
                  <td align="left" valign="middle" width="15px"><img src="/_layouts/images/square.gif" alt="" border="0" align="absmiddle" /></td>
                  <td align="left" valign="middle"><a href="{@FileRef}"><xsl:value-of select="@Title"/></a></td>
               </tr>
            </xsl:for-each>
         </table>
      </div>
    </xsl:template>
  </xsl:stylesheet>
</xsl>

Simple XSLT View for Data View Web Part





SharePoint Site Definition Template Names

23 11 2009

I’ve compiled a list of the template names of the most common OOTB Site Definitions. The list was generated by perusing the contents of the WEBTEMP files found in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\XML.

  • STS#0 – Team Site
  • STS#1 – Blank Site
  • STS#2 – Document Workspace
  • CMSPUBLISHING#0 – Publishing Site
  • BLANKINTERNET#2 – Publishing Site with Workflow
  • BLANKINTERNETCONTAINER#0 – Publishing Portal
  • WIKI#0 – Wiki Site
  • BLOG#0 – Blog Site
  • BDR#0 – Document Center
  • OFFILE#1 – Records Center
  • SPSPORTAL#0 – Collaboration Portal
  • SPSNHOME#0 – News Site
  • MPS#0 – Basic Meeting Workspace
  • MPS#1 – Blank Meeting Workspace
  • MPS#2 – Decision Meeting Workspace
  • MPS#3 – Social Meeting Workspace
  • MPS#4 – Multipage Meeting Workspace
  • SPSMSITE#0 – Personalization Site
  • SPSITES#0 – Site Directory
  • SPSREPORTCENTER#0 – Report Center
  • SRCHCEN#0 – Search Center with Tabs
  • SRCHCENTERLITE#0 – Search Center




Creating a SharePoint List Template using a Feature

23 09 2009

Building on the last post about creating a content type, in this post we’ll create a custom list template which uses that content type and deploy it using a feature.

If you’ve ever taken a look at the schema definition file for a list template, you may baulk at it’s size and complexity, in fact you can ignore most of this, creating a custom list template turns out to be relatively straightforward.

A list template comprises 2 files;

  1. The list template.xml file
  2. The list template schema.xml file

Create the list template.xml file and add it to your feature manifest file;

<?xml version="1.0" encoding="utf-8"?>
<Elements Id="8f1d5d09-6834-40b7-b245-c62c0811596e" xmlns="http://schemas.microsoft.com/sharepoint/">
	<ListTemplate Name="MyTestListTemplate"
		  DisplayName="TestList"
		  Description=""
		  BaseType="0"
		  Type="100"
		  OnQuickLaunch="TRUE"
		  SecurityBits="11"
		  Sequence="410"
		  Image="/_layouts/images/itgen.gif" />
</Elements>

Note the Name and BaseType attributes, we will create a subfolder with the same name as the Name attribute and place the schema.xml file there. The BaseType attribute indicates the list type from which this list templates inherits which in this case is the basic custom list.
Add the listtemplate.xml file to the feature manifest;

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="adc873b4-fe40-4169-8304-c9a1dd697c2b"
          Title="MySiteTypes"
          Description="Description for MySiteTypes"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Site"
          ActivateOnDefault="FALSE"
          ImageUrl="GenericFeature.gif"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="sitecolumns.xml"/>
    <ElementManifest Location="mycontenttype.xml"/>
    <ElementManifest Location="listtemplate.xml"/>
  </ElementManifests>
</Feature>

Now for the schema.xml file, create a subfolder in your feature named “MyTestListTemplate” and in that folder create a new XML file called schema.xml.
lt01
To populate this file you have some options;

  1. Create it manually (take a look at the huge amount of CAML!!)
  2. Copy the base Custom List schema.xml file and modify it
  3. Craft the list/views etc in SharePoint using the UI and export the template using SharePoint Manager or the SharePoint Solution Generator

Using one of these methods you should then modify your schema.xml file as follows;

Change the Name, Title and URL attributes;

<List Name="TestList" Title="TestList" Description="" Direction="0" BaseType="0"
		Url="Lists/TestList"
		FolderCreation="FALSE"
		EnableContentTypes="TRUE"
		Type="100"
		Id="8f1d5d09-6834-40b7-b245-c62c0811596e"
		xmlns="http://schemas.microsoft.com/sharepoint/">

The contenttypes section should contain only the content type(s) you’ve defined for this list;

	<ContentTypes>
		<ContentTypeRef ID="0x0100928100FAB05A4148BD6F4C8E6A716B40" />
	</ContentTypes>

To the Fields section add only the field definitions defined in the content types list in the above contenttypes section;

<Fields>
	<Field Type="Number" DisplayName="MyReference" Required="FALSE" Decimals="0" Commas="FALSE" Group="My Group" ID="{918BA6B8-60BC-417e-AEE5-F9E1A0A59144}" StaticName="MyReference" Name="MyReference" Customization="" SourceID="{6a05fd42-eda7-4fb7-9bfd-2a35ded20c24}" ColName="float1" RowOrdinal="0">
		<Default>0</Default>
	</Field>
	<Field Type="Choice" DisplayName="MyType" Description="Description of MyType" Required="FALSE" Format="RadioButtons" FillInChoice="FALSE" Group="My Group" ID="{1FDC83BE-D596-45e2-9DD3-AD3338798784}" StaticName="MyType" Name="MyType" Customization="" SourceID="{6a05fd42-eda7-4fb7-9bfd-2a35ded20c24}" ColName="nvarchar3" RowOrdinal="0">
		<Default>Person</Default>
		<CHOICES>
			<CHOICE>Person</CHOICE>
			<CHOICE>Place</CHOICE>
			<CHOICE>Thing</CHOICE>
		</CHOICES>
	</Field>
	<Field Type="Note" DisplayName="MyDetails" Description="Description of MyDetails" Required="FALSE" NumLines="5" RichText="FALSE" Sortable="FALSE" Group="My Group" ID="{AB31BEFA-A178-4a1e-B324-B73E9C15D5A8}" StaticName="MyDetails" Name="MyDetails" AllowHyperlink="TRUE" RichTextMode="Compatible" IsolateStyles="FALSE" AppendOnly="FALSE" Customization="" SourceID="{6a05fd42-eda7-4fb7-9bfd-2a35ded20c24}" ColName="ntext2" RowOrdinal="0">
		<Default>Unknown</Default>
	</Field>
</Fields>

Modify the ViewFields section of each View (there are 2 views defined by default, a HTML view and the AllItems view) to include the columns required;

	<ViewFields>
		<FieldRef Name="Attachments" />
		<FieldRef Name="LinkTitle" />
		<FieldRef Name="MyReference" />
		<FieldRef Name="MyType" />
		<FieldRef Name="MyDetails" />
	</ViewFields>

You might also want to use the LinkTitleNoMenu column which is a straight link column to the proprty page of the list item, rather than the LinkTitle column which includes the dropdown ECB menu.

If you have exported the schema.xml file using one of the methods mentioned, you should check that your View element(s) (except the view with BaseViewID of 0 zero) have the SetupPath attribute set correctly.

<View DefaultView="TRUE"
      Type="HTML" DisplayName="All Items" Url="AllItems.aspx" Level="1" BaseViewID="1" ContentTypeID="0x"
      SetupPath="pages\viewpage.aspx"
      ImageUrl="/_layouts/images/DECISION.GIF" WebPartZoneID="Main">
   ...
   ...
</View>

Finally check that the Forms section is as follows;

    <Forms>
      <Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
    </Forms>

Build and Deploy the feature to your SharePoint site and activate the MySiteTypes feature.
lt02
The new “Test List” list template will now appear in the Custom Lists section of a sites Create page as shown.
lt03
You can now create a sample list based on this template.
lt04





Creating a SharePoint Content Type using a Feature

8 09 2009

Following on from this post in which I created Site Columns using a feature, in this post we will create a Content Type based on these site columns.

For an introduction to Content Types see the MSDN site.

Building on the solution in the previous post, we will create a Content Type called “My Site Content Type” using the site columns created there.

Add a new XML file called mysitecontenttype.xml to the MySiteTypes folder as shown below.

sct01

Open the new XML file, delete it’s default content and drop in the following Content Type XML.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
	<ContentType ID="0x0100928100FAB05A4148BD6F4C8E6A716B40"
					 Name="My Site Content Type"
					 Description="My Site Content Type"
					 Group="My Group">
		<FieldRefs>
			<FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Required="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE" />
			<FieldRef ID="{918BA6B8-60BC-417e-AEE5-F9E1A0A59144}" Name="MyReference" Required="TRUE" />
			<FieldRef ID="{1FDC83BE-D596-45e2-9DD3-AD3338798784}" Name="MyType" Required="TRUE" />
			<FieldRef ID="{AB31BEFA-A178-4a1e-B324-B73E9C15D5A8}" Name="MyDetails" Required="TRUE" />
		</FieldRefs>
		<XmlDocuments>
			<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
				<FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
					<Display>ListForm</Display>
					<Edit>ListForm</Edit>
					<New>ListForm</New>
				</FormTemplates>
			</XmlDocument>
		</XmlDocuments>
	</ContentType>
</Elements>

You can create this XML in a few ways;

  1. Create the Content Type using the SharePoint UI and export it, using for example Andrew Connells STSADM commands.
  2. Extract the Content Type XML using SharePoint Manager.
  3. Create it manually.

If you use the first approach, ensure that the XmlDocument element has the NamespaceURI attribute shown, if you don’t, you may experience the same problem I had when I failed to do this, discussed here.

If you use SharePoint Manager, you will need to change the Field elements to FieldRef elements.

In either case you must ensure that the ID attributes have unique values. If you exported the content type from SharePoint to package it into a feature then you should certainly change the the content type ID attribute value.

Having created the Content Type, we now need to modify the Feature manifest file, feature.xml, to tell it about the Content Type. Open the feature.xml file and add an ElementManifest element, as shown below.

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="adc873b4-fe40-4169-8304-c9a1dd697c2b"
          Title="MySiteTypes"
          Description="Description for MySiteTypes"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Site"
			 ActivateOnDefault="FALSE"
			 ImageUrl="GenericFeature.gif"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="sitecolumns.xml"/>
    <ElementManifest Location="mycontenttype.xml"/>
  </ElementManifests>
</Feature>

Use the WSPBuilder Build and Deploy commands to build and deploy to SharePoint.

After the solution has been deployed, browse to your SharePoint Site Collection and go to Site Settings – Site Collection Features and activate the MySiteTypes feature.

sc05

Navigate to Site Settings – Site Content Types under the Galleries section and your new Content Type is ready for use.

sct02

So lets try it out.

  1. Create a new Custom List
  2. Navigate to the List Settings and choose Advanced Settings under General Settings.
  3. On the Content Types section, choose Yes for Allow Management of Content Types.
  4. Click OK.

sct03

  1. Now choose Add from existing content types

sct04

  1. Click Add
  2. Click OK

You’ll notice that the List Settings now includes the Content Types site columns in its Columns section.

sct05

Modify the default view to include those columns. Now when you add a new list item you will see the option to add a new “My Site Content Type”.

sct06

The form used to edit the Content Types columns is generated by SharePoint at runtime, but you can customize these or provide your own.

sct07





SharePoint Integer and Numeric Field (Site Column) Types

2 09 2009

One of the Field types available for defining Site Columns is Integer, another is Numeric. When you create a site column using the UI, you won’t see an option for creating a site column of type Integer.

However if you create a site column of type Integer and provision it into SharePoint using a feature, you won’t see it in the Site Columns gallery. Curious, if you then build a Content-Type using this Site Column and provision that into SharePoint, and say, create a list using this Content-Type, voila, at last you will see your “Integer” type Site Column.

It turns out that the base Integer field type is marked UserCreatable = FALSE in the fldtypes.xml file, which mean that you can’t create fields of this type using the UI, but they can still be included as part of a Content Type.

Ok so why might you want to create a field (Site Column) of type Integer. Lets say you want to create a Site Column to store 4 digit years.

e.g.      2009

Well the Numeric field type allows you to set the Decimals part to zero, so the UI won’t display 2009.0. Great, except that the UI will display the year value 2009 as 2,009 with embedded commas,  which as a year display format isn’t great. The Field element also has an attribute called COMMAS which is supposed to control whether to display values using commas.

But, this attribute seems to be ignored, even if you set this attribute to FALSE the value is still displayed using commas.

What to do? enter the Integer field type, it doesn’t suffer from this problem and as long as you only want to use it as part of a Content-Type it’ll work perfectly.