【问题标题】:How to get items from SharePoint by Current User via REST or JSOM?当前用户如何通过 REST 或 JSOM 从 SharePoint 获取项目?
【发布时间】:2016-09-23 15:08:36
【问题描述】:

我有一个包含两列的 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());
    }

不,我需要在我的列表中的多用户字段中查询我当前用户属于人员字段的所有项目。我不知道如何查询这个。

谁能帮帮我?

【问题讨论】:

  • 这只是一个提示,CAML 查询包含... "" + "YourValue" + "/Value>";
  • 但这只能在带有 SPQuery 的服务器对象模型中使用?不是吗?
  • 不,你也可以在JSOM中使用它

标签: rest sharepoint sharepoint-2013 sharepoint-jsom


【解决方案1】:

我在 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多