Over a million developers have joined DZone.

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.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

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#:

        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);  
                // This the result of the query.  
                var item = items.FirstOrDefault();  
                // Ensure target directory exists.  
                var curPath = System.Web.Hosting.HostingEnvironment.MapPath ("~/folder name");  
                // 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";  
                    Imgurl = “http://sitename/appname/foldername/" + ProjectUID + ".Jpeg";  
                // Return the image URL.  
                return imgurl;  

That's it! 

Build and deploy API integrations 7x faster. Try the Cloud Elements 100% RESTful platform for 30 days free. Access your trial here.

sharepoint ,c# ,integration ,local machines

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}