如何通过REST或JSOM从当前用户获取SharePoint项目?

时间:2016-09-21 10:52:56

标签: rest sharepoint sharepoint-2013 sharepoint-jsom

我有一个包含两列的SharePoint列表:

users (type people, multiple values allowed)
responsible_department (type string)

我想从此列表中获取当前用户在ùsers`字段中的项目。该字段可以包含多个用户(允许多个用户)!

我目前能够获得当前用户:

    var currentUser;
    function init() {
        this.clientContext = new SP.ClientContext.get_current();
        this.oWeb = clientContext.get_web();
        currentUser = this.oWeb.get_currentUser();
        this.clientContext.load(currentUser);
        this.clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }

    function onQuerySucceeded() {
        console.log(currentUser.get_loginName());
    }

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

不,我需要查询列表中的mutli用户字段,查看当前用户属于people字段的所有项目。我不知道如何查询。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

我在MSDN上找到了一个解决方案,这对我有用:

<script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
 var siteURL = _spPageContextInfo.webAbsoluteUrl;
 var listname = "CustomList";
 var currentUserId=_spPageContextInfo.userId;
 var url = siteURL + "/_api/web/lists/getbytitle('" + listname + "')/items?$select=Title,PeopleField/ID&$filter=substringof('"+currentUserId+"',PeopleField/ID)&$expand=PeopleField/ID";
 $.ajax({
     url: url,
     method: "GET",
     headers: { "Accept": "application/json; odata=verbose" },
     success: function (data) {      
        var items = data.d.results;
        for(var i = 0; i < items.length;i++) {           
            var item=items[i];
            console.log(item.Title);
        }       
     },
     error: function (data) {       
     }
 });
});
</script>