4.3 Use UpdateLists to Insert Items into Two Lists with Lookup Relationships

This example shows how to use UpdateLists to insert list items into two lists that have lookup relationships. In this example, List 1 is referred to by GUID {E5BDB272-1DFB-4752-903E-BF7BFF2052FE}. List 2 is referred to by GUID {3B6DEE82-D5AC-4ACE-A6E1-00774FA1E10F}. List 1 has a field named "Occupation" that is of lookup type. It references the id attribute of the list items in List 2. The list item to be inserted into List 1 references a list item that is to be inserted into List 2 in the same UpdateLists call.

The protocol client sends the following message to the protocol server to insert list items into two lists in one call:

 <?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
     <UpdateLists xmlns="http://schemas.microsoft.com/office/Access/Server/WebServices/AccessServer/"> 
       <u cmd="i" ln="{3B6DEE82-D5AC-4ACE-A6E1-00774FA1E10F}" ut="0" id="-1">
         <f n="JobTitle" v="Sales Representative" />
         <f n="_OldID" v="2" />
       </u>
      <u cmd="i" ln="{E5BDB272-1DFB-4752-903E-BF7BFF2052FE}" ut="4" id="-1">
        <f n="FullName" v="Nancy Freehafer" />
        <f n="Account" v="nancy@northwindtraders.com" />
        <f n="_OldID" v="4" />
        <f n="Occupation" v="-1" />
      </u>
      <par>false</par>
    </UpdateLists> 
  </soap:Body>
</soap:Envelope>

Because there are two lists to be updated, there are two u elements in the message that is shown later. In the first u element, ln equals {3B6DEE82-D5AC-4ACE-A6E1-00774FA1E10F}, meaning the insertion happens to List 2. The second u element in the message that is shown later has the ln attribute equals {E5BDB272-1DFB-4752-903E-BF7BFF2052FE}, referring to List 1.

If the list item to be inserted to List 1 references a list item that already exists in List 2 on the protocol server, the value of the field named "Occupation" is filled with the identifier of the list item being referenced. However, in this example, the list item to be inserted to List 1 references a list item that is to be inserted in List 2 in the same UpdateLists call. Therefore, at the time the protocol client sends the message, it does not know the identifier value for the list item being referenced. However, it knows that in the first u element, the id attribute equals "-1". That is the list item to be inserted into List 2. Therefore, the value of the fourth f element in the second u element is "-1" to refer to the list item to be inserted in List 2 in the first u element of this UpdateLists call.

The protocol server responds with the following message:

 <?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body> 
     <UpdateListsResponse xmlns="http://schemas.microsoft.com/office/Access/Server/WebServices/AccessServer/">
       <UpdateListsResult>
         <mit xsi:nil="true" />
         <Update ec="0" em="" cmd="i" ut="0" ln="{3B6DEE82-D5AC-4ACE-A6E1-00774FA1E10F}" id="1" >
           <f n="JobTitle" v="Sales Representative" />
           <f n="_OldID" v="2" />
           <f n="LinkTitleNoMenu" v=""/>
           <f n="Editor" v="25;#Andrew Cencini" />
           <f n="Author" v="25;#Andrew Cencini" />
           <f n="Modified" v="05/7/2009 06:32:01" />
           <f n="Created" v="05/7/2009 06:32:01"/>
           <f n="ID" v="1" />
           <f n="owshiddenversion" v="1" />
           <f n="Attachments" v="False" />
         </Update>
         <Update ec="0" em="" cmd="i" ut="4" ln="{E5BDB272-1DFB-4752-903E-BF7BFF2052FE}" id="1" > 
          <f n="FullName" v="Nancy Freehafer" />
          <f n="Account" v="nancy@northwindtraders.com" />
          <f n="_OldID" v="4" />
          <f n="LinkTitleNoMenu" v="" />
          <f n="Editor" v="25;#Andrew Cencini" />
          <f n="Author" v="25;#Andrew Cencini" />
          <f n="Modified" v="05/7/2009 06:32:01" />
          <f n="Created" v="05/7/2009 06:32:01" />
          <f n="Occupation" v="1;#Sales Representative" />
          <f n="ID" v="1" />
          <f n="owshiddenversion" v="1" />
          <f n="Attachments" v="False" />
        </Update>
      </UpdateListsResult>
    </UpdateListsResponse>
  </soap:Body>
</soap:Envelope>

In the first Update element, the list item is assigned an id attribute value "1" in List 2 ({3B6DEE82-D5AC-4ACE-A6E1-00774FA1E10F}). Once the new list item was created and assigned a permanent id, the protocol server performed identifier fix up on the remaining list items. Therefore, in the second Update element, the f element named "Occupation" in List 1 ({E5BDB272-1DFB-4752-903E-BF7BFF2052FE}) has the value "1; # Sales Representative" because it is referring to the list item with id = 1 in List 2.