Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have the chunk of code below:

SPListItem source = //some list item;
foreach (string file in source.Attachments)
{
    SPFile oFile = source.Web.GetFile(file);
    string strFilename = oFile.Name;
    byte[] binFile = oFile.OpenBinary();  //Errors out here!
    destination.Attachments.Add(oFile.Name, binFile);
    destination.SystemUpdate();
}

Which throws this error:

EXCEPTION in CrossSiteCopy! Cannot open file "2012-05-21_104000.png". at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx) at Microsoft.SharePoint.Library.SPRequest.GetFileAsStream(String bstrUrl, String bstrWebRelativeUrl, Boolean bHonorLevel, Byte iLevel, OpenBinaryFlags grfob, String bstrEtagNotMatch, String& pEtagNew, String& pContentTagNew) at Microsoft.SharePoint.SPFile.GetFileStreamCore(OpenBinaryFlags openOptions, String etagNotMatch, String& etagNew, String& contentTagNew) at Microsoft.SharePoint.SPFile.GetFileStream(OpenBinaryFlags openOptions, String etagNotMatch, String& etagNew, String& contentTagNew) at Microsoft.SharePoint.SPFile.OpenBinary(SPOpenBinaryOptions openOptions) at Copy_Special.CrossSiteCopy.CopyFieldValues(SPListItem source, SPListItem destination) at Copy_Special.CrossSiteCopy.<>c_DisplayClass1.b_0() at Microsoft.SharePoint.SPSecurity.<>c_DisplayClass4.b_2() at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) at Copy_Special.CrossSiteCopy.Execute(ActivityExecutionContext executionContext)

It's not giving me much information about WHY it cannot open the file. Can any one point me to what the problem might be?

share|improve this question
    
Can you check the SP log? Also, Can you make sure the source exists (wherever the file is)? –  Maria Burton Jun 13 '12 at 16:03

1 Answer 1

You need to specify more than just the attachment file name in the SPWeb.GetFile method! Specify the Web relative file URL or the server relative file URL here. You can construct the Web relative file URL from source.Attachments.UrlPrefix + file

share|improve this answer
1  
Instead of just using + to combine URLs you should use SPUrlUtility.CombineUrls –  Per Jakobsen Jun 13 '12 at 17:56
    
Thx was not aware of this util method! Anyway, I agree and am also using my own util Url.Combine that is not so SharePoint specific. It also takes any number of string inputs for combination. –  Lars Fastrup Jun 14 '12 at 7:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.