Search code examples
c#sharepointconsole-applicationonedrive

Is there a way to upload files via C# to a shared online OneDrive


I've been having a problem figuring out how to upload a set of files to a shared online OneDrive via a C# console application. I've tried multiple different solutions, but nothing is working. I think I'm starting off on the wrong foot on this one.

Modified link OneDrive: Shared OneDrive Link

This is the code I'm using to upload the file, but it keeps throwing an error

Microsoft.SharePoint.Client.ClientRequestException: Cannot contact site at the specified URL...

at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfo(WebRequestExecutor executor) at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate() at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest() at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() at API_Calls.ACH_Upload() in

filepath:line 70

public class API_Calls
{
    public static void ACH_Upload()
    {
        try
        {
            string userName = "username";
            string password = "password";
            var securePassword = new SecureString();

            foreach (char c in password)
            {

                securePassword.AppendChar(c);
            }

            using (var ctx = new ClientContext("OneDrive_Url"))
            {
                ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(userName, securePassword);
                Web web = ctx.Web;
                ctx.Load(web);
                ctx.ExecuteQuery();
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content = System.IO.File.ReadAllBytes(@"C:\Data\ACH_Files\ACH_" + DateTime.Now.ToString("MM_dd_yyyy") + @"\" + DateTime.Now.ToString("MM_dd_yyyy") + @"_ACH_organization.xlsx");
                newFile.Url = DateTime.Now.ToString("MM_dd_yyyy") + @"_ACH_organization.xlsx";
                List byTitle = ctx.Web.Lists.GetByTitle("ACH_" + DateTime.Now.ToString("MM_dd_yyyy"));
                Folder folder = byTitle.RootFolder.Folders.GetByUrl("_developmentfoldername");
                ctx.Load(folder);
                ctx.ExecuteQuery();
                Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(newFile);
                ctx.Load(byTitle);
                ctx.Load(uploadFile);
                ctx.ExecuteQuery();
                Console.WriteLine("done");
            }

        }

        catch (Exception e)
        {

            Console.WriteLine(e.ToString());
        }
 }
}

I expected it to read the file in a local folder and upload that file to a folder I made in the shared OneDrive folder, but as seen above it doesn't work.

Am I going down the correct path to get the right solution, or is it completely off? Thank you all in advance.


Solution

  • You appear to be hitting an issue for the CSOM client library obtaining the request digest. This is the first thing that is done, and it's accomplished by building a request off of the value you provide to ClientContext.

    It's not obvious from your question, but if the modified link is an example of what OneDrive_Url represents it won't work. The value needs to be the URL to the target site (e.g. https://insertorganization-my.sharepoint.com/personal/name_organization_com) and NOT a URL to a resource (or page) within the site.