SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A code snippet of how i can download a documentSet programmatically using the sharepoint object model would really help.

What I am trying to do is

  • Given the sharepoint site, log in with the user default credentials
  • look for the document library where the files are hosted
  • Pull the files down on to the local machine

What I have done so far,

using Microsoft.SharePoint.Client;

ClientContext cc = new ClientContext(ConfigurationManager.AppSettings["site"]);
        cc.Credentials = new NetworkCredential(username, pwd, domain);
        Web site = cc.Web;
        ListCollection collList = site.Lists;

        var oList = collList.GetByTitle("Document Set test");

        // Get the document set

        // Get All views
        var views = oList.Views;

        // Get All documents
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = @"<Query>
                                    <FieldRef Name='Title'/>
                                    <FieldRef Name='Display Name'/>
                                     <FieldRef Name='Created' />
                                     <Value IncludeTimeValue='TRUE' Type='DateTime'>1900-05-08T14:25:50Z</Value>
                                     <FieldRef Name='Title' Ascending='True' />

        var docs = oList.GetItems(camlQuery);

        Console.WriteLine(string.Format("{0} Models in the repository", docs.Count));

foreach (var doc in docs) 


Console.WriteLine(string.Format("{0} => {1} ", Environment.NewLine, doc["Title"]));


share|improve this question

Not sure what the CAML Query will return but an approach would be to have the CAML Query return the Doc Library containing the items. One then iterates through it by working with the Folders and Doc Sets prior to getting the document item itself. This example illustrates it - Working with SP 2010 Document Sets using the SP Object Model.

share|improve this answer

You need to add to your viewfields for your query the field containing the URL to the file itself. I'm not sure which field you want, but I think it's FileRef or FileLeafRef.

Then simply download using a WebClient call - something like WebClient.DownloadFile()

share|improve this answer

you can try this:

    foreach (var doc in docs) // your foreach, where you iterate all files under document set
     string targetFileName = @"c:\";
     var finfo = File.OpenBinaryDirect(cc, doc["FileRef"].ToString());
     using (var fs = new FileStream(targetFileName, FileMode.OpenOrCreate))

Hope, this will be helpful.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.