Following up on edit, display and new form

by Martin 8. juli 2009 10:49

Following up on Customizing DispForm, EditForm and NewForm

I've found out how to not override the RenderTemplates. "All" you have to do is:

  1. Create classes (in .cs-files) which overrides the classes, you want to customize
  2. Make sure to register both the assembly and the namespace in the ascx
  3. assign new ID's to the rendertemplates in your ascx

This is what I did in the ascx:

<%@ Control Language="C#" AutoEventWireup="false" %>
<%@Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@Assembly Name="Customer.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXX" %>
<%@Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>
<%@Register TagPrefix="SPHttpUtility" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.Utilities"%>
<%@ Register TagPrefix="wssuc" TagName="ToolBar" src="/_controltemplates/ToolBar.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBarButton" src="/_controltemplates/ToolBarButton.ascx" %>
<%@ Register TagPrefix="Customer" Assembly="Customer.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXX" Namespace="Customer.Controls.CustomControls" %>

<SharePoint:RenderingTemplate ID="MyListForm" runat="server">
 <Template>
 <div id="text">
  <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator=" " runat="server">
    <Template_RightButtons>
     <SharePoint:NextPageButton runat="server"/>
     <SharePoint:SaveButton runat="server"/>
     <SharePoint:GoBackButton runat="server"/>
    </Template_RightButtons>
  </wssuc:ToolBar>
  <SharePoint:FormToolBar runat="server"/>
  <Customer:EmployeeFieldIterator runat="server"/>
 </div>
 </Template>
</SharePoint:RenderingTemplate>

<SharePoint:RenderingTemplate ID="EmployeeDisplayCompositeField" runat="server">
 <Template>
  <h3><SharePoint:FieldLabel runat="server"/></h3>
  <p><SharePoint:FormField runat="server" /></p>
  <SharePoint:AppendOnlyHistory runat="server"/>
 </Template>
</SharePoint:RenderingTemplate>

<SharePoint:RenderingTemplate ID="EmployeeFormIterator" runat="server">
 <Template>
  <Customer:EmployeeCompositeField runat="server"/>
 </Template>
</SharePoint:RenderingTemplate>

<SharePoint:RenderingTemplate ID="EmployeeCompositeField" runat="server">
 <Template>
  <h3><SharePoint:FieldLabel runat="server"/></h3>
  <p><SharePoint:FormField runat="server"/></p>
  <p><SharePoint:FieldDescription runat="server"/></p>
  <SharePoint:AppendOnlyHistory runat="server"/>
 </Template>
</SharePoint:RenderingTemplate>

I've made the overriden classes in one .cs (not very best-practice, but it does the job!):

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.HtmlControls;

namespace Customer.Controls.CustomControls
{
 public class EmployeeFieldIterator : ListFieldIterator
 {
  protected override string DefaultTemplateName
  {
   get
   {
    return "EmployeeFormIterator";
   }
  }
 }

 public class EmployeeDisplayCompositeField : CompositeField
 {
  protected override string DefaultTemplateName
  {
   get
   {
    if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit || SPContext.Current.FormContext.FormMode == SPControlMode.New)
     return "EmployeeCompositeField";
    else
     return "EmployeeDisplayCompositeField";
   }
  }
 }

 public class EmployeeCompositeField : CompositeField
 {
  protected override string DefaultTemplateName
  {
   get
   {
    if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit || SPContext.Current.FormContext.FormMode == SPControlMode.New)
     return "EmployeeCompositeField";
    else
     return "EmployeeDisplayCompositeField";
   }
  }
 }
}

Notice the only thing I do is to ovevrride the DefaultTemplateName-property and return the ID of the RenderTemplate in the ASCX. Fairly easy.

If you expirience problems with the above, try searching through your latest SharePoint Logs in the 12-hive. That helped me!

Happy SharePointing!

Tags: , , , , ,

MOSS

Kommentarer (1) -

Sophie McGough
Sophie McGough United States
21-07-2011 23:18:35 #

I am trying to create a new template file.  Rather than overriding the template for all the site I am trying to create a new one and referencing that template for that list.  Can you help?

Besvar

Tilføj kommentar

  Country flag

biuquote
  • Kommentar
  • Eksempel
Loading

Page List

About

Take a look at my LinkedIn-profile.

DISCALIMER: The content on this blog is in NO way affiliated with my employer. The opinions expressed on this blog are solely my own and in NO way endorsed by my employer.

Recent comments

Comment RSS

Month List