0

I'm writing a .Net app that adds new entries to a SharePoint list and then attaches a file to that entry. The code below only works each day after the user has already added an item with an attachment to that list on that day. The next day, it stops working again. I'm using SharePoint 2010. Here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace SharePointListUpdateTest01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdPostComment_Click(object sender, EventArgs e)
        {
            CommentsListSOAP.ListsSoapClient lsc = new CommentsListSOAP.ListsSoapClient();
            const String sSharePointListGUID = "{GUID goes here}";

            try
            {
                XNamespace SharePointNS = "http://schemas.microsoft.com/sharepoint/soap/";
                // Load a file.
                byte[] pbyFileContents = null;
                string sFileName = null;
                using (FileStream fs = new FileStream("..\\..\\download.jpg", FileMode.Open, FileAccess.Read))
                {
                    sFileName = fs.Name;
                    using (BinaryReader freader = new BinaryReader(fs))
                    {
                        pbyFileContents = freader.ReadBytes((int)fs.Length);
                    }
                }

                lsc.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
                lsc.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://<<Path/to/site>>/_vti_bin/Lists.asmx");

                XElement xmleList = lsc.GetList(sSharePointListGUID);

                var xmldBatchUpdate = new XDocument(
                    new XDeclaration("1.0", "utf-8", null),
                    new XElement("Root",
                        new XElement("Batch",
                            //new XAttribute("OnError", "Continue"),
                            new XAttribute("ListVersion", "1"),
                            new XAttribute("ViewName", ""),
                            new XElement("Method", new XAttribute("ID", "1"), new XAttribute("Cmd", "New"),
                                new XElement("Field", new XAttribute("Name", "ID"), "New"),
                                new XElement("Field", new XAttribute("Name", "Title"), "Test comment on " + DateTime.Now.ToString()),
                                new XElement("Field", new XAttribute("Name", "comments1"), txtComment.Text),
                                new XElement("Field", new XAttribute("Name", "look"), 1),
                                new XElement("Field", new XAttribute("Name", "choice_column"), "#2"),
                                new XElement("Field", new XAttribute("Name", "look2"), 3),
                                new XElement("Field", new XAttribute("Name", "AssignedTo"), "-1;#" + txtAssignedTo.Text)
                                //new XElement("Field", new XAttribute("Name", "AssignedTo"), txtAssignedTo.Text)
                            )
                        )
                    )
                );

                XElement xmleUpdateResult = lsc.UpdateListItems(sSharePointListGUID, xmldBatchUpdate.Root.Element("Batch"));

                long iErrorCode = Convert.ToInt64(xmleUpdateResult.Element(SharePointNS + "Result").Element(SharePointNS + "ErrorCode").Value, 16);
                if (iErrorCode != 0)
                {
                    string sErrText = xmleUpdateResult.Element(SharePointNS + "Result").Element(SharePointNS + "ErrorText").Value;
                    MessageBox.Show("Error when adding item to SharePoint list: " + "(" + Convert.ToString(iErrorCode, 16) + ")" + sErrText);
                }
                else
                {
                    string sNewElementID = xmleUpdateResult.Element(SharePointNS + "Result").Element("{#RowsetSchema}row").Attribute("ows_ID").Value;
                    lsc.AddAttachment(sSharePointListGUID, sNewElementID, "download1.jpg", pbyFileContents);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
        }
    }
}

Running this first thing in the morning, the call to UpdateListItems will fail with error text "The user does not exist or is not unique". If I go to the list in my browser (Internet Explorer) and add an item to the list, that works, and subsequently calls to UpdateListItems work correctly (until the next day). Once I'm past that point, the call to AddAttachment throws the following exception:

System.ServiceModel.FaultException was caught
  Message=Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    Exception rethrown at [0]: 
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at SharePointListUpdateTest01.CommentsListSOAP.ListsSoap.AddAttachment(AddAttachmentRequest request)
       at SharePointListUpdateTest01.CommentsListSOAP.ListsSoapClient.SharePointListUpdateTest01.CommentsListSOAP.ListsSoap.AddAttachment(AddAttachmentRequest request) in G:\Research\WS\SharePointListUpdateTest01\SharePointListUpdateTest01\Service References\CommentsListSOAP\Reference.cs:line 2893
       at SharePointListUpdateTest01.CommentsListSOAP.ListsSoapClient.AddAttachment(String listName, String listItemID, String fileName, Byte[] attachment) in G:\Research\WS\SharePointListUpdateTest01\SharePointListUpdateTest01\Service References\CommentsListSOAP\Reference.cs:line 2903
       at SharePointListUpdateTest01.Form1.cmdPostComment_Click(Object sender, EventArgs e) in G:\Research\WS\SharePointListUpdateTest01\SharePointListUpdateTest01\Form1.cs:line 82
  InnerException:

If I now manually add a new item with an attachment to this list then that works and all subsequent calls to AddAttachment work just fine, until the next day. Checking the SharePoint server logs shows that AddAttachment causes:

"Unknown SPRequest error occurred. More information: 0x80020009", followed by "SOAP exception: Microsoft.SharePoint.SPException: Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0x80030102 (STG_E_REVERTED))

Any help would be appreciated.

P.S. the list is on my own personal site on a corporate SharePoint server so I have full control over the list.

0

I eventually resolved this problem by switching to the older .Net 2.0 web services interface (clicking on the "Advanced" button on the add services dialog). That version of SharePoint apparently used .Net 2.0 hence something was not be sent/received correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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