locked
Handling SharePoint UpdateListItems XML Response RRS feed

  • Question

  • I'm inserting some items into a SharePoint 2007 list using the Lists web service. I'm trying to write some code to handle any errors reported in the response, but navigating the XML is not working as expected. I'm attempting to get a collection of Result elements, then check each Result's ErrorCode child element for an error code. When I try to get the ErrorCode of the second result, it appears to give me the first result's ErrorCode again even though I'm calling SelectSingleNode on the second Result element. What am I doing wrong? Here is my data and code (with most of the z:row attributes omitted to keep it short):

    SharePoint response:

    - <Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
        - <Result ID="1,New">
              <ErrorCode>0x00000000</ErrorCode> 
              <ID /> 
              <z:row ows_ContentTypeId="0x0100760B0FF12756D249834F5B18A46B1A31" ows_Title="Seas-a-1-1-Target1" ows_PointID="1" ows_X_x0020_Value="355.000000000000" ... xmlns:z="#RowsetSchema" /> 
          </Result>
        - <Result ID="2,New">
              <ErrorCode>0x80020005</ErrorCode> 
              <ErrorText>The operation failed because an unexpected error occurred. (Result Code: 0x80020005)</ErrorText> 
          </Result>
      </Results>
    

    Code:

    System.Xml.XmlNode response = listService.UpdateListItems("SCs", batchElement);
    
    XmlNamespaceManager nsm = new XmlNamespaceManager(response.OwnerDocument.NameTable);
    nsm.AddNamespace("sp", response.NamespaceURI);
    
    XmlNodeList results = response.SelectNodes("//sp:Result", nsm);
    foreach (XmlNode result in results)
    {
        System.Diagnostics.Debug.WriteLine(result.OuterXml);
        XmlNode node = result.SelectSingleNode("//sp:ErrorCode", nsm);
        System.Diagnostics.Debug.WriteLine(node.OuterXml);
    }
    

    Output:

    <Result ID="1,New" xmlns="http://schemas.microsoft.com/sharepoint/soap/"><ErrorCode>0x00000000</ErrorCode><ID /><z:row ows_ContentTypeId="0x0100760B0FFF2756D249834F5B18A46B1A31" ows_Title="Seas-a-1-1-Target1" ows_PointID="1" ows_X_x0020_Value="355.000000000000" ... xmlns:z="#RowsetSchema" /></Result>
    <ErrorCode xmlns="http://schemas.microsoft.com/sharepoint/soap/">0x00000000</ErrorCode>
    <Result ID="2,New" xmlns="http://schemas.microsoft.com/sharepoint/soap/"><ErrorCode>0x80020005</ErrorCode><ErrorText>The operation failed because an unexpected error occurred. (Result Code: 0x80020005)</ErrorText></Result>
    <ErrorCode xmlns="http://schemas.microsoft.com/sharepoint/soap/">0x00000000</ErrorCode>
    

    Tuesday, June 19, 2012 6:06 PM

Answers

  • I didn't realize that my "node" was still a child of "results". So when I use //sp:ErrorCode, it's getting the first ErrorCode element in the document because // means the document root, which it still has access to even though I'm calling it on "result". I instead need:

    XmlNode node = result.SelectSingleNode("./sp:ErrorCode", nsm);

    to start with the current node.
    • Marked as answer by _xr280xr_ Tuesday, June 19, 2012 9:33 PM
    • Edited by _xr280xr_ Tuesday, June 19, 2012 9:34 PM
    Tuesday, June 19, 2012 9:33 PM

All replies

  • I didn't realize that my "node" was still a child of "results". So when I use //sp:ErrorCode, it's getting the first ErrorCode element in the document because // means the document root, which it still has access to even though I'm calling it on "result". I instead need:

    XmlNode node = result.SelectSingleNode("./sp:ErrorCode", nsm);

    to start with the current node.
    • Marked as answer by _xr280xr_ Tuesday, June 19, 2012 9:33 PM
    • Edited by _xr280xr_ Tuesday, June 19, 2012 9:34 PM
    Tuesday, June 19, 2012 9:33 PM
  • Hi Xr280xr,

    Thank you for sharing this solution here.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 20, 2012 10:52 AM