0

I am trying to download multiple files from a SharePoint site.

I have it working for one file, where I know the filename, by using a Script Task

WebClient client = new WebClient();
client.Credentials = GetCredentials();// Separate Method
client.DownloadFile(spFolderURL + "Filename.xlsx", spLocalFolder + "Filename.xlsx")

but I need to be able to loop through all the files within the folder and dynamically get the file name.

I am trying this code project solution and the solution below, I only need to download one file type (.xlsx).

using (SPSite oSite = new SPSite(spFolderURL))
{
  using (SPWeb oWeb = oSite.OpenWeb())
  {
    SPList list = oWeb.Lists["listname"];

    WebClient client = new WebClient();

    SPListItemCollection oItemCol = list.GetItems();
    foreach (SPListItem oItem in oItemCol)
    {
        string strID = Convert.ToString(oItem["ID"]);
        SPFolder folder = oWeb.Folders["Lists"].SubFolders["Tasks"].SubFolders["Attachments"].SubFolders[strID];
        foreach (SPFile file in folder.Files)
        {
            client.Credentials = GetCredentials();// Separate Method
            client.DownloadFile(spFolderURL + "Filename.xlsx", spLocalFolder + "Filename.xlsx")
        }

    }
  }
}

But I can't seem to get the SPSite and other SharePoint items recognised.

I have added a reference in the script to Microsoft.SharePoint.Client, but it looks like these items need Microsoft.SharePoint, I can't find that in the list of available assemblies.

I am using Visual Studio 2013

***EDIT

Have done further research I needed to download the SharePoint list adapters I am now able to access the SharePoint lists to extract the file names required.

Noelle
  • 772
  • 9
  • 18
  • 44

2 Answers2

1

Upon further research I discovered I needed to download the SharePoint list adapters I am now able to access the SharePoint lists to extract the file names required.

Noelle
  • 772
  • 9
  • 18
  • 44
0

SPSite(the object name starts with SP) is server side object model which can only be used in a farm solution or SharePoint server, you could use CSOM(the library which name contains client.xx.dll) to download the file.

sample here

Or http request sample

Lee
  • 5,305
  • 1
  • 6
  • 12