26 August 2011

Override the render function of the SharePoint content editor WebPart

Had you come across on Bug of the SharePoint Content Editor WebPart
So there is interesting problem
If using external links and internal links – you will get the bug
An example to the bug:
Try This - Internal links, external links and internal links again

What we get is the first two links work correctly, but the third link will link us to link the internal server name we have created the site. This is big bug for CEWP.
Hope you understood the bug.

How do we solve it? You can not inherit the WebPart - then shall we do?
Built Control Adapter performs render the code.
Create a small project in Visual Studio that contains the following code

using System;
using
 System.Collections.Generic;
using
 System.IO;
using
 System.Text;
using
 System.Web.UI;
using
 Microsoft.SharePoint;
using
 Microsoft.SharePoint.Administration;
namespace
 Roi.Moss.ControlAdapter.ContentEditorWebPart
{
  public class ContentEditorWebPartAdapter : System.Web.UI.Adapters.ControlAdapter
  {
    ///
 <summary>
    ///
 Override the render function off the CEWP
    ///
 </summary>
    ///
 <param name="writer">HtmlTextWriter write</param>
    protected
 override void Render(HtmlTextWriter writer)
    {
      //Create a new stringbuilder

      StringBuilder
 sb = new StringBuilder();
      //create a HtmlTextWriter

      HtmlTextWriter
 htw = new HtmlTextWriter(new StringWriter(sb));
      //Preform the base Render Method

      base
.Render(htw);
      //Get the output string

      string
 output = sb.ToString();
      //Create a list with the alternate access urls

      List
<Uri> alternateUrls = GetCurrentAlternateAccessMappingsUrls();
      //Read out the list with alternate access urls

      foreach
 (Uri alternateUrl in alternateUrls)
      {
        //Replace the url's with /

        output = output.Replace(alternateUrl.ToString(), "/");
      }
      //write the output to the page

      writer.Write(output);
    }
  }
}

The function GetCurrentAlternateAccessMappingsUrls has been showed last month.
Now pass the code to the GAC. On the folder of IIS which at App_Browsers folder.
For example C: \ Inetpub \ wwwroot \ wss \ VirtualDirectories \ 80 \ App_Browsers
Now you need to add the following code in a file compat.browser
<browsers>
  ...
  <
browser refID="Default">
    <
controlAdapters>      <adapter controlType="Microsoft.SharePoint.WebPartPages.ContentEditorWebPart"                 adapterType="Roi.Moss.ControlAdapter.ContentEditorWebPart.ContentEditorWebPartAdapter, Roi.Moss.ControlAdapter.ContentEditorWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3f86d098a47bbaa6" /&amp;gt;
    </
controlAdapters>
  </
browser>

  ...
</
browsers>

Hope I helped
Roi

No comments:

Post a Comment