1

Having some trouble returning certain fields from a SharePoint List SOAP request.

Here is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:soap1="http://schemas.microsoft.com/sharepoint/soap/">
   <soap:Header/>
   <soap:Body>
      <soap1:UpdateListItems>
         <soap1:listName>69A3FFFA-782B-45D5-B776-2BE6D5645745</soap1:listName>
         <soap1:updates>
            <Batch OnError="Continue">
            <Method ID="1" Cmd="New">
                <Field Name="Title">New Item</Field>
            </Method>
            </Batch>
         </soap1:updates>
      </soap1:UpdateListItems>
   </soap:Body>
</soap:Envelope>

I am able to use the following Jdom2 code to grab certain values like this:

            // set your name spaces.
            Namespace soap = Namespace.getNamespace("soap","http://www.w3.org/2003/05/soap-envelope");
            Namespace soap1 = Namespace.getNamespace("soap1","http://schemas.microsoft.com/sharepoint/soap/");

            // drill down into elements
            Element rootNode = doc.getRootElement();

            // Get Body node
            Element body = rootNode.getChild("Body",soap);
            // Get UpdateListItem Element
            Element UpdateListItems = body.getChild("UpdateListItems",soap1);
            // Get updates node
            Element updates = UpdateListItems.getChild("updates",soap1);

            // Set list name as String variable
            String listNameString = UpdateListItems.getChild("listName",soap1).getText();

            // Print list text value ** THIS WORKS**
            System.out.println(listNameString);

However, I can't seem to figure out how to select the Field elements. For example: How would I select the "Title" Field?

<Field Name="Title">New Item</Field>

UPDATE:

I also able to get the attribute "Name" from the "Field" element, but can only return or set the name of value of the attribute. I need to be able to access the test within the "Field" Element.

I can get the value of the attribute like this: System.out.println(field.getAttribute("Name").getValue()); // Prints Title

And I can get the name like this: System.out.println(field.getAttribute("Name").getName()); // Prints Name

But, I need to be able to return the text value of the element.

UPDATE 2: I didn't mention. The XML really looks like this:

`    <?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:soap1="http://schemas.microsoft.com/sharepoint/soap/">
   <soap:Header/>
   <soap:Body>
      <soap1:UpdateListItems>
         <soap1:listName>69A3FFFA-782B-45D5-B776-2BE6D5645745</soap1:listName>
         <soap1:updates>
            <Batch OnError="Continue">
            <Method ID="1" Cmd="New">
                <Field Name="Title">New Item</Field>
                <Field Name="Classification" Type="Choice">Funny</Field>
                <Field Name="Title">New Item</Field>
                <Field Name="Title" Type="Text">Funny List Item</Field>
            </Method>
            </Batch>
         </soap1:updates>
      </soap1:UpdateListItems>
   </soap:Body>
</soap:Envelope>`

I can submit this via SoapUI to SharePoint and it works. But if there are multiple "Field" elements, with different attributes, how can I select the correct one via Jdom2?

I can do this: String title = field.getText(); //returns New Item

But how would I be able to grab the text from other "Field" elements that use the "Name" attribute?

luskbo
  • 155
  • 3
  • 18

2 Answers2

1

It is all in the namespaces. You have three of them, soap, soap1, and there's also the default namespace, which, in this case, is "". JDOM designates this namespace as Namespace.NO_NAMESPACE.

So, to get the Field Element from the updates Element, you can do:

Element methods = updates.getChild("Method", Namespace.NO_NAMESPACE);
Element field = methods.getChild("Field", Namespace.NO_NAMESPACE);

These can be made simpler, if you want, by using the getChild method that does not have the namespace parameter at all, like:

Element methods = updates.getChild("Method");
Element field = methods.getChild("Field");

The important thing to see here, is that your document has 3 namespaces, and that the Field element (and Method too) are not in the soap, or soap1 namespace.

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Correct, but i'm trying to get the value in the Field Element for the Name attribute for the title. Right before reading your answer, I was able to replicate you answer. Element field = method.getChild("Field"); However, I need to be able to access the Field element for the Title attribute. So far, I can grabe the attribute "Title" with: Attribute title = field.getAttribute("Title"); but can't seem the get its text value. – luskbo Sep 26 '14 at 19:44
  • I guess I am being dense.... I can;t figure out whether you want the attribute value, or the text value. For either, if you have the `field`, you can get either the attribute or value... But, this makes no sense: *"...trying to get the value in the Field Element for the Name attribute for the title."*... it does not add up. – rolfl Sep 26 '14 at 20:40
  • I just updated my question. I need to get the value inside of the brackets. Its possible I'm making this more difficult than it has to be. From this: `New Item` I need the access the text "New Item" – luskbo Sep 26 '14 at 20:44
0

Thanks for the help rolfl. I figured it out. You can loop through the Child elements to access the different "Field" attributes. I then test for the attribute name to get or set its content. This is the best I could come up with.

    for (Element node : method.getChildren("Field")){ 
        if(node.getAttributeValue("Name").equalsIgnoreCase("Title")){
            node.setText("String");
        }
        System.out.println(node.getAttribute("Name").getValue());
    }
luskbo
  • 155
  • 3
  • 18