2

I'm developing a custom SharePoint 2013 Web Part that contains a Rich Text field. I'm using the method described here to allow the Ribbon to be used for Rich Text editing.

This works for everything on the Format Text tab, but on the Insert tab half of the icons are disabled - see here: https://i.sstatic.net/qJhS0.png

I need to use the Embed Code button to insert some JavaScript, can anybody help get this enabled?

This is a duplicate of this question on stackoverflow

2
  • I think it is normal behavior because what will you do if web part is added in Rich Text Editor as it is going to be deleted after page refresh? are you going to save text any where? Commented Jul 29, 2015 at 6:52
  • @Kalpesh The text entered will save on submit - there is a Save button on the page that posts back and server-side code handles the saving of the test, and the loading of the text when the page refreshes. This functionality is working correctly, it's just that the ribbon won't let me Embed Code. Commented Jul 30, 2015 at 3:44

1 Answer 1

0

You can add RichText Editor using following way also and it will make Embed Code action enable in ribbon.

Designer

<%@ Register TagPrefix="SharePointPublishing" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  <SharePointPublishing:HtmlEditor ID="htmlEditor" runat="server" AllowHtmlSourceEditing="true" PopupEditorMode="true" UseIntranetMode="true"></SharePointPublishing:HtmlEditor>

Code

using Microsoft.SharePoint.Publishing.WebControls;
using Microsoft.SharePoint.WebControls;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;

namespace RichTextFieldInWP.VisualWebPart1
{
  [ToolboxItemAttribute(false)]
  public partial class VisualWebPart1 : WebPart
  {
    // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
    // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
    // for production. Because the SecurityPermission attribute bypasses the security check for callers of
    // your constructor, it's not recommended for production purposes.
    // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
    public VisualWebPart1()
    {
    }

    protected override void OnInit(EventArgs e)
    {

        base.OnInit(e);
        InitializeControl();

        SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
        if (ribbon != null)
        {
            ribbon.TrimById("Ribbon.EditingTools.CPEditTab.Layout");
            ribbon.TrimById("Ribbon.EditingTools.CPEditTab.EditAndCheckout");
        }
        htmlEditor.Field = new RichHtmlField();
        htmlEditor.Field.ControlMode = SPControlMode.Edit;
        htmlEditor.Field.EnableViewState = true;
        htmlEditor.Field.AllowReusableContent = false;
        htmlEditor.Field.MinimumEditHeight = "200px";
        this.Controls.Add(htmlEditor.Field);   
    }

    protected void Page_Load(object sender, EventArgs e)
    {      
    }
}
}

Page

Rich Text Editor with Embed Code enabled

Note that you need to add reference of following assemblies:

  1. Microsoft.SharePoint.Publishing.dll

  2. Microsoft.Web.CommandUI.dll

Hope it will help to you.

5
  • Thanks for this. It works for Embed Code, but for some reason my App Part and Web Part buttons are disabled. Would you know why? Commented Jul 31, 2015 at 6:12
  • Have you added your web part in Publishing page or Web Part page? Commented Jul 31, 2015 at 6:24
  • It's in a Blank Web Part Page Commented Aug 3, 2015 at 3:15
  • It should work as I have also checked in Blank Web Part Page. Can you try to create new publishing site collection and check in newly created site collection? Commented Aug 3, 2015 at 4:02
  • I've realised the issue - the Insert App/Web Part buttons are available only when Editing the Page (i.e., when the WebPartManager is in DesignMode). Would you know how to get it to work when the page is in Display mode? Commented Aug 4, 2015 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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