Gaurav Rehan

Hi,

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";
}

try
{
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

Thanks

Gaurav Rehan




Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

Sahil

Where exactly are you getting the error.

Check the user context with which you are creating the code. If it is a web application make sure you open the site object with the right user context.

Regards,
Sahil






Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

Gaurav Rehan

Hi Sahil,

Thanks for reply.
The application is a console app that i am running on the same server where the MOSS2007 is installed. I tried to impersonate, that also didnt worked.
I am getting UnauthorizeAccessException at the time of calling
folder.Files.Add().


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

Thanks,
gaurav.





Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

SheDezy

Make sure the ID that is running the service or application has full contol to your doc library.




Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

Gaurav Rehan

Hi, The problem is resolved.

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

Thanks for your responses.

Gaurav






Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

AshishKSrivastava

Gaurav,

I get the similar error, but "/" doesnt seems to be any solution. What URL should be passed from the code, is it the the same url which is used to access the sharepoint from the browser e.g.

http://server/portalname/documents

can anyone help me please

Ashish





Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

Gaurav Rehan

Hi Ashish,

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.

Thanks,

Gaurav.






Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

SheDezy

Are you guys able to overwrite files that already exist in a document library I'm able to upload, but not overwrite.




Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

Gaurav Rehan

If you want to overwrite, then you should checkout first and then checkin the new version.




Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

SheDezy

So I should
check out
then add
then check in

how would i add that to something like this:

SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPFolder folder = web.GetFolder(pathFolder);
string fileURL = fileName;


SPFile file = folder.Files.Add(fileURL, fileContents, true);
return file.TimeCreated.ToLongDateString() + "::" + file.Title;






Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

Gaurav Rehan

1) ensure that fileURL is kind of SPFolderName + "/" + filename

2) Now try this:

SPFile sfile = web.GetFile(fileURL);

if (sfile != null)

{

sfile.CheckOut();

folder.Files.Add(fileURL, fileContents, true);

sfile.CheckIn("Add your comments here as checkin comments");

}

else

{

folder.Files.Add(fileURL, fileContents, true);

}

This should work.






Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

SheDezy

not sure what i'm doing wrong here:

<%@ WebService Language="C#" Class="UploadService.Files" %>

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.IO;
using System.Web.Services;

[WebService(Namespace = "Microsoft.SharePoint.Administration")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Files : System.Web.Services.WebService

{

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

SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPFolder folder = web.GetFolder(pathFolder);
string fileURL = fileName;
SPFile sfile = web.GetFile(fileURL);
if (sfile != null)

{
sfile.CheckOut();

folder.Files.Add(fileURL, fileContents, true);

sfile.CheckIn("checked in");
}

else
}
SPFile file = folder.Files.Add(fileURL, fileContents, true);
return file.TimeCreated.ToLongDateString() + "::" + file.Title;
{

}
catch (System.Exception ex)
{
return ex.Message + "::" + ex.Source;
}
}

}




Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

AshishKSrivastava

SheDezy,

Could you please post the actual error I had a different problem related to Authentication. It could be Authentication problem.

Ashish





Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

SheDezy

I'm running it as a service, so the only errors I see are in the event viewer. Unfortunately, the error is always the same no matter what I change. Here it is:

System.Web.Services - The request failed with the error message:
--
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://ussrm-ws1:25594/Docs/_layouts/1033/error.aspx ErrorText=c%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5Cweb%20server%20extensions%5C12%5CISAPI%5CFiles%2Easmx%2831%29%3A%20error%20CS0029%3A%20Cannot%20implicitly%20convert%20type%20%27void%27%20to%20%27Microsoft%2ESharePoint%2ESPFile%27">here</a>.</h2>
</body></html>

--. - - at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at WSUploader.sp.Files.UploadDocument(String fileName, Byte[] fileContents, String pathFolder)
at WSUploader.Uploader.ServiceTimer_Tick(Object sender, ElapsedEventArgs e)


Keep in mind, i am able to upload fine using this code:

<%@ WebService Language="C#" Class="UploadService.Files" %>

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.IO;
using System.Web.Services;

[WebService(Namespace = "Microsoft.SharePoint.Administration")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Files : System.Web.Services.WebService

{

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

SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPFolder folder = web.GetFolder(pathFolder);
string fileURL = fileName;
SPFile sfile = web.GetFile(fileURL);


SPFile file = folder.Files.Add(fileURL, fileContents, true);
return file.TimeCreated.ToLongDateString() + "::" + file.Title;



}
catch (System.Exception ex)
{
return ex.Message + "::" + ex.Source;
}
}

}


My problem is that I cannot get the files to overwrite. Could it still be a permissions issue




Re: SharePoint - Enterprise Content Management "Access denied" while uploading a document to sharepoint 2007 folder

SheDezy

One more thing...

When I use the code that works, and then upload a file of the same name...
My event viewer says that the file uploaded successfully but my log says this:

A file with the name IBM Reports/testreport.txt already exists. It was last modified by SHAREPOINT\system on 18 May 2007 10:06:47 -0500.

and when I check the file in the document library, it is still the originally uploaded file, no change.