How can i find the Lookup Column id when i pass the data to REST API?
-
17-02-2021 - |
Domanda
As shown in some examples I must use lookup column ID when i pass data to Rest API. But I cannot find what the lookup column's id. So How can i add this id to data
in rest api?
JSON.stringify({ '__metadata': { 'type': 'SP.Data.FeaturesListItem' },
'SubsystemId':"It will be integer"})
Soluzione
Ok, based on your answers to some of my questions in the comments, here is how you find the correct integer ID to use when trying to set a lookup column using the REST API.
You said that the lookup column you are trying to set looks at the list UlkeList
. In order to set the lookup column, you need to know the list item ID of the specific item you want to link to in the UlkeList
list. You also say that the values that are shown in the DocumentMeta
list in the lookup column come from the UlkeKod
column in the UlkeList
list.
I am assuming that when users are choosing what value to set, they are choosing from possible values from the UlkeKod
column. (I am also assuming that these are text values, or something that is readable and meaningful to humans, and not more integers.) So in order to get the list item ID of the item in the UlkeList
list that has the UlkeKod
value selected by the user, you would need to send a REST query like this:
/_api/web/lists/getbytitle('UlkeList')/items?$filter=UlkeKod eq 'Selected UlkeKod value'
Presumably you will only get one result for this query, but because of the way the query is constructed you will still get that single result in a results
array. So the data returned from that query will look something like this:
d: {
results: [
{
"Id": 7,
"ID": 7,
"Title": "Whatever the title of that item is",
"UlkeKod": "Selected UlkeKod value",
// other columns, such as Created, Modified, etc.
}
]
}
This data shows us that (in this pretend example) the item in the UlkeList
list that has the UlkeKod
column value of "Selected UlkeKod value" is the list item with ID: 7
.
That is the integer that you need to use to set the value of the GecerliUlke
lookup column in the DocumentMetaList
list.
So then you would end up doing something like
var ulkeListItemId = ulkeListResponse.d.results[0].ID
var data = JSON.stringify({
'__metadata': { 'type': 'SP.Data.DocumentMetaListItem' },
'GecerliUlkeId': ulkeListItemId
});
// use that data in your POST to the DocumentMeta list to set the lookup column
Now, in your comments you mention "it can take multiple variables". If what you really mean by that is that the GecerliUlke
lookup column allows multiple values, then, as is shown in the example in the other question you linked to, you would need to make the GecerliUlkeId
property in your data
an object with a results
property that is an array of all of the list item IDs of the items in the UlkeList
that you want to set as the lookup values:
var data = JSON.stringify({
'__metadata': { 'type': 'SP.Data.DocumentMetaListItem' },
'GecerliUlkeId': {
'results': [
firstUlkeListItemID,
secondUlkeListItemID,
thirdUlkeListItemID
]
}
});
If I have made a mistake in that users are not selecting values from the UlkeKod
column when they are making their selections for what to set in the GecerliUlke
column, the same general process should apply:
- Users select the value(s) that somehow indicate what they want to set in the
GecerliUlke
column. - You use those selected value(s) to query the
UlkeList
list to get back the matching list item(s). - You use the item
ID
(s) in the response from theUlkelist
to set theGecerliUlke
column in theDocumentMeta
list.