Question

I have used this javascript function to like my publishing pages. The problem is that I can only like the page if I am at the current page since the javascript uses the _spPageContextInfo variable.

The thing I want to do is to like a page from i.e a news archive or a start page that lists a summary of the page, for example title, preamble, image and date.

How can I get the _spPageContextInfo from a list item, and then set that object in a .aspx page?

Here is an example how I think it would be:

Page layout:

<span class="LikeButtonImage" data-spPageContext="_spPageContextInfo"></span>

Like.js:

$(“.LikeButtonImage”).click(function () {
   var spPC = $(this).data("spPageContext");
   LikePage();
}

And in the code set the data attribute for each list item.

Appreciate your help!

Was it helpful?

Solution

_spPageContextInfo is available on any SharePoint page. But you need not only this object, but also ListID (Pages library id) and ItemID (page id). You have to get them from your server and put into data attributes. Then inside these javascript functions replace _spPageContextInfo.pageListId and _spPageContextInfo.pageItemId with attribute values.

By the way, how do you suppose to render these buttons? Maybe you can try to use List View Web Part + Client Side Rendering? It's very easy to implement custom likes with it.

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