I am trying to copy the attachments from a list to a document library. When I try to write into a document library, occasionally I am getting following error "The remote server returned an error: (500) Internal Server Error. Cannot access a closed file. Cannot access a closed file. "
Any ideas on what is causing the problem. The code is specified below, The error message is specified after the code. Can you please let me know if I am missing anything. I cant find anything wrong in the code or in the library. The code is running as WCF Service and the call to process this is made from Infopath form.
string filePath = "c:\\Nate\\LogV2_" + string.Format("text-{0:yyyy-MM-dd_hh-mm-ss-tt}.txt", DateTime.Now); ; Stream myFile = File.Create(filePath); TextWriterTraceListener myTextListener = new TextWriterTraceListener(myFile); Trace.Listeners.Add(myTextListener); Trace.WriteLine("List Attachment processor for complete status"); try { SPSecurity.RunWithElevatedPrivileges(delegate() { string siteURL = "Site URL"; using (SPSite site = new SPSite(siteURL)) { SPWeb oWeb = site.OpenWeb(); SPList oList = oWeb.Lists["List Name"]; SPList docDestination = oWeb.Lists["Destination Library on same site"]; SPFolder fldRoot = oWeb.Folders["Destination Library on same site"]; SPFileCollection flColl = null; SPQuery oQuery = new SPQuery(); oQuery.Query = "<Where><Eq>" + "<FieldRef Name=\"ID\"/><Value Type=\"Number\">" + RequestID + "</Value></Eq></Where>"; SPListItemCollection oItems = oList.GetItems(oQuery); foreach (SPListItem lstItem in oItems) { // myListener.WriteLine("List Item : " + item["ID1"].ToString()); Trace.WriteLine("SPListItem oItems ID : " + lstItem["ID"].ToString() + " RequestID : " + RequestID); if (lstItem["ID"].ToString() == RequestID) { if (lstItem.Attachments != null && lstItem.Attachments.Count > 0) { foreach (String strName in lstItem.Attachments) { flColl = fldRoot.Files; SPListItem listtem = docDestination.Items.Add(); SPFile FileCopy = lstItem.ParentList.ParentWeb.GetFile(lstItem.Attachments.UrlPrefix + strName); SPQuery oQueryDest = new SPQuery(); oQueryDest.Query = "<Where><Eq>" + "<FieldRef Name=\"Name\"/><Value Type=\"Text\">" + FileCopy.Name + "</Value></Eq></Where>"; SPListItemCollection collListItems = docDestination.GetItems(oQuery); string destFile = flColl.Folder.Url + "/" + FileCopy.Name ; if (collListItems.Count > 0) { String strFileNamePrfix = FileCopy.Name.Substring(0, FileCopy.Name.Length - 3); strFileNamePrfix = strFileNamePrfix + "_" + collListItems.Count.ToString(); strFileNamePrfix = strFileNamePrfix + FileCopy.Name.Substring(FileCopy.Name.Length - 3,3); destFile = flColl.Folder.Url + "/" + strFileNamePrfix; } //string destFile = flColl.Folder.Url + "/" + FileCopy.Name ; byte[] fileData = FileCopy.OpenBinary(); Trace.WriteLine("Board Services dest file" + destFile + " Count = " + collListItems.Count.ToString()); //SPFile flAdded = flColl.Add(destFile, fileData, site.RootWeb.CurrentUser, site.RootWeb.CurrentUser, Convert.ToDateTime(lstItem[SPBuiltInFieldId.Created]), Convert.ToDateTime(lstItem[SPBuiltInFieldId.Modified])); SPFile flAdded = flColl.Add(destFile, fileData,true); SPListItem item = flAdded.Item; //item[SPBuiltInFieldId.Created] = Convert.ToDateTime(lstItem[SPBuiltInFieldId.Created]); //item[SPBuiltInFieldId.Modified] = Convert.ToDateTime(lstItem[SPBuiltInFieldId.Modified]); flAdded.Item.Update(); } } } } } }); //Trace.Flush(); //myFile.Close(); } catch (Exception ex) { string strLogEntry = ex.Message.ToString() + ex.StackTrace.ToString(); Trace.WriteLine(strLogEntry); //Trace.Close(); //throw ex; } finally { Trace.Flush(); myFile.Close(); }
Form submission failed. (User: xxxxx, Form Name: Template, IP: , Request: http://Site Url/Lists/List name/Task/editifs.aspx?List=e6647e38-cb01-46b3-8bee-308357045532&ID=116&Source=http://Site URL/Lists/List Name/AllItems.aspx?View={CB9F3C2B-49DF-4D75-9720-1E7E000FD229}&FilterField1=Status&FilterValue1=Completed&IsDlg=1&Web=706d51c9-e509-40f3-884a-7d40e126ff14,
Form ID: urn:schemas-microsoft-com:office:infopath:list:-AutoGen-2013-12-06T16:11:54:747Z, Type: DataAdapterException, Exception Message: The remote server returned an error: (500) Internal Server Error. Cannot access a closed file. Cannot
access a closed file.
at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
The remote server returned an error: (500) Internal Server Error.)