1

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?

2 Answers 2

0

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.

2
  • Thank you to taking the time! It's really not the answer I wanted but probably the answer I needed. It is a bit annoying that it works in the browser but not as an webjob, this it probably due to sso and oauth and they does not want to spend the time making it work, because they got graph. Thanks one more time.
    – Olof
    Dec 16, 2016 at 8:18
  • Since it does work in browser... You can try to use some "elevated" account instead of OAuth (of course if it's an option for you). Consider following article - you can obtain cookie for particular user and then attach those cookie to your request to excel services later. Dec 16, 2016 at 8:21
0

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.

1
  • It does work in the browser. There I make Rest request (with typescript) to the Excel service and gets specific ranges. It works lite a charm there.
    – Olof
    Dec 14, 2016 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.