将列表项标记为在SharePoint 2013中查看/读取

时间:2013-12-31 22:55:04

标签: list sharepoint

当用户在SharePoint列表中查看项目时,我需要捕获它已被查看以及谁查看了该项目(在其他列中)。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

我通过添加脚本编辑器Web部件并在其中插入此脚本来完成此操作:

<script type="text/javascript" src="/jquery-1.10.2.min.js"></script>
<script src="/jquery.SPServices-2013.02a.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(loadConstants, "sp.js"); });    

function loadConstants() {  
    var userid= _spPageContextInfo.userId;
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
    var requestHeaders = { "accept" : "application/json;odata=verbose" };
    $.ajax({
      url : requestUri,
      contentType : "application/json;odata=verbose",
      headers : requestHeaders,
      success : onSuccess,
      error : onError
    });

    function onSuccess(data, request){
        var loginName = data.d.Title;

        var docurl = document.URL;
        var beginindex = docurl.indexOf('?ID=') + 4;
        var endindex = docurl.indexOf('&Source=');
        var itemid = docurl.substring(beginindex, endindex);

        var ctx = new SP.ClientContext("your website url");
        var oList = ctx.get_web().get_lists().getByTitle('your library name');
        this.oListItem = oList.getItemById(itemid); 

        this.oListItem.set_item('Read', loginName + '  ' + getTodayDate());
        this.oListItem.update();

        ctx.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded), 
            Function.createDelegate(this, this.onQueryFailed)
        );
    }
    function onError(error) {
        alert("error");
    }
    function getTodayDate() {
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();
        if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = mm+'/'+dd+'/'+yyyy;
        return today;
    }
}

</script>