SharePoint Pals
 | Sign In
Insert a Button on the Edit Page Ribbon of a Publishing Page in SharePoint 2013
by Sathish Nadarajan 23 May 2014
SharePoint MVP
Today  :   3     Total  :    11261

I came across a very interesting requirement stating that, there should be a custom button on the Ribbon, when the user edits the publishing page. On Click of that, he should be able to insert a Link or an Image on the Rich Text Editor. i.e., on the content area of the publishing page. Just wanted to share with the community, how to achieve this.

As we saw, how to insert the buttons on the Ribbon on the previous articles here, let us not focus on them again. I request the readers to have a look on the above article if they have not come across before.

Hence, I am directly going to the Element.xml on my Module element.

 <?xml version="1.0" encoding="utf-8"?>
 <Elements xmlns="">
     Description="This Button will be used to Upload the Links, Images etc.,"
             Description="This group will contain the Button control"
             <Controls Id="UploadLinkControl">
           CommandAction="javascript:InsertLink(‘Link’,’ http://sathishserver:20000/sites/PublishingSite/Documents/Control_List.js’);
           function InsertLink(linkType, href) 
             alert('Test For InsertLink2'); 
             var fAllowRelativeLinks = false;
             if(linkType == 'Link')
               alert('Entered into First Condition : Link');
               if (IsSafeHrefAlert(href, fAllowRelativeLinks)) 
                 var rng = RTE.Cursor.get_range().$3_0;
                 var selectedText = RTE.Cursor.s_range.get_text();
                 var d = rng.ownerDocument;
                 var createA = d.createElement('a');
                 var createAText = d.createTextNode(selectedText);
                 createA.setAttribute('href', href);
                 SP.UI.UIUtility.insertAfter(createA, rng);
             else if(linkType == 'Image')
               alert('Entered into Second Condition : Image');
               var altText = 'Alternative Text For the Image';
               if (IsSafeHrefAlert(href, fAllowRelativeLinks)) 
                 var rng = RTE.Cursor.get_range().$3_0;
                 var selectedText = RTE.Cursor.s_range.get_text();
                 var d = rng.ownerDocument;
                 var a = d.createElement('img');
                 a.setAttribute('src', href);
                 a.setAttribute('alt', altText);
                 SP.UI.UIUtility.insertAfter(a, rng);
           else if(linkType == 'Text')
               alert('Entered into Third Condition : Text);
               var range = RTE.Cursor.get_range();
             var selection = range.parentElement();
             if (!selection) {
             var span = selection.ownerDocument.createElement('span');
             span.innerText = href;

In the above sample code, I am using three different types. One is a Link, Image and a Text.

We need to modify the code based on our requirement. I made all the three types in one method for demo purpose.

After deployment, the screen will looks like something below.


On Click of the UploadLink Button, users will be able to insert either an image or a link or a text.

This seems to be very straight forward right. Let us see some more interesting requirements on coming articles.

Note: The above code needs some modification by the time of actual implementation. To make it dynamic and reusable component.

Happy Coding.

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Twitter October 10, 09:26
How to Use Angular HTTP Services in SharePoint List -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation