Search code examples
restsharepointpostmanadal.jsmicrosoft-graph-files

Sharepoint list item using Api Rest


I need to get a list's items, so I created this function

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());
}

As a output I get 3000.

when I change the url to

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

I get an empty list!

PS :

It's work in Postman with no problem

The token is generated by adaljs :

Get Token

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

Adal config

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,
};

So I need to know :

  • what the reason fot this issue?

  • How can I fix it?


Solution

  • It's too general information, you need debug and figure out the detailed error information.

    My test demo:

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

    enter image description here