Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

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

$
0
0

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!




Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>