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.
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.