`

How To Download Images From SharePoint Site To Your Local Machine Using C#

First of all, make sure you have installed the required SDKs by downloading from this link. Once you are done with downloading, add the below two references to your project



Given below is my SharePoint site from where I need to extract the images.



As shown in the above figure, we are going to download the images from the above site; each image has one reference id in my project.

I will pass the reference id to a method and access the URL. Let's start writing the method in C#.

  1. [HttpGet]  
  2.         public string GetProjectReport(string ProjectUID)  
  3.         {  
  4.             
  5.             using (ClientContext context = new ClientContext("https://premkumar/sites/pwa"))  
  6.             {  
  7.                 // Create the user credential.  
  8.                 SecureString passWord = new SecureString();  
  9.                 foreach (char c in "pass@word1".ToCharArray()) passWord.AppendChar(c);  
  10.                 context. Credentials = new SharePointOnlineCredentials("username", passWord);  
  11.                 // Prepare the query to fetch the image.  
  12.                 var qry = new CamlQuery();  
  13.   
  14.                 qry.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>" + ProjectUID + ".jpeg</Value></Eq></Where></Query></View>";  
  15.   
  16.                 // Execute the query.  
  17.                 var sourceList = context.Web.Lists.GetByTitle("site name");  
  18.                 var items = sourceList.GetItems(qry);  
  19.                 context.Load(items);  
  20.   context.ExecuteQuery();  
  21.                 // This the result of the query.  
  22.                 var item = items.FirstOrDefault();  
  23.                 // Ensure target directory exists.  
  24.                 var curPath = System.Web.Hosting.HostingEnvironment.MapPath ("~/folder name");  
  25.                 Directory.CreateDirectory(curPath);  
  26.   
  27.                 // Download the image from Share Point Server based on the query result.  
  28.                 var ctx = (ClientContext)item.Context;  
  29.                 var fileRef = (string)item["FileRef"];  
  30.                 var fileName = System.IO.Path.GetFileName(fileRef);  
  31.                 var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileRef);  
  32.                 var filePath = Path.Combine(curPath, fileName);  
  33.                 using (var fileStream = System.IO.File.Create(filePath))  
  34.                 {  
  35.                       
  36.                         fileInfo.Stream.CopyTo (fileStream);  
  37.                       
  38.                 }  
  39.   
  40.                 // Construct the image URL.  
  41.                 string imgurl = string.Empty;  
  42.                 if (Request.RequestUri.OriginalString.Contains("localhost"))  
  43.                     imgurl = Request.RequestUri.OriginalString.Replace (Request.RequestUri.PathAndQuery, "") + "/FinanceialImages/" + ProjectUID + ".Jpeg";  
  44.                 else  
  45.                     Imgurl = “http://sitename/appname/foldername/" + ProjectUID + ".Jpeg";  
  46.   
  47.                 // Return the image URL.  
  48.                 return imgurl;  
  49.             }  
  50.         }  
Using the above method, we can download images from SharePoint sites.

I hopethis article helps you all. Thank you.

 

Ebook Download
View all
Learn
View all