How to use a single PageLayout for View and Edit Mode in SharePoint 2013

Sathish Nadarajan
 
Solution Architect
April 1, 2015
 
Rate this article
 
Views
14281

In one of the older Article, we saw how to create a PageLayout using Visual Studio. A small add-on to that article is the one which we are going to see now.

I request the readers to read that article first and then reading this one is a worth. Moreover, here we are not going to discuss elaborately. Just a continuity of the older one as recently I met with this kind of requirement.

Basically a Layout which should be used for both the View and Edit Mode. The user will be editing the PublishingPageContent from this layout. As soon as they save the page, then the PublishingPageContent should be rendered on the same page.

For that, there is nothing much deviation from the older one. But the only thing is EditModePanel.

The aspx file would be something as below.

 <%@ Page Language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
 
 <%@ Register TagPrefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 <%@ Register TagPrefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 <%@ Register TagPrefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 <asp:content contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server">
 	<SharePointWebControls:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/~language/Themable/Core Styles/pagelayouts15.css %>" runat="server"/>
 	<PublishingWebControls:EditModePanel runat="server">
 		<!-- Styles for edit mode only-->
 		<SharePointWebControls:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/~language/Themable/Core Styles/editmode15.css %>"
 			After="<% $SPUrl:~sitecollection/Style Library/~language/Themable/Core Styles/pagelayouts15.css %>" runat="server"/>
 	</PublishingWebControls:EditModePanel>
 	<SharePointWebControls:FieldValue id="PageStylesField" FieldName="HeaderStyleDefinitions" runat="server"/>
 </asp:content>
 <asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
 	<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
 </asp:Content>
 <asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
 	<SharePointWebControls:FieldValue FieldName="Title" runat="server"/>
 </asp:Content>
 <asp:Content ContentPlaceHolderId="PlaceHolderTitleBreadcrumb" runat="server"> 
     <SharePointWebControls:ListSiteMapPath runat="server" SiteMapProviders="CurrentNavigationSwitchableProvider" RenderCurrentNodeAsLink="false" PathSeparator="" CssClass="s4-breadcrumb" NodeStyle-CssClass="s4-breadcrumbNode" CurrentNodeStyle-CssClass="s4-breadcrumbCurrentNode" RootNodeStyle-CssClass="s4-breadcrumbRootNode" NodeImageOffsetX=0 NodeImageOffsetY=289 NodeImageWidth=16 NodeImageHeight=16 NodeImageUrl="/_layouts/15/images/fgimg.png?rev=23" HideInteriorRootNodes="true" SkipLinkText="" />
 </asp:Content>
 <asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
 	<div class="article article-body">
 		<PublishingWebControls:EditModePanel runat="server" CssClass="edit-mode-panel title-edit">
 			<SharePointWebControls:TextField runat="server" FieldName="Title"/>
 		</PublishingWebControls:EditModePanel>
 		<div class="article-content">
 			<PublishingWebControls:RichHtmlField FieldName="PublishingPageContent" HasInitialFocus="True" MinimumEditHeight="400px" runat="server"/>
 		</div>
 		
 	</div>   
 	 
 </asp:Content>
 

Happy Coding.

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Insert a Button on the Edit Page Ribbon of a Publishing Page in SharePoint 2013

Sathish Nadarajan
 
Solution Architect
May 23, 2014
 
Rate this article
 
Views
17904

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="http://schemas.microsoft.com/sharepoint/">
 
   <CustomAction
     Description="This Button will be used to Upload the Links, Images etc.,"
     Id="UploadLinkRibbonAction"
     Location="CommandUI.Ribbon">
     <CommandUIExtension>
       <CommandUIDefinitions>
         <CommandUIDefinition
           Location="Ribbon.EditingTools.CPInsert.Groups._children">
           <Group
             Id="UploadLinkRibbonActionGroup"
             Sequence="80"
             Description="This group will contain the Button control"
             Title="UploadLink"
             Template="Ribbon.Templates.Flexible2">
             <Controls Id="UploadLinkControl">
               <Button
                 Id="UploadLinkButton"
                 Command="UploadLinkButtonCommand"
                Image32by32="_layouts/15/images/Temp.32x32.png"
                 LabelText="UploadLink"
                 TemplateAlias="o1"
                 Sequence="10"/>
             </Controls>
           </Group>
         </CommandUIDefinition>
         <CommandUIDefinition
           Location="Ribbon.EditingTools.CPInsert.Scaling._children">
           <MaxSize
             Id="CustomEditRibbonActionsOneMaxSize"
             Sequence="15"
             GroupId="UploadLinkRibbonActionGroup"
             Size="LargeLarge"/>
         </CommandUIDefinition>
       </CommandUIDefinitions>
       <CommandUIHandlers>
         <CommandUIHandler
           Command="UploadLinkButtonCommand"
           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();
                 RTE.Cursor.get_range().deleteContent();
                 var d = rng.ownerDocument;
                 var createA = d.createElement('a');
                 var createAText = d.createTextNode(selectedText);
                 createA.setAttribute('href', href);
                 createA.appendChild(createAText);
                 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();
                 RTE.Cursor.get_range().deleteContent();
                 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();
             range.deleteContent();
             var selection = range.parentElement();
             if (!selection) {
                 return;
             }
             var span = selection.ownerDocument.createElement('span');
             span.innerText = href;
             range.insertNode(span);
             RTE.Cursor.get_range().moveToNode(span);
             RTE.Cursor.update();
         
             }
 
             
           }
             
           
           
           "/>
       </CommandUIHandlers>
     </CommandUIExtension>
 
   </CustomAction>
 </Elements>
 

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.

image

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment