Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Download a file from a sharepoint document library using Sharepoint REST API

$
0
0

Hi There


I am trying to download a file from a document library using sharepoint REST API.

What I have done is.

1. I have created a console application in Visual Studio 2015

2. Have registered the App Principles and have generated a client id and Secret if using:-https://mysp.sharepoint.com/_layouts/15/appregnew.aspx

3. Also have given permission for the app using:- https://mysp.sharepoint.com/_layouts/15/appinv.aspx

Can some one please advice what should be the Permission Request XML at this stage.

4. In the console application I have added following nGuit packages

Strategik.CoreFramework.v16

5. The above package have created tokenhelper class for me.

6. I have added the clientid and Secret id in the config file

6. I have written following code in Main function


  String accessToken;
            Uri siteUri = new Uri("https://mysp.sharepoint.com/");
            string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
            accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal ,siteUri.Authority,realm).AccessToken;

                  System.Net.HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(siteUri + "/_api/web/GetFolderByServerRelativeUrl('/foldername/filename')");
            endpointRequest.Method = "GET";
            endpointRequest.Accept = "application/json;odata=verbose";
            endpointRequest.Headers.Add("Authorization", "Bearer " + accessToken);
            endpointRequest.Headers.Add("X-RequestDigest", "form digest value");
            endpointRequest.Headers.Add("X-HTTP-Method", "PUT");
            //endpointRequest.Headers.Add("content-length", "length of post body");
            System.Net.HttpWebResponse endpointResponse = (System.Net.HttpWebResponse)endpointRequest.GetResponse();
            StreamReader reader = new StreamReader(endpointResponse.GetResponseStream());
            var searchXml = new XmlDocument();
            searchXml.LoadXml(reader.ReadToEnd());


7. I am getting error at

endpointResponse.GetResponseStream()

saying "The remote server returned an error: (403) Forbidden."

Can some one please help me. and advise if the above steps are correct. Please also advise for Point no3


Regards

Umesh


Viewing all articles
Browse latest Browse all 7589

Trending Articles