Richard Szalay

Tuesday, October 17, 2006

MOSS 2007: Creating named templates

Named templates allow for templating the output of a supporting field control from a centralised template. It's not necessarily meant for html reuse on page layouts, but it would do the job. You'd really only want to use it if you had complex html being outputted by an image/text field and it the same html was used across multiple pages.

Like I mentioned in a previous post, field templates (named or inline) will not give you property-level access to content; meaning you can't access the alternate text or the url of a link by itself. With that warning aside, let's create a named template.

To create a named template called 'ImageCustomTemplate':

  1. Create a file named CustomTemplates.ascx in c:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES (you can call the ascx file anything you want, as long as it is .ascx)
  2. In CustomTemplates.ascx, create a RenderingTemplate with the ID of 'ImageCustomTemplate'. Here is an example:
    <%@ Control Language="C#"   %>
    <%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, 
        Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" 
        namespace="Microsoft.SharePoint.WebControls"%>
    <spwc:RenderingTemplate ID="ImageCustomTemplate" runat="server">
     <Template>
      [<spwc:FieldValue runat="server" />]
     </Template>
    </spwc:RenderingTemplate>
    
  3. Add a RichImageField to your page layout, assign the FieldName and give it a DisplayTemplateName of ImageCustomTemplate:
    <pwc:RichImageField runat="server" FieldName="Page Image" DisplayTemplateName="ImageCustomTemplate" />
    
  4. Restart the website application/application pool.

MOSS 2007: Background colour cannot be changed through CssRegistration

Thanks to the order in which CSS files are registered and then rendered by the CssLink control, our good friend core.css (/_layouts/1033/styles/core.css) will always override your background color with background-color:#FFFFFF; (line 7).

Usually I'd recommend using !important to keep the background, but unfortunately Internet Explorer 6- doesn't support it properly.

To resolve, it unfortunately means adding a manual <link /> or <style /> after the CssLink control.

Also, those behavior attributes (core.css: line 12, line 1973) won't pass validation so I hope they don't render to the end user. Don't make me come down there Microsoft.

MOSS 2007 WCM: Customising the output of field controls

I've been looking at SharePoint 2007 from a Web Content Management perspective, and the first thing I want to know is how do I customise the HTML output. When displayed to the end user, rendering is validating XHTML should not be made complicated.

Customising the layouts doesn't always mean the default output is invalid, just that's not not what you want. Best example is a heading TextField - you want to surround it in <h1></h1>, but ONLY if it has content - empty tags not only look messy, they cause rendering issues.

Having run into several other problems, I was determined to resolve this one.

And I did, but unfortunately no single fix covers everything. One of the most important things I have discovered is that the major field controls, like RichLinkField and RichImageField, do NOT store structured data; it's just HTML. This means that you can't have access to the URL portion of the field.

Below are the common field controls and a sample of modified output:

TextField, RichImageField and RichLinkField

<spwc:TextField runat="server" FieldName="Byline">
  <DisplayTemplate>
    <pwc:EditModePanel runat="server" SuppressTag="true" PageDisplayMode="Display">
      <h1><spwc:FieldValue runat="server" ControlMode="Display" /></h1>
    </pwc:EditModePanel>
  </DisplayTemplate>
</spwc:TextField>

Note the use of the EditModePanel to stop the inner FieldValue control from causing an exception when going into edit mode.

RichHtmlField

I currently have not figured out a way to template the output of the RichHtmlField. Should anyone else attempt this, I'll cover what I've tried/learned so far:

  • RichHtmlField ignores the DisplayTemplate (and Template) property.
  • The CustomTemplate property is ignored by most, if not all, field controls.
  • Internally, RichHtmlField overrides the RenderFieldForDisplay method and renders the HTML out directly, which explains why it ignores the template properties.
  • TextField throws an exception if assigned the FieldName of the RichHtmlField (even if the RichHtmlField is removed from the page).
  • RichLinkField renders nothing - most likely because the regular expressions it uses to parse the HTML internally aren't matching anything. This also applies to RichImageField as it is a subclass of RichLinkField.
  • FieldValue, like RichHtmlField, overrides the default rendering process itself - meaning that templates do not work with it.

Monday, October 16, 2006

MOSS 2007: Changing the default Welcome page of a site

I recently posted on the MOSS TechNet Forums on how I might change the default (WelcomeLinks.aspx layout) to something else.

A few more days of mucking around led me to the answer. Turns out the idea is that you save a template based on a site (remember: site = channel/web-folder, site collection = root channel/website) - which I had located previously (thanks Shane) - but I hadn't figured out how to create a new site from that template. Newly created templates are not automatically added into the list of site templates that can be used, and need to be added manually before they appear in the site template list.

Below are the steps that I posted on TechNet forums to create a new site template:

  1. Create a site
  2. Change the welcome template to the template you want to use.
  3. Configure the page-layouts and site templates you want it to access.
  4. Save the template (/site_name/_layouts/savetmpl.aspx) - remember to check the "save content" checkbox to save the default page.
  5. Go to settings of "section root" site of this site template (ie. you want child-sites of this site to use the newly created site template)
  6. Look and Feel > Page Layouts and Templates > Subsites can only use the following site templates
  7. Add the site template that you just created and, if you want, remove the default one.

Now if only someone could help me with my other queries, I'll be happy for now.

Monday, October 09, 2006

Visual Studio .NET 2003 will not be supported under Vista

Visual Studio .NET 2003 will not be supported under Vista. Sounds a bit harsh, doesn't it? It's true, though, as Somasegar recently confirmed on his blog.

While I think that's pretty weak effort (with the bugginess of VS.NET 2005), I think its pretty unlikely that any developer is not going to disable User Account Control anyway. Having said that, running an unsupported application allows Microsoft to ignore any problems you are having.

Let's hope all the negative feedback they are receiving makes them change their mind.