mo.notono.us

Wednesday, October 03, 2007

MOSS: The ViewStyle Tag

Side note: I thought I had blogged on this before, but I can't find any evidence that I did...

One of the more daunting tasks of SharePoint development is modifying the schema.xml files of a List definition;  the CAML of the ViewHeader, ViewBody, ViewFooter, etc elements is tedious to wade through.  Granted, if you need a custom view, you can simply copy and paste from an existing view, then modify as needed, but that adds over 1600 lines of XML to your Schema file.  Not much fun.

Turns out there is a much simpler way:  If you don’t have any need for custom rendering of the View, you can use one of the standard styles defined in the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\GLOBAL\XML\VWSTYLES.XML file.  These are the same views that are accessible from the SharePoint UI under View – Modify This View  -  Style group.

To apply one of these styles, use the ID attribute of the style from the VWSTYLES.XML file and add a <ViewStyle ID=”x” /> element to your View in the schema file.  Then safely remove the ViewHeader, ViewFooter, ViewBody, ViewEmpty, GroupByHeader, GroupByFooter, PagedRowset, PagedClientCallbackRowset, and PagedRecurrenceRowset tags.  That's a lot of XML saved.

Internally, the SPView.ApplyStyle() method is then called, and any View styles defined in the Schema file are overridden by the ViewStyle definition.  (It is unknown how/if PagedClientCallbackRowset, PagedRecurrenceRowset, PagedRowset and other View child elements get their values set by the ViewStyle – that part of the code base is obfuscated.)

Labels: , ,

Tuesday, February 10, 2009

MOSS: The dreaded schema.xml

My colleague Tad sent me a link to Andrew Connell’s post: A Quicker Way to Create Custom SharePoint List Templates with the comment - ‘I guess “quicker” is relative.’  Tad’s right – this seems to be going too far for a simple schema.xml file.
Much of Andrew’s post is valid – there’s not much doing getting around the Content Types and the List Template (if you need them), and using the OOTB list interface plus Keutmann’s excellent SharePoint Manager tool  to build queries is a simple workaround, but I’m balking on his approach for creating the the List’s schema.  If you take Andrew’s approach, you’re gonna end up with a MINIMUM of 1600 lines of xml in your schema: my approach is far simpler; below is a fully functioning schema that’s only 30+ lines – see if you can spot the magic bullet…:
<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" Id="{AB426CDE-98F2-432A-B296-880C7931DEF3}"
     Title="Setting" Url="Lists/Setting" BaseType="0"
     FolderCreation="FALSE" DisableAttachments="TRUE" VersioningEnabled="FALSE"
     Direction="$Resources:Direction;"
     xmlns="http://schemas.microsoft.com/sharepoint/">
       <MetaData>
              <Fields>
                     <Field Type="Text" Name="Title" DisplayName="Name" Required="TRUE" />
                     <Field Type="Text" Name="Value" DisplayName="Value" Required="TRUE" />
              </Fields>
              <Views>
                     <View BaseViewID="0" Type="HTML" WebPartZoneID="Main" DisplayName="All Items" DefaultView="TRUE"
                         MobileView="True" MobileDefaultView="False" SetupPath="pages\viewpage.aspx"
                         ImageUrl="/_layouts/images/issues.png" Url="AllItems.aspx">
                           <ViewStyle ID="17"/>
                           <RowLimit Paged="TRUE">100</RowLimit>
                           <Toolbar Type="Standard" />
                           <ViewFields>
                                  <FieldRef Name="Edit" />
                                  <FieldRef Name="Title"/>
                                  <FieldRef Name="Value"/>
                           </ViewFields>
                           <Query>
                                  <OrderBy>
                                         <FieldRef Name="Title"/>
                                  </OrderBy>
                           </Query>
                     </View>
              </Views>
              <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>
              <DefaultDescription>Settings used in the application.</DefaultDescription>
       </MetaData>
</List>
Yep, it’s the ViewStyle tag I wrote about back in 2007.  It eliminates that 90% of the schema file that Andrew suggests you ignore, leaving a fairly terse xml that can actually be digested and debugged (sort of).

Labels: , ,