Modify HitCounter script to Add to Favourites button

Forums Requests Modify HitCounter script to Add to Favourites button

Tagged: 

This topic contains 3 replies, has 2 voices, and was last updated by  Alexander Bautz 2 months ago.

  • Author
    Posts
  • #21664

    Brett
    Participant

    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
    Keymaster

    Hi,
    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":spjs.dffs.data.thisItemID,"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+"\")'>");
            }else{
                b.push("<input type='button' style='background-color:yellow;' value='Remove from favorites' onclick='toggleDocFav(\""+favRes.items[0].ID+"\",\""+currDocRes.EncodedAbsUrl+"\")'>");
            }
        }
        jQuery("#favBtnPlaceholder").html(b.join(""));
    }
    // Call function on load:
    getIsFavorite();
    
    function toggleDocFav(id,url){
        var res;
        if(!id){
            // add to fav
            res = spjs.utility.addItem({"listName":"Favorites","data":{"Title":url}});
            if(res.success){
                alert("Successfully added as favorite");
                getIsFavorite();
            }
        }else{
            res = spjs.utility.deleteItem({"listName":"Favorites","id":id});
            if(res.success){
                alert("Successfully removed from favorite");
                getIsFavorite();
            }
        }
    }

    I hope this gets you in the right direction.

    Best regards,
    Alexander

  • #21809

    Brett
    Participant

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

  • #21816

    Alexander Bautz
    Keymaster

    Thanks for the feedback – I’m glad it worked.

    Alexander

You must be logged in to reply to this topic.