SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

How to Add a Button to the HTML Editor Control in SharePoint

The HTML Editor Control (or rich text editor) in SharePoint 2007 is a pretty helpful addition to the product (assuming your users are using IE). If you would like to programmatically add your own buttons to the toolbar, you will most likely find only one or two articles from a Google search on the topic. Most links point to this page at the msdn:

How to: Add a Button to the HTML Editor Field Control

Unfortunately, I think this document is flawed in many ways. Because of this, the other day, I set out to actually get it working, and in the process I have simplified their example and have made it a bit more useful (hopefully). I'm not sure why their example is filled with syntax errors, contains unnecessary logic (example: always reading the second item in an array?!?!), and does not bother to reset the button to a usable state when they are done. Never the less, I think I have a basic understanding of how to use it. Note: I'm not guaranteeing this is the right way to do it, but it worked for me. Searching online and in the sdk for functions starting with RTE_ is pointless; Microsoft has left us out in the cold on this one.

My example doesn't do anything particularly useful, it just allows you to select a piece of text, click the newly created button, a javascript prompt appears asking for a url, and then whatever you type in that prompt is linkified in the html text area. Yes, I realize you can already make links in the out of the box control, but I had a specific need to bolt something extra onto a special link type... thus this customization was born.

The first thing you need to do to add your own button, is to edit the RTE2ToolbarExtension.xml file with SharePoint Designer. The file is located at:

"YOURSITE"/_catalogs/masterpages/Editing Menu/RTE2ToolbarExtension.xml

Much like the example tries to do (though is missing the closing tag), mine simply contains:

<?xml version="1.0" encoding="utf-8" ?>
<RTE2ToolbarExtensions>
     <RTE2ToolbarExtraButton id="extraButtonTestId"
        src="RTE2ToolbarExtraButtonTest.js" />
</RTE2ToolbarExtensions>

This tells SharePoint to look for my own Javascript file called RTE2ToolbarExtraButtonTest.js which is located on the server at:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\RTE2ToolbarExtraButtonTest.js

Since this file is on the server you will most likely need to remote to your SharePoint 2007 server to do any editing.

I have included the source of my new button’s Javascript code below. Hopefully it’s commented well enough that I don’t need to write up a walkthrough. I would give more explanation of what’s going on inside of it, but like I said, I couldn't find any documentation on the RTE_methods. When you save this file to the 1033 directory, you may need to cycle your web application pool before the changes are apparent (also, you may have to check in and publish your RTE2ToolbarExtension.xml file from SharePoint Designer). Once the files are created, simply go to your SharePoint site and Edit some page content with Internet Explorer. When you select some text, the rich text editor should appear and our new button will show. Clicking on the button will launch the javascript prompt and when you click Ok, it should linkify your selected text, and it should return the button state to clickable. See the attached screenshot:

 

 

RTE2ToolbarExtraButtonTest.js

------------------------------

 

//the function that is called on button click

function TestButtonOnClick(strBaseElementID, arguments) {

      //get the document that is being edited

      var docEditor=RTE_GetEditorDocument(strBaseElementID);

     

      //if nothing is being edited quit

      if (docEditor==null) { return; }

     

      //get the selected text

      var selection=docEditor.selection;

      var rngSelection=selection.createRange();

 

      //pop open a simple javascript prompt

      var reply = prompt("Please enter a valid URL:", "")

     

      //take the return of the prompt and set a link url to it and

      //set the label of the link to the selected text

      //rngSelection is our selected text, and we are using the

      //pasteHTML method to overwrite it with our link

 

      rngSelection.pasteHTML("<a target='_blank' href='"+reply+"'>"+rngSelection.htmlText+"</a>");    

     

      //restore the button state (make it clickable again)

      RTE_RestoreSelection(strBaseElementID);

     

      return true;

}

 

// The function that is called when the button's state is reset.

function TestButtonOnResetState(strBaseElementID, arguments) {

      //get the document that is being edited

      var docEditor=RTE_GetEditorDocument(strBaseElementID);

     

      //if nothing is being edited quit

      if (docEditor==null) { return; }

     

      //actually enable the button

            // Parameters:

            // 1 - Pass in the ID of the actual editor (which

            //     automatically is sent over on the reset call)

            // 2 - Boolean, True to turn it back on, False to

            //     keep it off

            // 3 - The name of the button to turn on

      RTE_TB_SetEnabledFromCondition(strBaseElementID, true, 'myCustomButton');

 

      return true;

}

 

