-
Tuesday, April 14, 2009 6:44 PMda.3vil.genius
da.3vil.genius
65 Points 2 0 0Recent AchievementsForums Answerer I First Marked AnswerHello,
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 AMmanish parab
manish parab
Ag technologies
Partner
85 Points 6 0 0Recent AchievementsForums Replies I First Marked Answer Forums Answerer IAg technologies(Partner)85 PointsI 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 PMSteve.Curran MVP
Steve.Curran MVP
KnowledgeLake
Partner, MVP
29,435 Points 10 4 2Recent AchievementsProfile Complete Proposed Answerer I Forums Replies IVKnowledgeLake(Partner, MVP)29,435 PointsTry 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 PMda.3vil.genius
da.3vil.genius
65 Points 2 0 0Recent AchievementsForums Answerer I First Marked AnswerNope, still the same error.
-
Tuesday, April 14, 2009 7:56 PMSathish TK
Sathish TK
Partner
95 Points 3 0 0Recent AchievementsFirst Answer Confirmed Forums Answerer I First Forums Reply(Partner)95 PointsAre 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 PMMoonis Tahir
Moonis Tahir
MCC, Partner
8,500 Points 12 3 1Recent AchievementsProfile Complete Forums Answerer IV Proposed Answerer I(MCC, Partner)8,500 Pointswhere 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 PMDave Hunter
Dave Hunter
Portaltech Consulting Li...
Partner, MVP
20,310 Points 11 4 1Recent AchievementsProposed Answerer I Forums Answerer IV Forums Replies IVPortaltech Consulting Li...(Partner, MVP)20,310 PointsYou 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 AMmanish parab
manish parab
Ag technologies
Partner
85 Points 6 0 0Recent AchievementsForums Replies I First Marked Answer Forums Answerer IAg technologies(Partner)85 PointsI 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 AMHemendra Agrawal
Hemendra Agrawal
Wartsila
Partner
21,190 Points 20 6 2Recent AchievementsCode Answerer III New Blog Rater New Blog CommentatorWartsila(Partner)21,190 PointsHi,
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 AMAmit.AK.Kumar
Amit.AK.Kumar
Partner
1,350 Points 10 2 0Recent AchievementsForums Replies III Proposed Answerer I First Helpful Vote(Partner)1,350 PointsPlease 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 AMHemendra Agrawal
Hemendra Agrawal
Wartsila
Partner
21,190 Points 20 6 2Recent AchievementsCode Answerer III New Blog Rater New Blog CommentatorWartsila(Partner)21,190 PointsHi,
Try this:
SPfolder.Files.add(filename, content, false );
You may be refer this also:
Let us know your result
Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see" -
Wednesday, January 12, 2011 5:25 AMAkilaskk
Akilaskk
40 Points 4 0 0Recent AchievementsForums Answerer I First Helpful Vote First Forums Replyhi,
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 AMHemendra Agrawal
Hemendra Agrawal
Wartsila
Partner
21,190 Points 20 6 2Recent AchievementsCode Answerer III New Blog Rater New Blog CommentatorWartsila(Partner)21,190 PointsHi,
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 AMAmit.AK.Kumar
Amit.AK.Kumar
Partner
1,350 Points 10 2 0Recent AchievementsForums Replies III Proposed Answerer I First Helpful Vote(Partner)1,350 PointsPlease 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