0

I am new to SharePoint and using 2013 classic version. I have created a website and in it, it has list with items. So, I need to trace the number of clicks of each item individually. My code is not working for tracing the number of clicks as the count gets stopped with 1 and doesn't increment then for individual item. But it counts overall clicks of all items in that list. I'm stuck with this for two weeks. Please help me and Many many thanks in advance.

function Getprevcount(linkname) {

    var resturl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('abc')/items?$top=1000";

    $.ajax({

        url: resturl,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },

        success: function (data) {
           var lnt = data.d.results.length;
            for (var i = 0; i < lnt; i++) {
                if (data.d.results[i].Title == linkname) {
                    prevcount = data.d.results[i].Count;
                }
            }
        },
        error: function (data) {
        console.log(data.responseJSON.error);
        }
    });
}

   function Updatecount(updatlink) {

    var resturl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('abc')/items?$select=Title&$filter=Name";
    //alert(resturl);
    prevcount = prevcount + 1;
    //alert(prevcount);

    $.ajax({

        url: resturl,
        method: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose",
            "IF-MATCH": "*",
            "X-HTTPS-Method": "MERGE"
        },

        data: "{__metadata:{'type':'SP.Data.TestcounterListItem'}, Count:prevcount}",

        async: false, success: function (data) {
            alert("I'm in Update Count Section");
            console.log(prevcount);
        },
        error: function (data) {
        console.log(data.responseJSON.error);
        }
    });
}
0

I assume your code is embeded in the DispForm.aspx page:

// function to retrieve the parameters from the URL
var getUrlParameter = function getUrlParameter(sParam) {
  var sPageURL = window.location.search.substring(1),
      sURLVariables = sPageURL.split('&'),
      sParameterName,
      i;

  for (i = 0; i < sURLVariables.length; i++) {
    sParameterName = sURLVariables[i].split('=');

    if (sParameterName[0] === sParam) {
      return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
    }
  }
}

var itemID = getUrlParameter('ID');
// get the number of views for this item from the current list
// so I filter the list to only get the current item
var resturl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Demo')/items("+itemID+")";
$.ajax({
  url: resturl + "?$select=Count",
  method: "GET",
  headers: { "Accept": "application/json; odata=verbose" },
  success: function (data) {
    var type = data.d.__metadata.type;
    var count = data.d.Count*1;
    count++;
    // we now update the item with the new count
    $.ajax({
      url: resturl,
      method: "POST",
      headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "Content-Type": "application/json;odata=verbose",
        "IF-MATCH": "*",
        "X-HTTP-Method": "MERGE"
      },
      data: JSON.stringify({
        __metadata:{
          'type':type
        },
        'Count':count
      }),
      success: function (data) {
        alert("View Count Updated to "+count);
      },
      error: function (data) {
        console.log(data.responseJSON.error);
      }
    });
  },
  error: function (data) {
    console.log(data.responseJSON.error);
  }
});

 

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.