0

I'm trying to create a couple of lists automatically using REST.

List A:
TextField: ID
TextField: Name

List B:
LookupField: ID (Get from list A)
LookupField: ID:Name (Additional read-only column)

Using the UI I can simply go to the settings of the lookup field ID in list B and tick the checkbox "Name" from the list "Add a column to show each of these additional fields:" However, I struggle to do the same using REST. Does anyone know if this is possible?

I have tried to create additional lookup fields using SP.Field, but I cannot find any way to set the PrimaryFieldID.

The following produces the error: 400 Bad Request. Even if I change the type from SP.Field to SP.FieldLookup.

$.ajax({ url: "http://<site url>/_api/web/lists(guid'da58632f-faf0-4a78-8219-99c307747741') /fields('1d22ea11-1e32-424e-89ab-9fedbadb6ce1')", type: "POST", data: "{ '__metadata': { 'type': 'SP.FieldLookup' }, 'PrimaryField': <Guid of list B field ID> }", headers: { "X-RequestDigest": <form digest value>, "content-type": "application/json;odata=verbose", "content-length": <length of body data>, "X-HTTP-Method": "MERGE" }, success: successHandler, error: errorHandler });

Thanks!

0

Figured it out!

It worked using the /createfieldasxml endpoint.

$.ajax({
  url: "http://<site url>/_api/web
  /lists(guid'da58632f-faf0-4a78-8219-99c307747741')
  /fields('1d22ea11-1e32-424e-89ab-9fedbadb6ce1')
  /createfieldasxml",
  type: "POST",
  data: "{ '__metadata': { 'type': 'SP.XmlSchemaFieldCreationInformation' }, 'SchemaXml': "<Field Type='Lookup' Title='ID:Name' DisplayName='ID:Name' List='<GUID of list A>' ShowField='Name' FieldRef='<GUID of field ID in list B>' SourceID='<GUID of list B>' ReadOnly='TRUE' UnlimitedLengthInDocumentLibrary='FALSE' />",
  headers: { 
    "X-RequestDigest": <form digest value>,
    "content-type": "application/json;odata=verbose",
    "content-length": <length of body data>,
  },
  success: successHandler,
  error: errorHandler
});

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.