9

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

Tony
  • 859
  • 1
  • 7
  • 19

3 Answers3

2
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);
     }
 }
Jonathan Willcock
  • 5,012
  • 3
  • 20
  • 31
seiya1223
  • 582
  • 2
  • 5
0

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

VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • I checked we have the mime type set in IIS, mp4's play just fine, the only issue is that on the response of the code uploading the mp4, I get the file not found error – Tony Sep 20 '18 at 16:53
0

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 ?

  • When I upload through the SharePoint UI it works fine, the file will make it into the document library. When I upload via the code the file still makes it into the SharePoint document library... but throws a file not found. They aren't expected to be retrieved using the CSPM model. – Tony Sep 24 '18 at 16:58
  • I came across this article where one person had a messed up drop off library during a SharePoint migration from 2010 to 2013 and got the same File Not Found ecxeption from the UI. https://social.technet.microsoft.com/Forums/windows/en-US/bc2ad5ff-9056-4bca-a68f-0b999c7f30a0/file-not-found-when-creating-a-new-document-set?forum=sharepointgeneral They resolved it by configuring the drop off library by following the article here. http://blog.ciaops.com/2016/02/working-with-sharepoint-drop-off.html?m=1 Hope it helps. – PravinKotwani Sep 24 '18 at 17:35
  • tried to follow these steps with no luck, I even try creating a new library – Tony Sep 25 '18 at 16:48