none
How to resolve exception when uploading file to SharePoint using List.RootFolder.Files.Add (C#) RRS feed

  • Question

  • Here is the code I am using:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.SharePoint.Client;
    
    namespace FilesToSharePointLibrary
    {
        class Program
        {
            static void Main(string[] args)
            {
                String fileToUpload = "C:/Users/UserName/Desktop/Test.txt";
                String sharePointSite = "http://share-internal.CompanyName.com/Section/Subsection"; 
                String libraryName = "Library Name";
                String fileName = fileToUpload.Substring(fileToUpload.LastIndexOf("\\") + 1);
                using(ClientContext context = new ClientContext(sharePointSite))
                {
                    FileCreationInformation FCInfo = new FileCreationInformation();
                    FCInfo.Url = fileToUpload;
                    FCInfo.Overwrite = true;
                    FCInfo.Content = System.IO.File.ReadAllBytes(fileToUpload);
    
                    Web web = context.Web;
                    List library = web.Lists.GetByTitle(libraryName);
                    library.RootFolder.Files.Add(FCInfo);
                    context.ExecuteQuery();
    
                }
                Console.WriteLine("Success");
            }
        }
    }

    The issue I am having in the context.ExecuteQuery(); line. When I attempt to add one file to the SharePoint Library, Library Name I get the following Exception: Microsoft.SharePoint.Client.ServerException: 'Value does not fall within the expected range.'

    Any help would be greatly appreciated. Thanks for your input!



    Monday, December 2, 2019 1:46 AM

Answers

  • Hey,

    Did you debug your code?

    The FileCreationInformation Url property is a local path and I don't think it is the right value.

    Besides, the fileName is also equal to fileToUpload...


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Learn Microsoft 365 from Microsoft DOCs now!
    Please Vote and Mark as Answer if it helps you.

    • Marked as answer by zhw12 Tuesday, December 3, 2019 1:17 AM
    Monday, December 2, 2019 3:22 AM
  • Hi zhuw12,

    Please use SharePoint library folder url when setting FCInfo.Url property, modify like this:

                String fileToUpload = "C:/Users/Administrator.CONTOSO2016/Desktop/Test.txt";
                String sharePointSite = "http://sp2016/sites/dev/";
                String libraryName = "Documents";
                String fileName = fileToUpload.Substring(fileToUpload.LastIndexOf("\\") + 1);
                using (ClientContext context = new ClientContext(sharePointSite))
                {
                    FileCreationInformation FCInfo = new FileCreationInformation();
                    FCInfo.Url = "http://sp2016/sites/dev/Shared%20Documents/Test.txt";
                    FCInfo.Overwrite = true;
                    FCInfo.Content = System.IO.File.ReadAllBytes(fileToUpload);
    
                    Web web = context.Web;
                    List library = web.Lists.GetByTitle(libraryName);
                    Microsoft.SharePoint.Client.File uploadfile = library.RootFolder.Files.Add(FCInfo);
                    uploadfile.CheckIn("testcomment",CheckinType.MajorCheckIn);
                    context.ExecuteQuery();
    
                }
                Console.WriteLine("Success");

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by zhw12 Tuesday, December 3, 2019 1:17 AM
    Monday, December 2, 2019 8:45 AM

All replies

  • Hey,

    Did you debug your code?

    The FileCreationInformation Url property is a local path and I don't think it is the right value.

    Besides, the fileName is also equal to fileToUpload...


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Learn Microsoft 365 from Microsoft DOCs now!
    Please Vote and Mark as Answer if it helps you.

    • Marked as answer by zhw12 Tuesday, December 3, 2019 1:17 AM
    Monday, December 2, 2019 3:22 AM
  • Hi zhuw12,

    Please use SharePoint library folder url when setting FCInfo.Url property, modify like this:

                String fileToUpload = "C:/Users/Administrator.CONTOSO2016/Desktop/Test.txt";
                String sharePointSite = "http://sp2016/sites/dev/";
                String libraryName = "Documents";
                String fileName = fileToUpload.Substring(fileToUpload.LastIndexOf("\\") + 1);
                using (ClientContext context = new ClientContext(sharePointSite))
                {
                    FileCreationInformation FCInfo = new FileCreationInformation();
                    FCInfo.Url = "http://sp2016/sites/dev/Shared%20Documents/Test.txt";
                    FCInfo.Overwrite = true;
                    FCInfo.Content = System.IO.File.ReadAllBytes(fileToUpload);
    
                    Web web = context.Web;
                    List library = web.Lists.GetByTitle(libraryName);
                    Microsoft.SharePoint.Client.File uploadfile = library.RootFolder.Files.Add(FCInfo);
                    uploadfile.CheckIn("testcomment",CheckinType.MajorCheckIn);
                    context.ExecuteQuery();
    
                }
                Console.WriteLine("Success");

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by zhw12 Tuesday, December 3, 2019 1:17 AM
    Monday, December 2, 2019 8:45 AM
  • Hi Justin,

    Yes, it's the problem.

    Thanks

    Tuesday, December 3, 2019 1:17 AM
  • Hi Jerry,

    Thanks for code demo, it's working.

    Tuesday, December 3, 2019 1:17 AM