现在的位置: 首页 > 综合 > 正文

Programmatically Upload a document with new Version to SharePoint List

2012年08月30日 ⁄ 综合 ⁄ 共 4794字 ⁄ 字号 评论关闭
文章目录
  • Tuesday, April 14, 2009 6:44 PM

    Avatar of da.3vil.genius
    da.3vil.genius

    Avatar of da.3vil.genius

    da.3vil.genius

     

    65 Points 2 0 0
    Recent Achievements
    Forums Answerer I First Marked Answer
     

    65 Points

     
      Has Code
    Hello,

    I'm trying to programmatically upload a file to a
    sharepoint list but keep getting the following error:

    The Web application
    at http://mysite.site.com/sites/appdevelop/Project%20Plans/Forms/AllItems.aspx?RootFolder=%2fsites%2fappdevelop%2fProject%20Plans%2fExternal%20Website
    could not be found. Verify that you have typed the URL correctly. If the URL
    should be serving existing content, the system administrator may need to add a
    new request URL mapping to the intended application.

    Here's the code I'm
    using (C#):

    private void UploadFileToSharePoint(string strInputFileName)
            {
                string destUrl = ConfigurationManager.AppSettings["destURL"]; 
                string destFileUrl = destUrl + "/New.txt;
                SPWeb site = new SPSite(destUrl).OpenWeb();
    
                site.AllowUnsafeUpdates = true;
    
                FileStream fileStream = File.Open(strInputFileName, FileMode.Open);
    
                site.Files.Add(destFileUrl, fileStream, true/*overwrite*/);
    
                fileStream.Close();
            }

    Can anyone tell me where I'm going wrong?

    Thanks in
    advance.

     

Answers

  • Wednesday, April 15, 2009 8:00 AM

    Avatar of manish parab
    manish parab

    Avatar of manish parab

    manish parab

    Ag technologies

    Partner

    85 Points 6 0 0
    Recent Achievements
    Forums Replies
    I
    First Marked
    Answer
    Forums Answerer
    I
    Ag technologies
    (Partner)

    85 Points

     
     Answered

     

    I have Modified your
    function a little bit..this is how it looks

     

     private void UploadFileToSharePoint(string strInputFileName,string sDocLibraryName)
    
            {
    
                string destUrl = ConfigurationManager.AppSettings["destURL"]; 
    
                
    
                SPWeb site = new SPSite(destUrl).OpenWeb();
    
    
    
                SPList myList = site.Lists[sDocLibraryName];
    
    
    
                string destFileUrl = myList.RootFolder.ServerRelativeUrl + @"/New.txt";
    
    
    
                site.AllowUnsafeUpdates = true;
    
    
    
                FileStream fileStream = File.Open(strInputFileName, FileMode.Open);
    
    
    
                site.Files.Add(destFileUrl, fileStream, true/*overwrite*/);
    
    
    
                fileStream.Close();
    
            }
    
    

    Call To this Function Would Be 

    UploadFileToSharePoint(@"C:\Ids.txt", "strore 102"/* name of Dc Library*/);
    
    

    Hope This Helps

    regards
    Manish
    http://mnish.blogspot.com/

    • Marked As Answer by da.3vil.genius Wednesday, April 15, 2009 3:39 PM
    •  

     

All Replies