DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > Downloading Images From SharePoint to Local Machine With C#

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.

Rathrola Prem Kumar user avatar by
Rathrola Prem Kumar
·
Jan. 07, 17 · Integration Zone · Tutorial
Like (2)
Save
Tweet
9.82K Views

Join the DZone community and get the full member experience.

Join For Free

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!

SharePoint Machine

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Importance of Semantics for Data Lakehouses
  • Data Science Project Folder Structure
  • 6 Reasons Cybersecurity Awareness Training is Important
  • Exhaustive JUNIT5 Testing with Combinations, Permutations, and Products

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo