none
Upload Document to Document Library programatically SharePoint 2013 RRS feed

  • Question

  • Hi..

    I want to upload document to document library programatically using sharepoint 2013 Visual Web Part


    Ravindranath

    Wednesday, September 18, 2013 10:59 AM

Answers

  • try this in your webpart code ...

    using (SPSite oSite = new SPSite("yourSite"))
    {
    using (SPWeb oWeb = oSite.OpenWeb())
    {
    string filePath = "C://MyFile.txt";
    SPFolder myLibrary = oWeb.Folders["documentLibraryName"];
    String fileName = System.IO.Path.GetFileName(filePath);
    FileStream fileStream = File.OpenRead(filePath);
    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, true);
    myLibrary.Update();
    }
    }



    Thanks
    Ganesh Jat [My Blog | LinkedIn | Twitter ]

    Wednesday, September 18, 2013 3:58 PM

All replies

  • Hi, 

    Why not just use standart ListViewWebPart??

    Wednesday, September 18, 2013 11:03 AM
  • try this in your webpart code ...

    using (SPSite oSite = new SPSite("yourSite"))
    {
    using (SPWeb oWeb = oSite.OpenWeb())
    {
    string filePath = "C://MyFile.txt";
    SPFolder myLibrary = oWeb.Folders["documentLibraryName"];
    String fileName = System.IO.Path.GetFileName(filePath);
    FileStream fileStream = File.OpenRead(filePath);
    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, true);
    myLibrary.Update();
    }
    }



    Thanks
    Ganesh Jat [My Blog | LinkedIn | Twitter ]

    Wednesday, September 18, 2013 3:58 PM