Question

I'm trying to access the rest API of Excel services in SharePoint online from a webjob in C#. I have a client secret and ClientId of an registered add-in and can make Rest request to normal lists and items on the site, but when I tries to access Excel services I get an 401. I can make requests in the Web client to Excel services without any problems.
I use the following code (more or less) and if I change the url to url = tmpURL + "/_api/web/lists/getbyTitle('Projekt')/Items" it works as a charm (so the accessToken should be fine).

                    String tmpURL = "https://XXXX.sharepoint.com/sites/SiteXXX";
                    string accessToken = GetAppOnlyAccessToken(tmpURL);
                    String url = tmpURL + "/_vti_bin/ExcelRest.aspx/fileFolder/fileName.xlsx/Model/Ranges('Sheet1!C8%7CG21')";
                    HttpWebRequest  endpointRequest =
                          (HttpWebRequest)HttpWebRequest.Create(url
                         );

                    endpointRequest.ContentType = "application/json;odata=verbose";
                    endpointRequest.Accept = "application/json;odata=verbose";
                    endpointRequest.Headers.Set("Authorization", "Bearer "+ accessToken);
                    endpointRequest.Headers.Add("IF-MATCH", "*");
       using (HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse())
        {
            using (StreamReader reader = new StreamReader(endpointResponse.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                Console.WriteLine(result);
            }
        }

What do I do wrong?

Was it helpful?

Solution

As per this document - Resources URI for Excel Services REST API

The Excel Services REST API applies to SharePoint 2013 and SharePoint 2016 on-premises. For Office 365 Education, Business, and Enterprise accounts, use the Excel REST APIs that are part of the Microsoft Graph endpoint.

From my understanding it simply doesn't work for SharePoint Onilne, you need to use MS Graph instead.
Here is the documents regarding MS Graph & Excel - Working with Excel in Microsoft Graph

Bad news that it looks like it's not possible to use Access token received for SharePoint for Excel resource. You need to create new Azure AD app, give required permissions and implement OAuth flow (Client Credentials flow since you are in a web job) in order to get an access token.

OTHER TIPS

As per my knowledge you can't call /_vti_bin/ExcelRest.aspx/fileFolder/fileName.xlsx/Model/Ranges('Sheet1!C8%7CG21') like particular cell data from Excel because Rest API will not read your inside Excel data.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top