Search This Blog

Saturday, July 8, 2017

SharePoint 2013: Showing Page Views within a SharePoint Page



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
    var clientContext;
    var siteCollection;
    var hitCount='';
    var siteCollUrl = '';
    var webHost = '';
    var isIE = false;
    var siteUrl = '';
    var date = '';
    var uname = '';
    var hitCountplaceholder = '#hitCountplaceholder';
    var listName = 'Statistics';
 
    $(document).ready(function () {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
            $(hitCountplaceholder).html('Count: ');
            SetPageVisitCount();
        });
    });

    function showProperties() {
        if (msieversion()) {
            alert($.browser.version);
            siteCollUrl = _spPageContextInfo.siteAbsoluteUrl + "/";
            webHost = siteCollUrl;
        }
        else {
            siteCollUrl = _spPageContextInfo.siteAbsoluteUrl;
            webHost = siteCollUrl + "/";
            alert(siteCollUrl);
        }

        SetPageVisitCount();
    }
    function SetPageVisitCount() {
        siteUrl = _spPageContextInfo.siteAbsoluteUrl;
        date = new Date();
        console.log(date);
        clientContext = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
        uname = clientContext.get_web().get_currentUser();
        clientContext.load(uname);
        var oList = clientContext.get_web().get_lists().getByTitle(listName);
        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItem = oList.addItem(itemCreateInfo);
        oListItem.set_item('Title', 'No Title');
        oListItem.set_item('url', siteUrl);
        oListItem.set_item('date', date);
        oListItem.set_item('uname', uname);
        oListItem.update();
        clientContext.load(oListItem);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }

    function onQuerySucceeded() {
        // alert('Item created: ' + oListItem.get_id());
        hitCount = oListItem.get_id();
        $(hitCountplaceholder).append(hitCount);
    }

    function onQueryFailed(sender, args) {
        console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

    function GetPageVisitCount() {
        appendString = '';
        $(hitCountplaceholder).append(appendString);
    }
</script>
<div id="hitCountplaceholder"></div>


MORE REFERENCES

http://spdeveloper.co.in/sharepoint2013/page-views-hit-counter-on-a-sharepoint-page.aspx



No comments:

Post a Comment