Now I am creating a web api to get document from SharePoint and save on local machine.
- //to save file on local system from sharepoint server.
- [Route("~/api/Insight/DownloadDocument")]
- [System.Web.Http.HttpGet]
- public async Task<HttpResponseMessage> DownloadDocument()
- {
- try
- {
- string fedAuthString = null;
- var headers = Request.Headers;
- if (headers.Contains("SPOIDCRL"))
- {
- fedAuthString = headers.GetValues("SPOIDCRL").First();
- }
- // fedAuthString = "Sharepoint url to get Document (api)";
- //InfraAndComm document
- string InfraAndCommInsightPath = System.Web.Hosting.HostingEnvironment.MapPath("~/IndustryDocument/InfraAndComm/");
- string InfraAndCommInsightsUrl = "Sharepoint url to get Document (api)";
- DocumentResult.DownloadDocument(fedAuthString, InfraAndCommInsightPath, InfraAndCommInsightsUrl);
- return Request.CreateResponse(HttpStatusCode.OK, " All insight document downloaded successfully!!!");
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.OK, "Something wrong !!! please try again");
- }
- }
- //Download Document from sharepoint server and store in Application folder..
- public async void DownloadDocument(string fedAuthString, string DocumentPath,string insightUrl)
- {
- string baseurl1= WebConfigurationManager.AppSettings["InsightUrl"].ToString(); //Base url/realtiveURL
- string baseurl = WebConfigurationManager.AppSettings["InsightBaseUrl"].ToString(); //Base URL
- Core.Model.Results rs = new Core.Model.Results();
- Uri uri = new Uri(baseurl1);
- string industryInsightsUrl = uri + insightUrl;
- var cookieContainer = new CookieContainer();
- cookieContainer.Add(uri, new Cookie("SPOIDCRL", fedAuthString));
- HttpClientHandler handler = new HttpClientHandler();
- handler.CookieContainer = cookieContainer;
- HttpClient _client = new HttpClient(handler);
- CancellationToken ct;
- TimeSpan? timeout = null;
- if (timeout.HasValue)
- _client.Timeout = timeout.Value;
- ct.ThrowIfCancellationRequested();
- _client.DefaultRequestHeaders.Add("Accept", @"application / json; odata = verbose");
- var resp = await _client.GetAsync(industryInsightsUrl);
- var result = resp.Content.ReadAsStringAsync().Result;
- // return Request.CreateResponse(HttpStatusCode.OK, result);
- //to desrilize json
- var obj = JsonConvert.DeserializeObject<RootValue>(result).d;
- List<InsightsReturn> InsightFinalList = new List<InsightsReturn>();
- //for file data
- if (obj.Files.results.Count > 0)
- {
- foreach (var FileData in obj.Files.results)
- {
- /////////////////////////////type code to find your data.
- }
- }
- }
