Modify HitCounter script to Add to Favourites button

Forums Requests Modify HitCounter script to Add to Favourites button


This topic contains 2 replies, has 2 voices, and was last updated by  Brett 10 hours, 36 minutes ago.

  • Author
  • #21664


    Hi Alexander,
    I’m trying to use your HitCounterforSharePoint solution as a Add to Favorites button for SP2013.
    This is for a document library, while viewing the displayform.aspx of the document, a person can click the Star image to add to their favorites rather than give it a star rating. Only 1x star is shown.
    The HitCounter list is their Favorites list, showing the Documents they Bookmarked.
    2x requests if you have time to assist please:
    1: I’ve got everything working except the part to unfavorite the document.
    Are you able to provide the extra javascript to remove the star rating from the HitCounter list?

    2: Also, can this solution be used in a calculated column in a list view?
    I did read in the comments that it captures the URL, so not sure if that will work or how much revamp would be needed to make it work?

    I understand SP2013 has built in Rating column feature but I believe there isn’t a list to then view all your favorites, which your solution does.

    Any help with this would be great.
    Thank you.

  • #21706

    Alexander Bautz

    You cannot use a calculated column in list view because the support for using HTML calculated columns (by specifying the output as number) no longer works (in O365 at least). Also, the list item is identified by the URL and you don’t have access to this in a calculated column.

    I don’t think the hit counter solution is a good start and think you should make it a bit simpler. I have written this little example to show a button in DispForm or EditForm.

    1: Add a list with DisplayName “Favorites” in your site. Only the Title field is used so you don’t have to add any new fields to this list.

    2: Add a HTML section to one of your tabs with this snippet:

    <div id="favBtnPlaceholder"></div>

    3: Add this to your Custom JS:

    function getIsFavorite(){
        var currDocRes, favRes, b = [];
        currDocRes = spjs.utility.getItemByID({"listName":_spPageContextInfo.pageListId,"id","viewFields":["ID","EncodedAbsUrl"]});
        if(currDocRes !== null){
            favRes = spjs.utility.queryItems({"listName":"Favorites","query":"<Where><And><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='User'>"+_spPageContextInfo.userId+"</Value></Eq><Eq><FieldRef Name='Title' /><Value Type='Text'>"+currDocRes.EncodedAbsUrl+"</Value></Eq></And></Where>", "viewFields":["ID"]});
            if(favRes.count === 0){
                b.push("<input type='button' style='background-color:green;color:white;;' value='Add to favorites' onclick='toggleDocFav(false,\""+currDocRes.EncodedAbsUrl+"\")'>");
                b.push("<input type='button' style='background-color:yellow;' value='Remove from favorites' onclick='toggleDocFav(\""+favRes.items[0].ID+"\",\""+currDocRes.EncodedAbsUrl+"\")'>");
    // Call function on load:
    function toggleDocFav(id,url){
        var res;
            // add to fav
            res = spjs.utility.addItem({"listName":"Favorites","data":{"Title":url}});
                alert("Successfully added as favorite");
            res = spjs.utility.deleteItem({"listName":"Favorites","id":id});
                alert("Successfully removed from favorite");

    I hope this gets you in the right direction.

    Best regards,

  • #21809


    HI Alexander,
    Your script works perfectly.
    Many thanks for your time and effort.

You must be logged in to reply to this topic.