MOSS Forum

Ask Question   UnAnswered
Home » Forum » MOSS       RSS Feeds

"Access denied" while uploading a document to sharepoint 2007 folder

  Asked By: Britney    Date: Nov 09    Category: MOSS    Views: 3128

I am looking forward to manage documents using Sharepoint 2007 libraries. I am trying to upload a simple text file by following code snippet:

public string UploadDocument(string fileName, byte[] fileContents, string pathFolder)
if (fileContents == null)
return "Null Attachment";

int iStartIndex = pathFolder.LastIndexOf("/");
string sitePath = pathFolder.Remove(iStartIndex);
string folderName = pathFolder.Substring(iStartIndex + 1);

SPSite site = new SPSite(sitePath);
SPWeb web = site.OpenWeb();

SPFolder folder = web.GetFolder(folderName);

string fileURL = fileName;

folder.Files.Add(fileURL, fileContents);

if (folder.Files[fileURL].CheckedOutBy.Name != "")
folder.Files[fileURL].CheckIn("File Checked In");

return "File added successfully!";

catch (System.Exception ex)
return "Error: " + ex.Source + " - " + ex.Message;

I am getting following error at the time the file is being added to the folder.

"Access denied" at Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException ex)

can someone help me out of this?



2 Answers Found

Answer #1    Answered By: Corrine Potts     Answered On: Nov 09

the issue was with the fileUrl. when I changed the fileUrl to foldername + "\" + filename. it worked.

Answer #2    Answered By: Kabeer Karkare     Answered On: Nov 09

I did following:

FileInfo finfo = new FileInfo(fileName); //fileName is the path of the file to be uploaded

string fileURL = folder.Url + "/" + finfo.Name; //folder is SPFolder, destination folder.

folder.Files.Add((fileURL, fileContents); //fileContent is the byte array carrying the content of the file

Try doing this, and see if it works for you.