Upload Documents to Document Library programmatically RRS feed

  • Question

  • Hello,


    I am trying to upload a doc into a doc library this is my helper function for any document upload

                 Guid siteID = SPContext.Current.Site.ID;
                  Guid webID = SPContext.Current.Web.ID; 

                                 using (SPSite secureSite = new SPSite(siteID))

                                     using (SPWeb web = new SPSite(destinationUrl).OpenWeb(webID))
                                         web.AllowUnsafeUpdates = true;
                                         string destinationUrl= destinationUrl+ "/" + strFileName;
                                         FileStream fileStream = File.Open(strInputFileName, FileMode.Open);
                                         web.Files.Add(destinationUrl, fileStream, hsInputs, true);

                                        web.AllowUnsafeUpdates = false;


     i can upload docs, if i am using my base machine on which the project is deployed but in any other machine this simply would not work.

    Any Idea will be appreciated


    Sunday, November 13, 2011 1:10 PM


All replies

  • The above code leaks an SPSite object:

    using (SPWeb web = new SPSite(destinationUrl).OpenWeb(webID))

    The SPSite object created in the line above is never disposed. The using only disposes the SPWeb object.

    Regarding the problem: did you verify that the destinationUrl points to a site collection in the farm the code is running on?

    Stefan Goßner
    Senior Escalation Engineer - Microsoft CSS
    This post is provided "AS IS" with no warrenties and confers no rights.
    • Proposed as answer by Shimin Huang Monday, November 14, 2011 10:49 AM
    • Marked as answer by Shimin Huang Friday, November 25, 2011 8:05 AM
    Sunday, November 13, 2011 8:34 PM
  • Using webservice may easier to make sure it work on client.
    • Edited by FunLee Monday, November 14, 2011 4:17 AM
    • Marked as answer by Shimin Huang Friday, November 25, 2011 8:05 AM
    Monday, November 14, 2011 4:16 AM

  • You can simply open the web using the statement like

    using (SPWeb web = secureSite.OpenWeb(webID))

    also make sure that destination URL is correct.

    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale
    Monday, November 14, 2011 6:37 AM