4.4 Add a Lookup Field

In this example, a new lookup field is added to an existing list that points to another existing list. The following steps provide an overview of this operation.

  1. Get the context from the protocol server that corresponds to the URL where the service is called.

  2. Get the associated Web object (1) from the context.

  3. Get the necessary list information from Web (Request 1).

  4. Add a lookup field to "List 1" that points to "List 2" (Request 2).

Request 1:

 <Request AddExpandoFieldTypeSuffix="true" SchemaVersion="14.0.0.0" LibraryVersion="14.0.4104.1000" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">
   <Actions>
     <ObjectPath Id="2" ObjectPathId="1" />
     <ObjectPath Id="4" ObjectPathId="3" />
     <ObjectPath Id="6" ObjectPathId="5" />
     <ObjectPath Id="8" ObjectPathId="7" />
     <ObjectIdentityQuery Id="9" ObjectPathId="7" />
     <ObjectPath Id="11" ObjectPathId="10" />
     <ObjectIdentityQuery Id="12" ObjectPathId="10" />
     <Query Id="13" ObjectPathId="10">
       <Query SelectAllProperties="false">
         <Properties>
           <Property Name="Id" ScalarProperty="true" />
         </Properties>
       </Query>
     </Query>
   </Actions>
   <ObjectPaths>
     <StaticProperty Id="1" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
     <Property Id="3" ParentId="1" Name="Web" />
     <Property Id="5" ParentId="3" Name="Lists" />
     <Method Id="7" ParentId="5" Name="GetByTitle">
       <Parameters>
         <Parameter Type="String">List 1</Parameter>
       </Parameters>
     </Method>
     <Method Id="10" ParentId="5" Name="GetByTitle">
       <Parameters>
         <Parameter Type="String">List 2</Parameter>
       </Parameters>
     </Method>
   </ObjectPaths>
 </Request>

Response 1:

 [
 {
     "SchemaVersion": "14.0.0.0", "LibraryVersion": "14.0.4104.1000", "ErrorInfo": null
 }, 2, {
     "IsNull": false
 }, 4, {
     "IsNull": false
 }, 6, {
     "IsNull": false
 }, 8, {
     "IsNull": false
 }, 9, {
     "_ObjectIdentity_": "740c6a0b-85e2-48a0-a494-e0f1759d4aa7:web:eb66d01f-d638-41f8-81d7-1a58dd459268:list:6c4bd5ac-b10c-46f0-9553-be32439987d7"
 }, 11, {
     "IsNull": false
 }, 12, {
     "_ObjectIdentity_": "740c6a0b-85e2-48a0-a494-e0f1759d4aa7:web:eb66d01f-d638-41f8-81d7-1a58dd459268:list:da4af6bf-43e6-463a-a95b-ece4183a7538"
 }, 13, {
     "_ObjectType_": "SP.List", "_ObjectIdentity_": "740c6a0b-85e2-48a0-a494-e0f1759d4aa7:web:eb66d01f-d638-41f8-81d7-1a58dd459268:list:da4af6bf-43e6-463a-a95b-ece4183a7538", "_ObjectVersion_": "0", "Id": "\/Guid(da4af6bf-43e6-463a-a95b-ece4183a7538)\/"
 }
 ]

Request 2:

 <Request AddExpandoFieldTypeSuffix="true" SchemaVersion="14.0.0.0" LibraryVersion="14.0.4104.1000" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">
   <Actions>
     <ObjectPath Id="18" ObjectPathId="17" />
     <ObjectPath Id="20" ObjectPathId="19" />
     <ObjectIdentityQuery Id="21" ObjectPathId="19" />
   </Actions>
   <ObjectPaths>
     <Property Id="17" ParentId="7" Name="Fields" />
     <Method Id="19" ParentId="17" Name="AddFieldAsXml">
       <Parameters>
         <Parameter Type="String">&lt;Field Type="Lookup" List="da4af6bf-43e6-463a-a95b-ece4183a7538" ShowField="Title" DisplayName="Lookup Field" Description="Lookup field created using CSOM." Required="FALSE" Indexed="FALSE" /&gt;</Parameter>
         <Parameter Type="Boolean">true</Parameter>
         <Parameter Type="Enum">0</Parameter>
       </Parameters>
     </Method>
     <Identity Id="7" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:web:eb66d01f-d638-41f8-81d7-1a58dd459268:list:6c4bd5ac-b10c-46f0-9553-be32439987d7" />
   </ObjectPaths></Request>

Response 2:

 [
 {
     "SchemaVersion": "14.0.0.0", "LibraryVersion": "14.0.4104.1000", "ErrorInfo": null
 }, 18, {
     "IsNull": false
 }, 20, {
     "IsNull": false
 }, 21, {
     "_ObjectIdentity_": "740c6a0b-85e2-48a0-a494-e0f1759d4aa7:web:eb66d01f-d638-41f8-81d7-1a58dd459268:list:6c4bd5ac-b10c-46f0-9553-be32439987d7:field:c84fe21c-7666-4129-85f8-1a317da06017"
 }
 ]