I am working on a Windows 8.1 app where I would like the ability to download files directly from SharePoint. We currently still have SharePoint 2010 here. Since I am developing for a Win 8.1 app, I am using the dll files found in C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI. The dll's I have referenced are:
Microsoft.SharePoint.Client.Portable
Microsoft.SharePoint.Client.Runtime.Portable
Microsoft.SharePoint.Client.Runtime.WindowsStore
I tried using the code below to download the file
public static async void DownloadFolderContents(string SharePointFolderPath, string SavePath) { try { // Create the ClientContext for the TMT site. using (ClientContext clientContext = new ClientContext(SharePointUrl)) { StorageFolder docLibrary = KnownFolders.DocumentsLibrary; StorageFolder saveFolder = await docLibrary.CreateFolderAsync(SavePath, CreationCollisionOption.OpenIfExists); // Get the website for the client context. Web website = clientContext.Web; // Set the project folder based on the SharePointFolderPath Folder projectFolder = website.GetFolderByServerRelativeUrl(SharePointFolderPath); // Load the website and folder. clientContext.Load(website); clientContext.Load(projectFolder, p => p.Files, p => p.Folders); await clientContext.ExecuteQueryAsync(); // Downlaod each file in the folder. foreach (var file in projectFolder.Files) { string fileRef = file.ServerRelativeUrl; string[] fileParts = fileRef.Split('.'); // Only download files that aren't prohibited by type. if (Array.IndexOf(ProhibitedFileTypes, fileParts[fileParts.Length - 1]) < 0) { StorageFile saveFile = await saveFolder.CreateFileAsync(file.Name, CreationCollisionOption.ReplaceExisting); clientContext.Load(file); ClientResult<Stream> sharePointFile = file.OpenBinaryStream(); await clientContext.ExecuteQueryAsync(); //using (Stream sharePointStream = sharePointFile.Value) //{ // using (var fileStream = await saveFile.OpenStreamForWriteAsync()) // { // await sharePointStream.CopyToAsync(fileStream); // } //} } } } } catch (Exception) { throw; } }
When I do this, I get an error that File.OpenBinaryStream doesn't exist which I believe is because we (still) have SharePoint 2010. If I try to use the older dlls, it won't compile because they require Web Services. Does anyone know a solution for this? I've searched around existing posts similar to this topic but none of their solutions seem to work. I believe that it's because they are either using SharePoint 2013 or newer or aren't working on a Windows Store App.