0

Unable to upload the documents in SharePoint Online using a console application. Any solution to this problem?

public static void SaveFileToSharePoint(string fileName)
    {

       string Url = "https://xxxxx.sharepoint.com/sites";
       string libraryName = "Shared Documents";

        using (ClientContext clientContext = new ClientContext(Url))
                        {
                            SecureString passWord = new SecureString();
                            foreach (char c in "*****".ToCharArray()) passWord.AppendChar(c);
                            clientContext.Credentials = new SharePointOnlineCredentials("[email protected]",
                                passWord);
                            List list = clientContext.Web.Lists.GetByTitle(libraryName);
                            clientContext.Load(list.RootFolder);
                            clientContext.ExecuteQuery();
                            using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
                            {
                                Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, list.RootFolder.ServerRelativeUrl.ToString() + "/" + fileName.Split('\\')[1], fileStream, true);
                            }
                        }

    }

1 Answer 1

2

You should refer this link once. This post contains the code for authenticating with your SharePoint Online and upload a file into the given library.

 class Program
    {
        static void Main(string[] args)
        {
            using (ClientContext clientContext = new ClientContext("https://xxxx.sharepoint.com/sites/SiteName"))
            {
                SecureString passWord = new SecureString();
                foreach (char c in "YourPassword".ToCharArray()) passWord.AppendChar(c);
                clientContext.Credentials = new SharePointOnlineCredentials("[email protected]", passWord);
                Web web = clientContext.Web;
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content = System.IO.File.ReadAllBytes(@"C:\Applications\PropertyMaster.xlsx"); // this is my sample file path
                newFile.Url = "testFile.xlsx"; // file name which should be created in library

                List docs = web.Lists.GetByTitle("Library Name");
                Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
                clientContext.ExecuteQuery();
            }
        }
    }

The above code worked for me.

My Output image

I have created a console application. Still you have any issue then inform me.

5
  • I tried that code also but it's of no use.
    – Amitabha
    Commented May 2, 2016 at 10:51
  • I have implemented the above code and it worked for me. Let me know the error or issue which you are facing. Commented May 2, 2016 at 11:15
  • @Amitabha What permissions does your user ([email protected]) have on the site? You may want to also check out this question: sharepoint.stackexchange.com/questions/70562/…. There are a couple ways to upload files via CSOM. See this MSDN post: msdn.microsoft.com/en-us/library/office/dn904536.aspx. Commented May 2, 2016 at 11:30
  • @Aakash Thanks for the code. It worked, i was doing a stupid mistake in the url (first time in sharepoint).
    – Amitabha
    Commented May 2, 2016 at 12:04
  • please don't forget to mark as answer so that other people can get help :-) Commented May 2, 2016 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.