Calculate the View Count of a list item using Javascript

<script language="ecmascript" type="text/ecmascript">
var itemId=GetQueryStringParams('id');
SP.SOD.executeOrDelayUntilScriptLoaded(getSetListItem,'SP.js');
var listItem;
var list;
var clientContext;
function GetQueryStringParams(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++)
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == "ID")
        {
            return sParameterName[1];
        }
    }
}

        function getSetListItem() {
            this.clientContext = SP.ClientContext.get_current();
            if (this.clientContext != undefined && clientContext != null) {
                var webSite = clientContext.get_web();
                this.list = webSite.get_lists().getByTitle("test1");
                this.listItem = list.getItemById(itemId);
                clientContext.load(this.listItem);
                this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess),
Function.createDelegate(this, this.OnLoadFailed));
            }
        }


        function OnLoadSuccess(sender, args) {
            var value = this.listItem.get_item("Count");
            this.listItem.set_item("Count", (value  + 1) );
            this.listItem.update();
            this.clientContext.load(this.listItem);
            this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess1),
Function.createDelegate(this, this.OnLoadFailed));
        }

        function OnLoadSuccess1(sender, args) {
            alert(this.listItem.get_item("SampleTwo"));
        }

        function OnLoadFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }</script>

Comments

Popular posts from this blog

Activating a SharePoint Feature on Multiple Sites or Site Collections using PowerShell

Managed Path with WildCard and Explicit Inclusion

List All SharePoint 2010 PowerShell Commands