The out-of-the-box master pages included with SharePoint contain a series of promoted actions on the top-right corner of the page. These JavaScript-based actions are displayed as links and include functions such as FOLLOW and SHARE as shown in the following screenshot. The PromotedActions
delegate control allows us to add additional actions, but does not replace the existing actions.
In this recipe, we will add a custom promoted action that fires a JavaScript alert. Promoted actions could be used for a variety of purposes related to the page or site the user is currently viewing. For instance, a promoted action could be created to post a link to the page on social network sites.
We should have already created our Visual Studio project in the Creating a Visual Studio solution for custom delegate controls recipe of this chapter before starting this recipe.
Follow these steps to add a promoted action with a delegate control:
Controls
folder.public
, inherit from the UserControl
base class, and implement the IDesignTimeHtmlProvider
interface:public class CustomPromotedAction : UserControl, IDesignTimeHtmlProvider
GetDesignTimeHtml
method and return the HTML provided by the Render
method in a monitored scope using the following code:public string GetDesignTimeHtml() { using (new SPMonitoredScope("Code6587EN.Ch07.Controls.CustomPromotedAction::GetDesignTimeHtml")) { StringWriter writer = new StringWriter(CultureInfo.CurrentCulture); HtmlTextWriter writer2 = new HtmlTextWriter(writer); this.Render(writer2); writer2.Close(); return writer.ToString(); } }
Render
method and add a monitored scope as follows:protected override void Render(HtmlTextWriter writer) { using (new SPMonitoredScope("Code6587EN.Ch07.Controls.CustomPromotedAction::Render")) { } }
var iconUrl = SPUtility.GetThemedImageUrl("/_layouts/15/images/spcommon.png", "spcommon");
We will use this image to display an icon for our promoted action. You can use a different image and modify the CSS accordingly for positioning.
STYLE
element to provide the CSS styles that we will use to render our custom action as follows:writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css"); writer.RenderBeginTag(HtmlTextWriterTag.Style); writer.Write("#customPromotedActionIcon { height:16px; width:16px; position:relative; display:inline-block; overflow:hidden; background-image: url('" + iconUrl + "'), background-position: -218px -48px; }"); writer.RenderEndTag();
writer.AddAttribute(HtmlTextWriterAttribute.Id, "customPromotedAction"); writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-promotedActionButton"); writer.AddAttribute(HtmlTextWriterAttribute.Href, "#"); writer.AddAttribute("onclick", "alert('You clicked the promoted action.'),"); writer.RenderBeginTag(HtmlTextWriterTag.A);
SPAN
element to display our custom promoted action image:writer.AddAttribute(HtmlTextWriterAttribute.Class, "s4-clust ms-promotedActionButton-icon"); writer.AddAttribute(HtmlTextWriterAttribute.Id, "customPromotedActionIcon"); writer.RenderBeginTag(HtmlTextWriterTag.Span); writer.Write(" "); writer.RenderEndTag();
SPAN
element to contain the display text for our custom promoted action:writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-promotedActionButton-text"); writer.RenderBeginTag(HtmlTextWriterTag.Span); writer.Write("Custom Action"); writer.RenderEndTag();
writer.RenderEndTag();
Elements.xml
file of our new element, register our custom promoted action control with the PromotedActions
delegate control:<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Control Id="PromotedActions" Sequence="90" ControlClass="Code6587EN.Ch07.Controls.CustomPromotedAction" ControlAssembly="$SharePoint.Project.AssemblyFullName$"> </Control> </Elements>
CustomPromotedAction
(the name of the class for our custom control)$SharePoint.Project.AssemblyFullName$
Code6587EN.Ch07.Controls
(the full namespace for the class, without the name of the class itself)True
True
CustomPromotedAction
(the name of the class)Promoted actions in SharePoint provide a series of JavaScript-based actions for end users. The default actions include FOLLOW and SHARE. Adding a custom promoted action adds actions rather than replacing the existing ones. The promoted action in our recipe simply fires the JavaScript alert
function to display a message to the user.
3.22.61.218