"Attempted to use an object that has ceased to exist" Error with lists.asmx

0

I am using a .NET application that creates a SharePoint list item through a form interface. The application needs to allow the user to upload attachments to the list item. I can create the list item, however when using the list.asmx service addAttachment method, I get a "Attempted to use an object that has ceased to exist" error when attempting to attach files through a FileUpload control, where the file is stored in session as a byte array.

My code looks like this...

Creating the list item:

protected void CreateListIteminSharepoint()
        {
            try
            {
                ServiceReference1.ListsSoapClient proxy = new ServiceReference1.ListsSoapClient();
                proxy.ClientCredentials.Windows.ClientCredential = new NetworkCredential();

                XElement list = proxy.GetList("MyList");

                // assigning field values...
                string strBatch = "<Method ID='1' Cmd='New'><Field Name='ID' >New</Field>

                // field assignments omitted to shorten code paste

                StringReader reader = new StringReader(strBatch);
                XElement method = XElement.Load(reader);
                XElement element = new XElement("Batch");
                element.SetAttributeValue("OnError", "Continue");
                element.SetAttributeValue("PreCalc", "TRUE");
                element.SetAttributeValue("ListVersion", "1");
                element.SetAttributeValue("ViewName", listViewGuid);
                element.Add(method);
                XElement ndReturn = proxy.UpdateListItems("MyList", element);

                // post headshot image attachment - function in code further down the post
                if (Session["Headshot"] != null)
                {
                    var headshotFile = (byte[])Session["Headshot"];
                    UploadDocToSharepoint(headshotFile, (string)Session["HeadshotFileName"]);
                }
            }
            catch (Exception ex)
            {
                    throw;
            }
        }

FileUpload control button event:

protected void btnHeadShotUpload_Click(object sender, EventArgs e)
{
    if (HeadshotUpload.HasFile)
    {
        var file = HeadshotUpload.FileBytes;
        Session.Add("Headshot", file);
        Session.Add("HeadshotFileName", headshotFileName + " - " + HeadshotUpload.FileName);
    }
}

Attachment function:

public void UploadDocToSharepoint(byte[] sourceFile, string destinationFileName)
        {
                ServiceReference1.ListsSoapClient proxy = new ServiceReference1.ListsSoapClient();
                proxy.ClientCredentials.Windows.ClientCredential = new NetworkCredential();

                try
                {
                    string addAttach = proxy.AddAttachment("MyList", "3228",
                        "image.jpg", sourceFile);
                    MessageBox.Show(addAttach);
                }

                catch (System.Web.Services.Protocols.SoapException ex)
                {
                    throw;
                }

                catch (Exception ex)
                {
                    throw;
                }
        }

ULS Log on SharePoint Server:

SOAP exception: Microsoft.SharePoint.SPException: Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0x80030102 (STG_E_REVERTED)) ---> System.Runtime.InteropServices.COMException (0x80030102): Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0x80030102 (STG_E_REVERTED))

In the attachment function, the 3228 list item ID is an existing list item I was using for testing. I have verified that this list item does exist in the SharePoint list. The desired end result would be to attach the file to the list item that is created just before the attachment is uploaded.

Can anyone provide any insight as to what might be causing this error?

.net
web-services
sharepoint
sharepoint-2010
asked on Stack Overflow Oct 29, 2015 by csunder

1 Answer

0

I was able to get the attachment function working. For some reason, SharePoint did not like the array from Session that was being passed into the addAttachment function. Passing a Memorystream into the addAttachment function's byte array parameter worked properly.

I'm not sure why this was necessary, but it did solve the problem. Examining each of the byte arrays revealed them to be identical as far as I could tell.

answered on Stack Overflow Nov 9, 2015 by csunder

User contributions licensed under CC BY-SA 3.0