Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy Attachments using GetFile

Status
Not open for further replies.

juergenbauer

Programmer
Feb 23, 2006
1
DE
Hi everyone,

developing a Web Part that is copying an entry from a list to another list (both Issue Lists) I currently faced to a problem:
I have two list items - one is a already existing issue (the 'currentItem'), the other one is a new entry (the 'newRequestItem') to be created in an other list.
I already copied the field values and now i would like to get the attachments as well.

... some code ...
Code:
SPAttachmentCollection currentAttachments = currentItem.Attachments; 
SPAttachmentCollection newRequestAttachments = newRequestItem.Attachments;

int nofAttachments = currentAttachments.Count;
output.Write(SPEncode.HtmlEncode("nofAttachments: "+ nofAttachments) + "<BR>"); // number of attachments is correctly dispayed

string urlString = currentAttachments.UrlPrefix;
output.Write(SPEncode.HtmlEncode("attachment URL: "+ urlString) + "<BR>"); // the URL is also correctly dispayed

string fileName;
string fileUrl; 

for (int p = 0; p < nofAttachments; p++)
{ 
fileName = currentAttachments[p];
fileUrl = currentAttachments.UrlPrefix + fileName;
output.Write(SPEncode.HtmlEncode("File to get: "+ fileUrl) + "<BR>"); // the URL+Filename is correctly dispayed

string attachFileString = changeRequestSite.GetFileAsString(fileUrl);
output.Write(SPEncode.HtmlEncode("String from File: "+ attachFileString) + "<BR>"); // no problem to get the content of the file

SPFile attachFile = changeRequestSite.GetFile(fileUrl); // ERROR: Value does not fall within the expected range.

byte[] binFile = attachFile.OpenBinary(); // code not executed 

newRequestAttachments.Add(fileName, binFile); // code not executed
}
newRequestItem.Update(); // code not executed
... some more code ...

As described under the 'SPWeb'-class in the SDK-Reference the public method 'GetFile' should return the file object (SPFile) located at the URL specified in the provided parameter string. However, the hard I try no SPFile is returned. Is there something wrong with my code or is there a mistake in the SDK?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top