Programmatically Upload a document with new Version to SharePoint List

Ask a question
 
  • Tuesday, April 14, 2009 6:44 PM
    da.3vil.genius

    da.3vil.genius

     

    65 Points 2 0 0
    Recent Achievements
    Forums Answerer I First Marked Answer
     
    65 Points
     
     
    0
    Sign In to Vote
    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
    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
     
     
    0
    Sign In to Vote

     

    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

  • Tuesday, April 14, 2009 6:52 PM
    Steve.Curran MVP

    Steve.Curran MVP

    KnowledgeLake

    Partner, MVP

    29,435 Points 10 4 2
    Recent Achievements
    Profile Complete Proposed Answerer I Forums Replies IV
    KnowledgeLake
    (Partner, MVP)
    29,435 Points
     
     
    0
    Sign In to Vote
    Try just using http://mysite.site.com/sites/appdevelop/Project%20Plans when constructing your SPSite object
    http://www.certdev.com
     
  • Tuesday, April 14, 2009 7:28 PM
    da.3vil.genius

    da.3vil.genius

     

    65 Points 2 0 0
    Recent Achievements
    Forums Answerer I First Marked Answer
     
    65 Points
     
     
    0
    Sign In to Vote

    Nope, still the same error.

     
  • Tuesday, April 14, 2009 7:56 PM
    Sathish TK

    Sathish TK

    Partner

    95 Points 3 0 0
    Recent Achievements
    First Answer Confirmed Forums Answerer I First Forums Reply
    (Partner)
    95 Points
     
     
    0
    Sign In to Vote
    Are you getting this error in anonymous mode or signed-in?

    Check the permissions on your list and ensure the proper user group can add new items to the list.

    In the code trying using RunWithElevatedPrivileges of the SPSecurity class (note: this is not the recommended approach)

    http://www.sathishtk.com/blog
     
  • Tuesday, April 14, 2009 8:43 PM
    Moonis Tahir

    Moonis Tahir

    MCC, Partner

    8,500 Points 12 3 1
    Recent Achievements
    Profile Complete Forums Answerer IV Proposed Answerer I
    (MCC, Partner)
    8,500 Points
     
     
    0
    Sign In to Vote
    where you are running your code, on sharepoint box Or on your development box trying to upload to a different sharepoint server? as long as you have permissions to the web and your code is running on sharepoint Box, there should be no issue in creating SPSite object. you can create SPSite object in RunWithElevatedPrivilage block.
    Moonis Tahir MCSD.net, MCTS Sharepoint 07 (Dev & Config), MCTS BizTalk 06, MCTS SQL 2005.
     
  • Tuesday, April 14, 2009 9:09 PM
    Dave Hunter

    Dave Hunter

    Portaltech Consulting Li...

    Partner, MVP

    20,310 Points 11 4 1
    Recent Achievements
    Proposed Answerer I Forums Answerer IV Forums Replies IV
    Portaltech Consulting Li...
    (Partner, MVP)
    20,310 Points
     
     
    0
    Sign In to Vote

    You need to use http://mysite.site.com/sites/appdevelop  in your new SPSite().  Project Plans is your document library.

    The following should work

    privatevoid UploadFileToSharePoint(string strInputFileName)
            {
                string destUrl = http://mysite.site.com/sites/appdevelop;
                string destFileUrl = destUrl  + "project%20plans" + "/New.txt;
                using(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();

                }
            }

    Hope this helps

    Dave


    My SharePoint Blog - http://www.davehunter.co.uk/blog
     
  • Wednesday, April 15, 2009 8:00 AM
    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
     
     
    0
    Sign In to Vote

     

    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/

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopmentlegacy/thread/c99e6161-66e9-4aea-953a-16f64f816cd7

 

 

 

 

  • Hi,

    i want to upload a document which is in local path, and if the document is already available, it should create a new version for the document.

    in API, SPfolder.Files.add method is having Overwrite option, i am not able to see the option to create version and all.

    Please guide me , Thanks in advance.

     

