In a WIKI site, I'd like to allow users to input <pre> tags.

My goal is to use a syntax highlighter to build a small KB for he IT team. How can I setup the rich editor to easily produces snippets like this :

<pre class="language-powershell">
$site = Get-SPSite http://url
$owner = $site.Owner

I'm using SP2010 enterprise, and the Enterprise Wiki template


For SharePoint 2010 you could create a custom action to add a button to the ribbon which opens a new page to paste you source code into. Once the user closes the window some Javascript wraps the code into the pre tags and inserts it at the position of the cursor.

UPDATE: You could use combination of the zoombldr.aspx page (used to edit the HTML source) and JavaScript code which is used to insert a hyperlink into a Wiki page. That's how I did it (even though mine was for 2007 those haven't changed a lot). Just add a button to the ribbon via custom action which pops up the zoombldr.aspx page and let the user paste the source into it. Once the page is closed the JavaScript grabs the content and insert it into a wrapped pre element.

UPDATE2: AFAIK there is no official SharePoint API to insert stuff into a rich-text field. However, here's a piece of JavaScript I just gobbled together (not tested!) to open a dialog and insert the returned code into the rich-text field:

function RTE_InsertSourceCode(strBaseElementID)
    var docEditor=RTE_GetEditorDocument(strBaseElementID);
    var currentContent=new Array;
    var variables=RTE_GetEditorInstanceVariables(strBaseElementID);
    var returnValue=showModalDialog(
        'dialogHeight: 550px; dialogWidth:500px; help: no; status:no; resizable: yes');
    if (returnValue != "")
        returnValue = "<pre class=\"language-powershell\">" + returnValue + "</pre>";
    var docTextRange=docEditor.body.createTextRange();
    if (RTE_UseDynamicHeightSizing(strBaseElementID))

All you have to do now is create your custom action and you should be all set.

For SharePoint 2007 you need to hack into the rich-text editor (note: works only in IE) function RTE_FullHtmlToolBarDefinitionFactory to add a button to the toolbar which then does the same as above.

I've been doing that some time ago for a SharePoint 2007 Blog and Wiki using the Google Code Prettifier project and it worked so far (with some issues in old IE 6/7 browsers, though).

  • I'm using SP2010 enterprise, and the Enterprise Wiki template. Is there any SP specific API to inject the html tag ? or should I use a standard div-editable insertion ? – Steve B Mar 16 '12 at 15:30
  • I've retagged your question if you don't mind – Sig Weber Mar 16 '12 at 17:06
  • Thanks for the sample code. I'll take a close look at this solution. – Steve B Mar 19 '12 at 8:36

I'd look into adding this to your webpart. It's lite weight, free, and fully customizable: http://ckeditor.com/

  • 2
    It might be helpful if you included some information/links about integrating ckeditor with SharePoint and how this would solve Steve's problem. – Kit Menke Mar 16 '12 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.