none
how to upload xml file to document library

    Question

  • Dear friends,

    I am programatically creating a xml file as shown below:
    <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
    <item>
      <item Name="ReksTest" Type="DocumentLibrary" GroupOrUser="NT AUTHORITY\authenticated users" Permission=" Restricted Read  Limited Access " />
      <item Name="ReksTest" Type="DocumentLibrary" GroupOrUser="ALKHALIJI\mterier" Permission=" Limited Access " />
      <item Name="ReksTest" Type="DocumentLibrary" GroupOrUser="ALKHALIJI\kmohamed" Permission=" Limited Access " />
      <item Name="ReksTest" Type="DocumentLibrary" GroupOrUser="ALKHALIJI\mosstrans" Permission=" Limited Access " />
      <item Name="ReksTest" Type="DocumentLibrary" GroupOrUser="ALKHALIJI\bbadr" Permission=" Limited Access " />
     <item/>
    this file is saved to local drive as test.txt using doc.Save("local path where we want to save the file").

    I now need to save this file into sharepoint document library say "Permission" as an xml file.

    How can I do this?

    Thanks for help in advance!

    Reks
    Monday, October 13, 2008 7:58 AM

Answers

  • Hello,
    you should do something like this:
    string filename; // name of your file  
    byte[] bytes; // byte-array containing the file info  
    using (SPSite site = new SPSite("url to site")) {  
      using (SPWeb web = site.OpenWeb()) {  
         SPDocumentLibrary library = web.Lists["Name of your list"as SPDocumentLibrary;  
         if (library != null) {   
            SPFolder folder = library.RootFolder;  
            string url = folder.Url + "/" + filename;  
            folder.Files.Add(url, bytes);  
        }  
      }  
    /WW
    http://www.wictorwilen.se/
    • Marked as answer by Paul Galvin Monday, October 13, 2008 10:53 AM
    Monday, October 13, 2008 8:14 AM
  • Hi,
    bytes will contain the bytes of the file, there are several methods to get this. You can use the FileStream object like this:
    bytes[] bytes = null;  
    using(FileStream stream = new FileStream("c:\\temp\\text.xml", FileMode.Open)) {  
      bytes = new byte[stream.Length];  
      stream.Read(bytes,0,stream.Length);  
    /WW
    http://www.wictorwilen.se/
    • Marked as answer by Paul Galvin Monday, October 13, 2008 10:53 AM
    Monday, October 13, 2008 9:14 AM

All replies

  • Hello,
    you should do something like this:
    string filename; // name of your file  
    byte[] bytes; // byte-array containing the file info  
    using (SPSite site = new SPSite("url to site")) {  
      using (SPWeb web = site.OpenWeb()) {  
         SPDocumentLibrary library = web.Lists["Name of your list"as SPDocumentLibrary;  
         if (library != null) {   
            SPFolder folder = library.RootFolder;  
            string url = folder.Url + "/" + filename;  
            folder.Files.Add(url, bytes);  
        }  
      }  
    /WW
    http://www.wictorwilen.se/
    • Marked as answer by Paul Galvin Monday, October 13, 2008 10:53 AM
    Monday, October 13, 2008 8:14 AM
  • Hey thanks for quick reply. What will bytes contain???? as I create a test.txt containing above dynamic data. Which is nothing but the xml content. test.txt is saved in local drive at "C:\" path how do I save this file to sharepoint library?

    Once again thanks for you help.

    Reks
    Monday, October 13, 2008 8:35 AM
  • Hi,
    bytes will contain the bytes of the file, there are several methods to get this. You can use the FileStream object like this:
    bytes[] bytes = null;  
    using(FileStream stream = new FileStream("c:\\temp\\text.xml", FileMode.Open)) {  
      bytes = new byte[stream.Length];  
      stream.Read(bytes,0,stream.Length);  
    /WW
    http://www.wictorwilen.se/
    • Marked as answer by Paul Galvin Monday, October 13, 2008 10:53 AM
    Monday, October 13, 2008 9:14 AM
  • Hi Wictor, do you know how to overwrite file in the sharepoint list?

    Your code work fine if the file is not exist in the document library or list but if the file is exist you get an error message that will say that the file is already exist.


    Monday, October 13, 2008 2:24 PM
  • Heya,
    just add a boolean value of true as a third parameter to the Add method.
     folder.Files.Add(url, bytes,true);   
    This will overwrite any existing files with the same name.
    /WW
    http://www.wictorwilen.se/
    Monday, October 13, 2008 2:59 PM
  • Thanks Wictor, i must really stupid to miss something like that. 
    Monday, October 13, 2008 3:34 PM