Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Downloading Images From SharePoint to Local Machine With C#

DZone's Guide to

Downloading Images From SharePoint to Local Machine With C#

By simply passing the reference ID to a method written in and accessing the URL, you can download images from a SharePoint site to your local machine.

Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

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"))  
            {  
                // Create the user credential.  
                SecureString passWord = new SecureString();  
                foreach (char c in "pass@word1".ToCharArray()) passWord.AppendChar(c);  
                context. Credentials = new SharePointOnlineCredentials("username", passWord);  
                // Prepare the query to fetch the image.  
                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>";  
  
                // Execute the query.  
                var sourceList = context.Web.Lists.GetByTitle("site name");  
                var items = sourceList.GetItems(qry);  
                context.Load(items);  
  context.ExecuteQuery();  
                // This the result of the query.  
                var item = items.FirstOrDefault();  
                // Ensure target directory exists.  
                var curPath = System.Web.Hosting.HostingEnvironment.MapPath ("~/folder name");  
                Directory.CreateDirectory(curPath);  
  
                // Download the image from Share Point Server based on the query result.  
                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);  
                      
                }  
  
                // Construct the image URL.  
                string imgurl = string.Empty;  
                if (Request.RequestUri.OriginalString.Contains("localhost"))  
                    imgurl = Request.RequestUri.OriginalString.Replace (Request.RequestUri.PathAndQuery, "") + "/FinanceialImages/" + ProjectUID + ".Jpeg";  
                else  
                    Imgurl = “http://sitename/appname/foldername/" + ProjectUID + ".Jpeg";  
  
                // Return the image URL.  
                return imgurl;  
            }  
        } 

That's it! 

Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
sharepoint ,c# ,integration ,local machines

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}