Blog posts - recent

MOSS 2007 / WCMS development - Overriding RenderFieldForDisplay from the classes RichHtmlField and RichImageField

Today it's time again for some MOSS 2007 / WCMS development... When implementing solutions for Microsoft Office SharePoint Server 2007 Publishing (WCMS) sometimes you have to inherit from a certain class and you have to override methods in order to extend the out of the box functionality. In my example I will extend the method RenderFieldForDisplay(HtmlTextWriter output) from the classes RichImageField and RichHtmlField. The method is used for rendering the field controls item value. In order to to this the object model offers

  • Microsoft.SharePoint.Publishing.WebControls
  • Microsoft.SharePoint.Publishing.Internal.WebControls

which can be found at:

  • Microsoft.SharePoint.Publishing.dll

Overriding RenderFieldForDisplay from the class RichImageField

When you want to override the RenderFieldForDisplay method of the class RichImageField you need to open the following namespaces:

using Microsoft.SharePoint.Publishing.WebControls

After opening the namespaces your class can inherit from RichImageField and override the methods of the class. In this example (which doesn't claims to be best practice) I capture the Html in the variable html of type string. Now you can modify the html by using string operators or regular expressions.

/// <summary>
/// Your summary...
/// </summary>
public class RichImageFieldOverride : RichImageField
{
    /// <summary>
    /// Your summary...
    /// </summary>
    /// <param name="output"></param>
    protected override void RenderFieldForDisplay(HtmlTextWriter output)
    {
        if (base.FieldName == null) { }
        if (base.FieldName == "") { }
 
        TextWriter tempWriter = new StringWriter();
        base.RenderFieldForDisplay(new HtmlTextWriter(tempWriter));
 
        string newHtml = tempWriter.ToString();
        //do what you like...
 
        output.Write(newHtml);
    }
}

First of all you can do some error handling stuff and after that you can get the Html be creating a new Textwriter object and getting the Html output from the base class. Now you can change the Html and at the end you can write the Html back to the output.

Registering the control

After compiling the code and putting the assembly in the Global Assembly Cache (GAC) you only need to register it at the top of your .aspx page:

<%@ Register TagPrefix="agl" Namespace="agl.Controls" Assembly="agl.Controls, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=****************"
%>

Of course you don't have to use the GAC if you have fulfill security requirements.

Using the control

After registering the control you can use it by putting the following line of code in your .aspx page:

<agl:RichImageFieldOverride ID="ID" FieldName="FieldName" 
runat="server"></agl:RichImageFieldOverride>

In edit mode there is no change since you have overridden the method RenderFieldForDisplay.

There is no change when you are in edit mode since you modified RenderFieldForDisplay. 

Overriding RenderFieldForDisplay from the class RichHtmlField

When you want to override the RenderFieldForDisplay method of the class RichHtmlField you need to open the following namespaces:

using Microsoft.SharePoint.Publishing.WebControls;
using Microsoft.SharePoint.Publishing.Internal.WebControls;

After opening the namespaces your class can inherit from RichHtmlField and override the methods of the class. In this example (which doesn't claims to be best practice) I capture the Html in the variable html of type string.

/// <summary>
/// Your summary...
/// </summary>
public class RichHtmlFieldOverride : RichHtmlField
{
    /// <summary>
    /// Your summary...
    /// </summary>
    /// <param name="output"></param>
    protected override void RenderFieldForDisplay(HtmlTextWriter output)
    {
        if (this.ItemFieldValue is string)
        {
            string html = this.ItemFieldValue.ToString();
            //do what you like...
 
            bool canCacheResults = true;
            output.Write(HtmlEditorInternal.ConvertStorageFormatToViewFormat(html, 
out canCacheResults));
            if (!canCacheResults)
            {
                base.CanCacheRenderedFieldValue = false;
            }
        }
    }
}

Again you can modify the html by using string operators or regular expressions like I have shown in the other example above.

Register control

After compiling the code and putting the assembly in the Global Assembly Cache (GAC) you only need to register it at the top of your .aspx page: 

<%@ Register TagPrefix="agl" Namespace="agl.Controls" Assembly="agl.Controls, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=****************"
%>

Of course you don't have to use the GAC if you have fulfill security requirements.

Use control

After registering the control you can use it by putting the following line of code in your .aspx page: 

<agl:RichHtmlFieldOverride ID="ID" FieldName="FieldName" 
runat="server"></agl:RichHtmlFieldOverride>

That's it for today with another MOSS 2007 / WCMS development article.

Comments

2/12/2009 8:05:54 PM #

Hi,

Please help. I have tried what you have written for a RichHTMLField and I simply can't understand what is wrong.

I simply don't get the edit content ability nor does it show the Page content but it

shows FieldName. The control I added has the properties for a RichHTMLField but it

does not behave as a RichHTMLField. I can't edit the page content as I wanted.

My aim was to show rounded corners. I tried the simple solution where I follow what

you have written and in the DisplayForField method I just add html = "<h1>I am here</h1>

Am I missing anything?

Kind regards,

Ayman

Ayman Osman Denmark

2/12/2009 11:02:27 PM #

Hi again,

I found out what I have done wrongly Smile

It was a wrong fieldname I refered to.

Ayman

Ayman Osman Denmark

2/13/2009 7:10:33 AM #

Hey that's great! Smile

I have read your comment yesterday night and wanted to check it out this morning...

Have a nice day,
Andreas

Andreas Switzerland

2/25/2009 8:08:04 AM #

Trackback from namics SharePoint Weblog

WCMS mit SharePoint (MOSS) 2007 – Teil 5

namics SharePoint Weblog

3/25/2009 2:41:59 PM #

That’s great, I never thought about Overriding RenderFieldForDisplay from the classes RichHtmlField and RichImageField like that before.

Jason Tailor United States

4/1/2009 3:14:12 PM #

Hi Andreas,

I try to create a custom field which extends the HTMLField and uses a control that extends RichHTMLField.
All, what this new custom field should do is to preset a few settings like "AllowTables = false" etc. (the same as you can configure in SharePoint Designer but I want to set these properties programmatically).
If I drag and drop the new custom field in SharePoint Designer it will be shown as it should (there is a previewbox with lorem-ipsum-text) but if I check in the template and try to use it in a new .aspx-page, there is no control to see, neither in edit-mode of the page direct nor on the edit-page for the properties. I also implemented the RenderFieldForDisplay-method like your listing above shows but without success. Do you have any idea, what I could have forgotten?

Kind regards,
Ricky

Ricky Germany

4/3/2009 3:06:05 PM #

Hi Ricky,

sounds really interesting what you are doing... but I have to admit that the only thing that comes to my mind is security. Maybe you are running SharePoint Designer with admin rights and maybe you have fewer rights in an aspx page. Or there might be a problem getting an object... something like SPContext.Current. But it's difficult to say...

Hope this helps a bit...
Andreas

Andreas Switzerland

Add comment

Your comment is not displayed until it is approved.




  Country flag

biuquote
Loading