I am getting a 403 Forbidden access when I try to have my C# web part add a file to the _layout/mapped folder on the SharePoint server. There must be another way. The code runs fine in development of course because I have Site Collection Administrator permissions.
The code is wrapped in RunWithElevatedPrivileges but this is where the 403 is coming from.
I believe the problem lies in the fact that the file needs to be created before it can be
uploaded. The code turns a string into a file to be uploaded (It is an InfoPath Form in a document library).
The code:
1) takes a row from the .csv file supplied by the user,
2) adds the pertinent fields into a string template of XML code(InfoPath),
3) saves that code in a string array ("res[x]" below),
4) that string is saved via filestream in the _layouts mappedfolder as a
.txt file,
5) and the .txt file is uploaded into the existing SharePoint Document
Library ("SubFolder" below) with an .xml extension ("fileToUpload" below):
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string myMapPathLayouts =
Page.Server.MapPath("/_layouts/ConvertCSVtoXML_WebPart/");
string tempFile = myMapPathLayouts + "/" + "MyTest" + x + ".txt";
using (FileStream fileStream = File.Create(tempFile))
{
AddText(fileStream, res[x]);
SPFile spfile = SubFolder.Files.Add(fileToUpload, fileStream, replaceExistingFiles);
SubFolder.Update();
}
File.Delete(tempFile);
});
Please help...