Programmatically upload a document with new version to Sharepoint list

Source: Internet
Author: User
Document directory
  • Answers
  • All replies

Ask a question

  • Tuesday, 2017l 14,200 9 pm

    Da.3devil. Genius

    Da.3devil. Genius

     

    65 points 2 0 0 recent achievementsforums answerer I first marked answerda.3devil. Genius's threads view Profile

    65 points 0

    Sign in to vote hello,

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

    The Web Application
    At http://mysite.site.com/sites/appdevelop/Project%20Plans/Forms/AllItems.aspx? Rootfolder = % 2 fsites % 2 fappdevelop % 2 fproject % 20 plans % 2 fexternal % 20 website
    Cocould not be found. Verify that you have typed the URL correctly. If the URL
    Shocould be serving existing content, the system administrator may need to add
    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.

     

    • Reply
    • Quote
     
Answers
  • Wednesday, 2017l 15,200 9 AM

    Manish parab

    Manish parab

    Ag Technologies

    Partner

    85 points 6 0 0 recent achievements forums replies
    I first marked
    Answer forums answerer
    I Manish parab's threads view profile 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 wocould 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.3devil. GeniusWednesday, 2017l 15,200 9 pm
    •  

     

    • Reply
    • Quote
     
All replies
  • Tuesday, 2017l 14,200 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 Steve. Curran MVP's threads view profile 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

     

    • Reply
    • Quote
     
  • Tuesday, 2017l 14,200 9 pm

    Da.3devil. Genius

    Da.3devil. Genius

     

    65 points 2 0 0 recent achievements forums answerer
    I first marked
    Answer da.3devil. Genius's threads view Profile

    65 points 0

    Sign
    In to vote

    Nope, still the same error.

     

    • Reply
    • Quote
     
  • Tuesday, 2017l 14,200 9 pm

    Sathish TK

    Sathish TK

    Partner

    95 points 3 0 0 recent achievements first answer
    Confirmed forums answerer
    I first forums
    Reply sathish TK's threads view profile (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
    Recommended Approach)

    Http://www.sathishtk.com/blog

     

    • Reply
    • Quote
     
  • Tuesday, 2017l 14,200 9 pm

    Moonis Tahir

    Moonis Tahir

    MCC, partner

    8,500 points 12 3 1 Recent achievements Profile
    Complete forums answerer
    IV proposed answerer
    I moonis Tahir's threads view profile (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
    You have permissions to the web and your code is running on SharePoint box,
    There shoshould 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,
    The MCTs SQL 2005.

     

    • Reply
    • Quote
     
  • Tuesday, 2017l 14,200 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 Dave Hunter's threads view profile 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 shocould work

    Privatevoid
    Uploadfiletosharepoint (string strinputfilename)
    {

    String
    Desturl = http://mysite.site.com/sites/appdevelop;
    String destfileurl =
    Desturl + "project % 20 Plans" + "/new.txt;
    Using (spweb site =
    New
    Spsite (desturl). openweb ())
    {


    Site. allowunsafeupdates = true;

    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

     

    • Reply
    • Quote
     
  • Wednesday, 2017l 15,200 9 AM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.