OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

Show the Share Action on your Page Layouts

Page Layout Sharing Social
post

This post is over a year old, some of this information may be out of date.

A question I received a couple of days ago was if it would be possible to display the share action on a page like the action in the context menu of that page.

Show image Share Action
Share Action

Solution

The solution for this is really simple, all you need to do is creating a hyperlink on the page layout and use a JavaScript function call in the href attribute for the click event.

<a href="javascript:sharePage()" title="Share page">Share this page</a>

The function behind this call looks like this:

function sharePage() {
EnsureScriptFunc("sharing.js", "DisplaySharingDialog", function () {
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var listId = _spPageContextInfo.pageListId;
var itemId = _spPageContextInfo.pageItemId;
if (typeof webUrl !== "undefined" && typeof listId !== "undefined" && typeof itemId !== "undefined") {
DisplaySharingDialog(webUrl, listId, itemId.toString());
}
});
}

This function doesn’t require a lot of code. You’ll just need to be sure that the DisplaySharingDialog function is available, and pass through the following parameters:

  • The current URL of the site;
  • The list ID of the current page;
  • The item ID of the current page. As you can see in the code, these things can be resolved from the _spPageContextInfo object.

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code