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#.
- [HttpGet]
- public string GetProjectReport(string ProjectUID)
- {
-
- using (ClientContext context = new ClientContext("https://premkumar/sites/pwa"))
- {
-
- SecureString passWord = new SecureString();
- foreach (char c in "pass@word1".ToCharArray()) passWord.AppendChar(c);
- context. Credentials = new SharePointOnlineCredentials("username", passWord);
-
- var qry = new CamlQuery();
-
- qry.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>" + ProjectUID + ".jpeg</Value></Eq></Where></Query></View>";
-
-
- var sourceList = context.Web.Lists.GetByTitle("site name");
- var items = sourceList.GetItems(qry);
- context.Load(items);
- context.ExecuteQuery();
-
- var item = items.FirstOrDefault();
-
- var curPath = System.Web.Hosting.HostingEnvironment.MapPath ("~/folder name");
- Directory.CreateDirectory(curPath);
-
-
- var ctx = (ClientContext)item.Context;
- var fileRef = (string)item["FileRef"];
- var fileName = System.IO.Path.GetFileName(fileRef);
- var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileRef);
- var filePath = Path.Combine(curPath, fileName);
- using (var fileStream = System.IO.File.Create(filePath))
- {
-
- fileInfo.Stream.CopyTo (fileStream);
-
- }
-
-
- string imgurl = string.Empty;
- if (Request.RequestUri.OriginalString.Contains("localhost"))
- imgurl = Request.RequestUri.OriginalString.Replace (Request.RequestUri.PathAndQuery, "") + "/FinanceialImages/" + ProjectUID + ".Jpeg";
- else
- Imgurl = “http:
-
-
- return imgurl;
- }
- }
Using the above method, we can download images from SharePoint sites.
I hopethis article helps you all. Thank you.