Answers

  • Wednesday, January 12, 2011 5:41 AM
    Hemendra Agrawal

    Hemendra Agrawal

    Wartsila

    Partner

    21,190 Points 20 6 2
    Recent Achievements
    Code Answerer III New Blog Rater New Blog Commentator
    Wartsila
    (Partner)
    21,190 Points
     
     
     
    0
    Sign In to Vote

    Hi,

    Thanks for reply. You need to write small algorithm to generate version.

    1. Check file whether it is existing or not

    2. If it is existing then CheckOut your file and CheckIn it

    Like this: SPListItem.File.CheckOut / SPListItem.File.CheckIn

    This will create new version of your item.

    Hope this could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
    • Marked As Answer by KeFang Chen Monday, January 17, 2011 7:52 AM
    •  
     
  • Wednesday, January 12, 2011 9:45 AM
    Amit.AK.Kumar

    Amit.AK.Kumar

    Partner

    1,350 Points 10 2 0
    Recent Achievements
    Forums Replies III Proposed Answerer I First Helpful Vote
    (Partner)
    1,350 Points
     
     
     
    0
    Sign In to Vote

    Please try this:

     

     destinationFolder.Files.Add(destinationFileURL, filebytes,sourceFile.Properties, true); 


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog
    • Marked As Answer by KeFang Chen Thursday, January 27, 2011 3:32 AM
    •  
     

All Replies

  • Tuesday, January 11, 2011 11:07 AM
    Hemendra Agrawal

    Hemendra Agrawal

    Wartsila

    Partner

    21,190 Points 20 6 2
    Recent Achievements
    Code Answerer III New Blog Rater New Blog Commentator
    Wartsila
    (Partner)
    21,190 Points
     
     
     
    0
    Sign In to Vote

    Hi,

    Try this:

    SPfolder.Files.add(filename, content, false );

    You may be refer this also:

    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/c99e6161-66e9-4aea-953a-16f64f816cd7/

    Let us know your result


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
     
  • Wednesday, January 12, 2011 5:25 AM
    Akilaskk

    Akilaskk

     

    40 Points 4 0 0
    Recent Achievements
    Forums Answerer I First Helpful Vote First Forums Reply
     
    40 Points
     
     
     
    0
    Sign In to Vote

    hi,

    Thanks for your response,

    i was trying like,

    SPfolder.Files.add(filename, content, false );

    but it shows , A file with the name "spfolder/hello.txt" already exists.

    i have enabled versioning in the SPfolder too.

    Please guide me. thanks in advance 

     
  • Wednesday, January 12, 2011 5:41 AM
    Hemendra Agrawal

    Hemendra Agrawal

    Wartsila

    Partner

    21,190 Points 20 6 2
    Recent Achievements
    Code Answerer III New Blog Rater New Blog Commentator
    Wartsila
    (Partner)
    21,190 Points
     
     
     
    0
    Sign In to Vote

    Hi,

    Thanks for reply. You need to write small algorithm to generate version.

    1. Check file whether it is existing or not

    2. If it is existing then CheckOut your file and CheckIn it

    Like this: SPListItem.File.CheckOut / SPListItem.File.CheckIn

    This will create new version of your item.

    Hope this could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"
    • Marked As Answer by KeFang Chen Monday, January 17, 2011 7:52 AM
    •  
     
  • Wednesday, January 12, 2011 9:45 AM
    Amit.AK.Kumar

    Amit.AK.Kumar

    Partner

    1,350 Points 10 2 0
    Recent Achievements
    Forums Replies III Proposed Answerer I First Helpful Vote
    (Partner)
    1,350 Points
     
     
     
    0
    Sign In to Vote

    Please try this:

     

     destinationFolder.Files.Add(destinationFileURL, filebytes,sourceFile.Properties, true); 


    Thanks, Amit Kumar, LinkedIn Profile ** My Blog
    • Marked As Answer by KeFang Chen Thursday, January 27, 2011 3:32 AM

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopmentlegacy/thread/1f003923-dc88-427b-a6b8-34c3616d5b1a

转载于:https://www.cnblogs.com/ilawrence/archive/2012/11/02/2750663.html

你可能感兴趣的:(c#)