Tony
Tony

Reputation: 889

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

Upvotes: 9

Views: 1317

Answers (3)

seiya1223
seiya1223

Reputation: 582

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);
     }
 }

Upvotes: 2

PravinKotwani
PravinKotwani

Reputation: 23

Have you tried uploading the file to the SharePoint document library from the SharePoint UI ? Does it work well ? It could be that the SharePoint site could be configured to disallow certain file types.

https://support.office.com/en-us/article/types-of-files-that-cannot-be-added-to-a-list-or-library-30be234d-e551-4c2a-8de8-f8546ffbf5b3#ID0EAABAAA=2013,_2010

Also, are files uploaded to SharePoint expected to be retrieved using the CSOM model ?

Upvotes: 0

VDWWD
VDWWD

Reputation: 35554

I have no specific knowledge of SharePoint. But with websites and IIS if an extension is not listed under MIME Types, then the browser will throw a 404.

It would seem that SharePoint has something similar. See

https://kerseub.wordpress.com/2012/04/23/add-new-file-type-in-sharepoint/

and

https://social.technet.microsoft.com/Forums/office/en-US/ef4a0922-bacd-40df-9c97-25d09bed30ac/how-to-play-mp4-video-in-sharepoint-2013?forum=sharepointgeneral

Upvotes: 0

Related Questions