How To: overcome glitches with the standard field controls shipped with MOSS 2007

Some of our field controls shipped with MOSS show a quite ugly behaviour as they add an extra   entry after the field. Means an extra space.

This is especially ugly for the RichImageField control as it prevents two images to show up right beside each other. This article shows that this is actually a long known issue. But this article only shows a workaround which can be compared with the approach we used in CMS 2001 before we had custom placeholder controls: to hide the control in published mode, to read the content and then to modify it in the way we want to have it.

An approach in CMS 2002 would have been to create a custom placeholder for this. So only one single change and you can benefit from this anywhere on your site.

A similar approach can easily be implemented using a custom field control in MOSS.

A field control that would fix the problem discussed in this article would look like this:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint.Publishing.WebControls;
 
namespace StefanG.SharePoint.WebControls
{
    public class CorrectedRichImageField : RichImageField
    {
        protected override void RenderFieldForDisplay(HtmlTextWriter output)
        {
            // create a new tempWriter to consume the output from the base class
            TextWriter tempWriter = new StringWriter();
            base.RenderFieldForDisplay(new HtmlTextWriter(tempWriter));
 
            // capture the output of the base class and do the required adjustments
            string newHtml = tempWriter.ToString().Replace(“</span>&nbsp;”“</span>”);
 
            // write out the corrected html 
            output.Write(newHtml);
        }
    }
}

If you need information about how to implement such a field control please have a look at the relevant topic in the SDK:
http://msdn2.microsoft.com/en-us/library/aa981226.aspx

15 Comments


  1. Thanks for the tip. My team have started evaluating MOSS as a WCM platform and we’ve run into issues re accessibility and poor markup (tables being used instead of CSS, spacers, etc) when using the standard controls so this technique will come in very handy. Do you know if there are plans to provide web standards-friendly controls in future (e.g. via service pack or separate download)?

    Reply

  2. Hi Mark,

    the same question often came up with CMS 2002 and the anwer was usually that this should be addressed with custom placeholder controls.

    So I would not expect it. But if enough requirements from customers arrive through service requests it might be that this question will be evaluated again for a service pack in the future.

    Cheers,

    Stefan

    Reply

  3. Not the answer I wanted to hear but thanks for replying all the same 🙂

    Reply

  4. Oen question on RichImageFields: Is it possible to have a custom page layout with multiple PageImages on it?? I’ve tried many times, today, but couldn’t get it to work.

    FieldName was set to PublishingPageImage, ID was set to something sifferent for each instance.

    Page ‘renders’ correctly in Sharepoint Designer, and when edititing the page in Sharepoint I get to see the placeholders and I can set an image to it, but When I save, all Image-fields show the exect same image, regardless of how I set them…

    Any thoughts on this?

    Reply

  5. Hi Iskander,

    first you need to create a content type to be used by your page layout. The page layout is just the rendering window for the content type. For each field control you add you need to have an individual column in the content type.

    Then bind the different field controls to the different columns.

    Cheers,

    Stefan

    Reply

  6. Hi Stefan,

    My biggest issue with controls are the web parts that come out of the box or even custom built ones. The web part is wrapped with table tags. Has anyone tried css adapters for web parts?

    thanks,

    anabhra

    Reply

  7. i have a question.

    i want to repalce my own modified Datetime control instead of sharepoint original datetime control. is it possible?

    Regards,

    Sed Taha

    Reply

  8. Hi Sed,

    if this is on a page layout you can do this.

    Best would be to derive your control from the original one.

    Cheers,

    Stefan

    Reply

  9. Can you show an example of this being used in an aspx page?

    Reply

  10. Hi Ed,

    you can only use this in a page layout – like the original field controls comming with MOSS.

    You have to open the page layout using SharePoint Designer and then you have to replace the original RichImageField control with the CorrectedRichImageField in Html View.

    Also ensure to register the namespace of your DLL and class at the top.

    Have a look in the SDK for details.

    Cheers,

    Stefan

    Reply

  11. Does it mather what namespace you use? Do it have to end with .SharePoint.WebControls?

    Reply

  12. The namespace can be different.

    Just ensure that you add the correct namespace as a reference in your page layout.

    Reply

  13. We came across a random issue lately where a publishing image (RichImageField) does not render correctly

    Reply

  14. Thanks.

    And i use RenderFieldForDisplay for my custom render.

    DisplayPattern is hard to use for me.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.