0

Why I can't directly create a SP.FieldLookup with REST? Creating a SP.FieldCreationInformation works fine but hasn't all properties I need (e.g. Description or RelationshipDeleteBehavior).

Thx in advance!

// SP.FieldCreationInformation is WORKING

var data = { parameters: {
    __metadata: { 'type': 'SP.FieldCreationInformation' },
    FieldTypeKind: 7,
    Title: 'TestLookup',
    LookupListId: '809f8f9d-5b41-476f-9765-016bbb5ef36a',
    LookupFieldName: 'Title'
}};


$.ajax({
    url: "https://.../_api/lists(guid'd8392641-0422-4b06-97ec-621650c89c9c')/fields/addfield",
    type: "POST",
    data: JSON.stringify(data),
    headers: { 
        "X-RequestDigest" : $("#__REQUESTDIGEST").val(),
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose"
    },
    success: function(d) { alert("added"); },
    error: function(e) { alert("error"); }
});

// SP.FieldLookup is NOT WORKING

var data = {
    __metadata: { 'type': 'SP.FieldLookup' },
    FieldTypeKind: 7,
    Title: 'TestLookup',
    LookupList: '809f8f9d-5b41-476f-9765-016bbb5ef36a',
    LookupField: 'Title'
};

$.ajax({
    url: "https://.../_api/lists(guid'd8392641-0422-4b06-97ec-621650c89c9c')/fields",
    type: "POST",
    data: JSON.stringify(data),
    headers: { 
        "X-RequestDigest" : $("#__REQUESTDIGEST").val(),
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose"
    },
    success: function(d) { alert("added"); },
    error: function(e) { alert("error"); }
});
0

I think, for setting other properties you need to edit call again.. Though I have not tried below but you can check..

MERGE request example: Change a lookup field

$.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' }, 'AllowMultipleValues': true ,'RelationshipDeleteBehavior' : 1}",
  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
});

Gets or sets a value that specifies the delete behavior of the lookup field. Represents a RelationshipDeleteBehaviorType value: None = 0, Cascade = 1, Restrict = 2. See RelationshipDeleteBehavior in the .NET client object model reference for more information about this property.

Reference link-

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.