Search code examples
c#sharepointasmxcsomsharepointdocumentlibrary

Can't upload file to Document Library using CSOM


I am trying to upload multiple file to a Document Library and also update its coloumn values. List(Doc Lib) already exists but I am stuck with uploadinf the file

I've tried these methods

  1. using lists.asmx

            NetworkCredential credentials = new NetworkCredential("user", "Pass", "domain");
            #region ListWebService
            ListService.Lists listService = new ListService.Lists();
            listService.Credentials = credentials;
            List list = cc.Web.Lists.GetByTitle(library);
            listService.Url = cc.Url + "/_vti_bin/lists.asmx";
            try
            {
                FileStream fStream = System.IO.File.OpenRead(filePath);
                string fName = fStream.Name.Substring(3);
                byte[] contents = new byte[fStream.Length];
                fStream.Read(contents, 0, (int)fStream.Length);
                fStream.Close();
    
                string attach = listService.AddAttachment(library, itemId.ToString(), Path.GetFileName(filePath), contents);
    
            }
            #endregion
    
    
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                CSVWriter("Message:\n" + ex.Message + "\nDetail:\n" +
                    ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace, LogReport);
            }
    

It gives a error ServerException :To add an item to a document library, use SPFileCollection.Add() on AddAttachment()

  1. Using

            List lib = cc.Web.Lists.GetByTitle("TestLib");
             FileCreationInformation fileInfo = new FileCreationInformation();
             fileInfo.Content = System.IO.File.ReadAllBytes("C:\\Users\\AJohn\\Desktop\\sample.docx");
             fileInfo.Url = "https://serverm/sites/Testing1/TestLib/sample.docx";
             fileInfo.Overwrite = true;
             Microsoft.SharePoint.Client.File upFile = lib.RootFolder.Files.Add(fileInfo);
             cc.Load(upFile);
             cc.ExecuteQuery();
    

I was able to upload once using this method, but now I am getting ServerException :To add an item to a document library, use SPFileCollection.Add() on cc.ExecuteQuery()

But if at all this method works, what I want is that I should update the coloumn values related to this file. In first method I get item.ID so from there I can update the Coloumn Values


Solution

  • Regarding the second method, the following example demonstrates how to upload a file into Documents library and set it's properties (e.g. Category text field)

    using (var ctx = new ClientContext(webUri))
    {
         var targetList = ctx.Web.Lists.GetByTitle("Documents");
         var fileInfo = new FileCreationInformation
         {
              Url = System.IO.Path.GetFileName(sourcePath),
              Content = System.IO.File.ReadAllBytes(sourcePath),
              Overwrite = true
         };
    
         var file = targetList.RootFolder.Files.Add(fileInfo);
         var item = file.ListItemAllFields;
         item["Category"] = "User Guide";
         item.Update();
         ctx.ExecuteQuery();
    }