For backup purposes (as users are wonderful at accidentally deleting things they shouldn't) I'm trying to write some software to download the contents of my company's users "One Drives" to the company network so we can back them up, in case they delete anything.
I currently am using the Microsoft.SharePoint.Client to do so.
I am also of the impression OneDrive For Business differs enough (with it's entanglement in Sharepoint) from OneDrive.
For example... if my OneDrive url is
https://accountname-my.sharepoint.com/personal/firstname_lastname_mycompany_com/Documents/
I can successfully list all the folders in the root of Documents and download all the files in the root of the Documents folder.
Here's an example of listing the folders within Documents.
public void Listfiles()
{
var folders = ListFolders(dirPath, clientContext, web);
// now do something with results contained in 'folders'
}
public List GetDocumentLibrary(string libraryName, ClientContext clientContext, Web web)
{
var query = clientContext.LoadQuery(web.Lists.Where(p => p.Title == libraryName));
clientContext.ExecuteQuery();
return query.FirstOrDefault();
}
public List<Folder> ListFolders(string libraryName, ClientContext clientContext, Web web)
{
var list = GetDocumentLibrary(libraryName, clientContext, web);
var folders = list.RootFolder.Folders;
clientContext.Load(folders);
clientContext.ExecuteQuery();
return folders.ToList();
}
This code works fine for returning a list of folders within the Documents folder, however, my problem is trying to list the contents of one of these folders.
e.g. https://accountname-my.sharepoint.com/personal/firstname_lastname_mycompany_com/Documents/PurchaseRequests/
If I try to run the same code above, this time I get the following exception thrown.
An unhandled exception of type 'Microsoft.SharePoint.Client.ClientRequestException' occurred in Microsoft.SharePoint.Client.dll
Additional information: Cannot contact site at the specified URL
https://accountname-my.sharepoint.com/personal/firstname_lastname_mycompany_com/Documents.
There is no Web named "/personal/firstname_lastname_mycompany_com/Documents/_vti_bin/sites.asmx"
This is occurring at the ExecuteQuery() statement in GetDocumentLibrary(), I don't understand why it isn't returning any results, as I've changed the clientcontext and web to point to the new URL to query instead of the one pointing at documents.
Does anyone have any idea why it's looking for /_vti_bin/sites.asmx, if so does anyone have any suggestions as to where I'm going wrong?
Am I going about this the correct way? Is there something I'm missing when it comes to subfolders in Sharepoint/OneDrive For Business? Is this some Microsoft oddity?
Many Thanks
Dougie