none
upload documents using COM in sharepoint 2010

    问题

  • Hello all.

    can you please advise the source code as how to upload a document using Client object model in sharepoint 2010,

    many thanks

    Kajal

    2012年6月27日 21:19

答案

  • Hi you can use a MemoryStream and the FileCreationInformation class for this: This sample creates a doc library and adds a file to it

    using (ClientContext ctx = new ClientContext("your site url")) { Web site = ctx.Web; ctx.Load(site); ctx.ExecuteQuery(); ListCreationInformation listCI = new ListCreationInformation(); listCI.Title = "MyDocs"; listCI.Description = "A document library"; listCI.TemplateType = (int)ListTemplateType.DocumentLibrary; //Create document MemoryStream m = new MemoryStream; StreamWriter w = new StreamWriter(m); w.Write("Hello World from a Document"); w.Flush; FileCreationInformation fileCI = new FileCreationInformation(); fileCI.Content = m.ToArray; fileCI.Overwrite = true; fileCI.Url = "http://<yoursiteurl>/MyDocs/MyFile.txt"; Folder rootFolder = site.GetFolderByServerRelativeUrl("MyDocs"); ctx.Load(rootFolder); Microsoft.SharePoint.Client.File newFile = rootFolder.Files.Add(fileCI); ctx.ExecuteQuery();

    }


    Kind Regards Bjoern
    Blog










    2012年6月27日 22:03

全部回复

  • Hi you can use a MemoryStream and the FileCreationInformation class for this: This sample creates a doc library and adds a file to it

    using (ClientContext ctx = new ClientContext("your site url")) { Web site = ctx.Web; ctx.Load(site); ctx.ExecuteQuery(); ListCreationInformation listCI = new ListCreationInformation(); listCI.Title = "MyDocs"; listCI.Description = "A document library"; listCI.TemplateType = (int)ListTemplateType.DocumentLibrary; //Create document MemoryStream m = new MemoryStream; StreamWriter w = new StreamWriter(m); w.Write("Hello World from a Document"); w.Flush; FileCreationInformation fileCI = new FileCreationInformation(); fileCI.Content = m.ToArray; fileCI.Overwrite = true; fileCI.Url = "http://<yoursiteurl>/MyDocs/MyFile.txt"; Folder rootFolder = site.GetFolderByServerRelativeUrl("MyDocs"); ctx.Load(rootFolder); Microsoft.SharePoint.Client.File newFile = rootFolder.Files.Add(fileCI); ctx.ExecuteQuery();

    }


    Kind Regards Bjoern
    Blog










    2012年6月27日 22:03
  • thanks Bjoern for your help !
    2012年6月28日 4:23