【问题标题】:Sharepoint list item using Api Rest使用 Api Rest 的 Sharepoint 列表项
【发布时间】:2020-06-10 17:38:33
【问题描述】:

我需要获取列表的项目,所以我创建了这个函数

export function retrieveSPItems(spToken, alias) {
  var url = `{path_to_my_site}/_api/web/Lists/getByTitle('Briefs')/ItemCount`;
  var myHeaders = new Headers({
    Accept: "application/json;odata=nometadata",
    Authorization: spToken,
  });
  return fetch(url, {
    method: "get",
    headers: myHeaders,
  }).then((response) => response.json());
}

作为输出,我得到 3000。

当我将网址更改为

var url = `{path_to_my_site}/_api/web/Lists/getByTitle('Briefs')/Items`;

我得到一个空列表!

PS

在 Postman 中可以正常工作

token 由 adaljs 生成:

获取令牌

authContext.acquireToken(SP_BASE_URL, function (error, token){....})

Adal 配置

export const adalConfig = {
  tenant: CURRENT_TENANT,
  clientId: CURRENT_APP_ID,
  endpoints: {
    api: CURRENT_APP_ID,
    graph: GRAPH_BASE_URL,
    sharepoint: SP_BASE_URL,
  },
  cacheLocation: "localStorage",
  validateAuthority: true,
};

所以我需要知道:

  • 出现此问题的原因是什么?

  • 我该如何解决?

【问题讨论】:

    标签: rest sharepoint postman adal.js microsoft-graph-files


    【解决方案1】:

    信息太笼统,需要debug搞清楚详细的错误信息。

    我的测试演示:

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="Scripts/adal.js"></script>
        <script type="text/javascript">
    
            var authContext = null;
            var user = null;
    
            (function () {
                window.config = {
                    instance: 'https://login.microsoftonline.com/',
                    tenant: 'xxx.onmicrosoft.com',
                    clientId: '9afc37cb-x-x-x-xxx',
                    postLogoutRedirectUri: window.location.origin,
                    endpoints: {
                        graphApiUri: "https://graph.microsoft.com",
                        sharePointUri: "https://xxx.sharepoint.com/",
                    },
                        cacheLocation: 'localStorage' // enable this for IE, as sessionStorage does not work for localhost.
                };
    
                authContext = new AuthenticationContext(config);
                var isCallback = authContext.isCallback(window.location.hash);
                authContext.handleWindowCallback();
                //$errorMessage.html(authContext.getLoginError());
                if (isCallback && !authContext.getLoginError()) {
                    window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
                }
    
                user = authContext.getCachedUser();
                if (!user) {
                    authContext.login();
                }
                //authContext.acquireToken(window.config.clientId, function (error, token) {
                //    console.log('---');
                //})
                authContext.acquireToken(window.config.endpoints.sharePointUri, function (error, token) {
                    alert(token);
                    if (error || !token) {
                        console.log("ADAL error occurred: " + error);
                        return;
                    }
                    else {
                        var listUri = window.config.endpoints.sharePointUri + "sites/lee/_api/web/lists/GetByTitle('mylist')/items?$select=Title";
    
                        $.ajax({
                            type: "GET",
                            url: listUri,
                            headers: {
                                "Authorization": "Bearer " + token,
                                "accept": "application/json;odata=verbose"
                            }
                        }).done(function (response) {
                            console.log("Successfully fetched list from SharePoint.");
                            var items = response.d.results;
                            for (var i = 0; i < items.length; i++) {
                                console.log(items[i].Title);
                                $("#SharePoint").append("<li>" + items[i].Title + "</li>");
                            }
                        }).fail(function () {
                            console.log("Fetching list from SharePoint failed.");
                        })
                    }
                })
    
            }());
        </script>
    

    【讨论】:

    • 谢谢你的回复,但我得到了完全相同的结果
    猜你喜欢
    • 2019-06-06
    • 2016-01-08
    • 1970-01-01
    • 2023-04-06
    • 2014-10-26
    • 2015-12-20
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    相关资源
    最近更新 更多