1

I just want to access an excel document in SharePoint, which is possible through excel services. I've written the following code for that

but still it is giving me error of 403 forbidden.

My code:

         using System;
         using System.Collections.Generic;
         using System.Security;
         using System.Net;
         using System.Runtime.Serialization;
         using System.Runtime.Serialization.Json;
         using Microsoft.SharePoint.Client;

        namespace ConsoleApplication1
        {
          class Program
          {
            static void Main(string[] args)
            {
              try {
                //Console.WriteLine("Enter the sharepoint online UserName...");
                String username = "username";
                const string password = "password";
                SecureString pass = new SecureString();
                foreach (char itemC in password.ToCharArray())
                {
                    pass.AppendChar(itemC);
                }
                //pass.MakeReadOnly();
                Console.WriteLine();
                Console.WriteLine(pass);
                SharePointOnlineCredentials cred = new SharePointOnlineCredentials(username, pass);
                Console.WriteLine();
                Console.WriteLine("Requesting the REST...");
                String url ="https://serverhosr_url/sites/sitename/_vti_bin/ExcelRest.aspx/Shared%20 Documents/Template.xlsb/Model/Ranges?$format=json";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Credentials = cred;
            req.Headers["X-FORM_BASED_AUTH_ACCEPTED"] = "f";
                Console.WriteLine(req.ToString()); 
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RangeResponse));
            RangeResponse rr = ser.ReadObject(response.GetResponseStream()) as RangeResponse;
               Console.WriteLine("the response is as follows.."+rr.rows[0][0].fv);
               Console.ReadLine();
               }
               catch (NotSupportedException) { }
              }
            }
            [DataContract]
            public class CellValue
            {
             [DataMember]
              public object v { get; set; }
             [DataMember]
             public object fv { get; set; }
            }
             [DataContract]
            public class RangeResponse
            {
            [DataMember]
            public String name { get; set; }
            [DataMember]
            public CellValue[][] rows { get; set; }
          }
        }
this is giving error at line where the code is HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

1 Answer 1

1

Pavan, I guess your code is the result of the implementation of this.

I have done it and I could connect successfully with my user, password & tenant information.

What I can see in the URL is that there is a space in "/Shared%20 Documents/" that needs to be eliminated.

Also you haven't specified the range itself like:

/Model/Ranges**('A1')**

Hope you find it useful. Kind Regards

Your Answer

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

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