Question

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: http://i.stack.imgur.com/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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top