Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint 2013 - Document Library upload c#

I'm trying to upload video using c# to SharePoint 2013 document library, every time the code runs I get a "file not found" exception, it only throws errors with .mp4 files. If I upload a jpg or any other file format for that matter it will work. What am I doing wrong? Thanks in advance for the help.

        string result = string.Empty;

        try
        {
            //site url
            ClientContext context = new ClientContext("siturl");

            // The SharePoint web at the URL.
            Web web = context.Web;

            FileCreationInformation newFile = new FileCreationInformation();
            newFile.Content = System.IO.File.ReadAllBytes(@"C:\test.mp4");
            newFile.Url = "test.mp4";

            List docs = web.Lists.GetByTitle("Learning Materials2");

            Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
            context.Load(uploadFile);
            context.ExecuteQuery();
        }catch(Exception Ex)
        {

        }

UPADTE Created a library, for people to download that will work for uploading .mp4's https://github.com/bikecrazyy/sharepoint-library-uploader

like image 654
Tony Avatar asked Sep 17 '18 18:09

Tony


People also ask

How do I upload a document to SharePoint 2013?

To upload a file to SharePoint 2013 Document Library: Open the Document Library. Navigate to Files > Upload document, or click the New Document button. Browse for a file on your desktop and click OK.

How do I automatically upload files to SharePoint?

Make A Flow To Upload Documents To A SharePoint LibraryOpen the Power Automate action from the top menu and select Create a new flow. Choose the Power Apps button template. Name the flow UploadFileToDocument library and click Save.

Why is there no upload button on SharePoint?

You could check the permission of current user in the “Apps for SharePoint” library. Go to library settings->”Permissions for this document library” under “Permissions and Management”. Then you also could switch to another user to check if the same issue will occur.


1 Answers

string fileName=@"C:\test.mp4";

using (var clientContext = new ClientContext(siturl))
{
     using (var fs = new FileStream(fileName, FileMode.Open))
     {
         var fi = new FileInfo(fileName);
         var list = clientContext.Web.Lists.GetByTitle("Learning Materials2");
         clientContext.Load(list.RootFolder);
         clientContext.ExecuteQuery();
         var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name);
         Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fs, true);
     }
 }
like image 73
seiya1223 Avatar answered Sep 20 '22 19:09

seiya1223