0

I have a search result web part based on a display template I made up. I created an action to had items as favorites. This way they appear first in the search result web part and they have a full star icon. Other items have an empty star and appear after favorites items.

full star

and

empty star

How I made it:

All my items have a column "FavoriteMembers" which is a people picker with multiple selection. If current user is in the field then he have a full star, if not he have an empty star. I used a managed property to do it.

I manage to update items from display template to enable user to add or remove favorite on clic on the star. (It updates my item well, in real time)

My problem:

If user refresh his browser, item will keeps is old favorite status until the Shareoint search crawled the item again. In SharePoint Online, it takes between 15min and 6 hours (my observation and others).

The solution I'm trying to use:

CAML Query
I want to use a caml query to retrieve the "FavoriteMembers"column content in real time and update the star icon.

I already use CAML Query on other projects and managed to make it works. This time, it's the first time I'm trying to make it in a display template but query keep going in the failed part.

My Code:

function ListItems(){
    var clientcontext = new SP.ClientContext.get_current();
    var list = clientcontext.get_web().get_lists().getByTitle(currentItemList);
    clientcontext.load(list);

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Contains><FieldRef Name=\'Title\'/><Value Type=\'Text\'>'+ currentItemTitle +'</Value></Contains></Where></View>');
    var collListItem = list.getItems(camlQuery);
    clientcontext.load(collListItem);

    var currentUser = clientcontext.get_web().get_currentUser();
    clientcontext.load(currentUser);

    clientcontext.executeQueryAsync(function () {
        // some more script
    }, function () {
        console.log("alert");
    });
}

Any advice ? Can someone confirm I can use CAML Query in a display template ?

0

I didn't manage to make it work with CAML Query so I decided to use REST API.

Getting FavoriteMembers field value with REST API

$.ajax({
            url: siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?$filter=Title eq '" + title + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                var object = data.d.results;
                if (object[0].FavoriteMembersStringId!=null) {
                    var array = object[0].FavoriteMembersStringId.results;
                    for (var j = 0; j < array.length; j++ ) {
                        //if one id in the array is userID, then he has the item as favorite
                        if(array[j]==userID){
                            console.log(title + " is a favorite of " + name);
                            //Then input code to show a full star
                        }
                    }
                }
            },
            error: function (error) {
                console.log(JSON.stringify(error));
            }
            });

I had another problem when two users were updating the FavoriteMembers field at the same time. I managed to solve it the same way. If you need more information about this, feel free to contact me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.