// Register the toolbar button, which is necessary for the button

// to be displayed

      // Parameters:

      // 1 - What we want to call the button

// 2 - A reference to a url for the button icon (I picked an

//     existing one for simplicity)

      // 3 - What text we want to say next to the button

      // 4 - The tooltip for the button

      // 5 - The name of the function to call when it is clicked

      // 6 - The name of the function to call when the button is

      //     reset (or done being clicked)

 

RTE2_RegisterToolbarButton("myCustomButton", RTE_GetServerRelativeImageUrl("rtelnk.gif"), "Custom Button", "Custom Tooltip", TestButtonOnClick, TestButtonOnResetState);

 


 


Posted 05-07-2007 10:27 PM by mossman

Comments

Jonathon wrote re: How to Add a Button to the HTML Editor Control in SharePoint
on 02-23-2008 11:42 PM

Thanks for the article man, this is really helpful!  Just to note, I discovered there is a 7th argument on the RTE2_RegisterToolbarButton function that whatever object you pass you can get back as the second argument on both the [MyButton]OnClick or [MyButton]OnReset functions.

Integrating Radi with SharePoint and Dynamics CRM wrote Custom Actions in the SharePoint Rich HTML Editor Control with a custom Pop-up dialog
on 04-15-2008 6:52 PM

Me and a colleague of mine (Selvi) had to create a custom action button in the HTML Editor Control in

Sharepoint Nirvana wrote Create Expandable Regions in MOSS Publishing Pages
on 07-09-2008 8:59 AM

Create Expandable Regions in MOSS Publishing Pages

Tony Yuan wrote re: How to Add a Button to the HTML Editor Control in SharePoint
on 08-07-2008 2:33 PM

Why I cannot find this file?

"YOURSITE"/_catalogs/masterpages/Editing Menu/RTE2ToolbarExtension.xml

When create such a folder and file?  nothing shows up on the toolbar.  Does anyone has any idea?

Abdallah Mossad wrote re: How to Add a Button to the HTML Editor Control in SharePoint
on 08-17-2008 7:08 AM

Hi Tony,

you cant find that file RTE2ToolbarExtension.xml because you did not activate the publishing feature infrastructure (site collection feature) as well as the Publishing feature (site level feature).

Once you activate them you should be able to go to Sharepoint designer and open the site and locate the directory _catalogs/masterpages/Editing Menu/ there you will find the file.

Good Luck.

Melissa wrote re: How to Add a Button to the HTML Editor Control in SharePoint
on 09-21-2008 12:23 PM

Some of the out of the box toolbar buttons in my HTML Editor control do not work. For example, when I attempt to change the font size of my text, nothing happens. Any idea what is causing this and how to fix it?

Joe wrote re: How to Add a Button to the HTML Editor Control in SharePoint
on 10-27-2008 10:20 AM

Good article - thanks. I wonder if you know whether or not you can save multiple versions of the HtmlEditor.js file and then use these inline with a certain Group - eg. No text colour change for Group 'X'.

rüya tabiri wrote re: How to Add a Button to the HTML Editor Control in SharePoint
on 11-26-2008 11:24 AM

Thank you...

Husseiny wrote re: How to Add a Button to the HTML Editor Control in SharePoint
on 02-26-2009 1:40 PM

great article thanks,

I have a question please:

How can I modify the Reach Text Editor Tool bar of the Wiki page?

I would like to limite the Heading Size to 1-3 instead of 1-7 and limite the Fornts to specified  font,also Remove Button “Edit HTML Source”.

please I need your answer ASAP

Thanks

... wrote re: How to Add a Button to the HTML Editor Control in SharePoint
on 03-03-2009 12:05 PM

Gute Arbeit hier! Gute Inhalte.

Ziinou wrote re: How to Add a Button to the HTML Editor Control in SharePoint
on 06-17-2009 8:59 AM

Hi everyone! Thanks a lot for this topic...I'm having the same problem, I didn't find any other helpful script!! I don't know how to add a specific button : text wrapping for pictures! the HtmlEditor.js didn't help...especially that I'm not good at js

Please, can you  help me... I'm totally LOST!

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.