Question

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

Was it helpful?

Solution

You're getting that error because you must be passing the Url to your OneDrive for Business document library to the Client Context instead of the Url to your OneDrive for Business site.

E.g. you're probably doing this:

new ClientContext("https://accountname-my.sharepoint.com/personal/firstname_lastname_mycompany_com/Documents/")

When you should be doing this:

new ClientContext("https://accountname-my.sharepoint.com/personal/firstname_lastname_mycompany_com/")

Note that the "Documents" part of the Url is a document library not a site. You must pass a site Url to new ClientContext(). That's why you get the error.

To list all the folders you'll need to do a bit of recursion or similar. Once you get that initial list of root folders, for each folder in that list do:

clientContext.Load(folder.Folders);
clientContext.ExecuteQuery();

And then for each folder returned do the same etc. until you have the full list of folders.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top