1

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

Andy Crz
  • 13
  • 1
  • 5

1 Answers1

2

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();
}  
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • Thanks Vandim it worked..and I was able to update the coloumns via CSV, Any idea how to download full list (with coloumn values) into a csv ? – Andy Crz Oct 16 '15 at 06:52
  • @AndyCrz, it sounds to me like a good question, would you mind to create a separate question for it? – Vadim Gremyachev Oct 16 '15 at 07:11
  • http://stackoverflow.com/questions/33266404/sharepoint-uploading-files-with-column-values-cant-load-taxonomy-fields-in-ta here – Andy Crz Oct 21 '15 at 18:14