Automatically set all available options from a lookup field
AnsweredHi there
I asked skybow recently to provide a function code, that automatically selects all available choices of a choice field.
Christof came up with a wonderful piece of code that does exactly what we needed! THANK YOU FOR THAT. (if anyone is interested, please find the code and instructions below)
However, we now facing a similar requirement but with a lookup field instead!
Is it possible to have a lookup column automatically filled with all available values of a sublist?!
Szenario:
In a sublist, there are configuration items "A", "B" and "C".
In the mainlist, there is a lookup column to get the values from the Sublist.
The user now has to select "A", "B" and "C" manually.
Our goal is, that there is a function code on that column, that automatically populates the column with "A", "B" and "C".
Maybe a little adaption of the provided function code for choice fields could do the trick?!
Best wishes and thanks a lot for any hint on this one
Dieter
- - - - - - Function Code to automatically select all available options of a choice field - - - - - -
This script will return all values from a Choice field in an array which can be used to set it on the form.
Just replace the listName and columnName:
var listName = "CaseFiles";
var columnName = "AChoiceField";
return new Promise((resolve, reject) => {
var context = new SP.ClientContext.getcurrent(); var web = context.getweb();
this.list = web.getlists().getByTitle(listName); this.fields = this.list.getfields();
var choiceField = context.castTo(this.list.get_fields().getByInternalNameOrTitle(columnName), SP.FieldChoice);
context.load(this.list);
context.load(this.fields);
context.load(choiceField);
context.executeQueryAsync(
(sender, args) => {
var choices = choiceField.get_choices();
resolve(choices);
},
(sender, args) => {
console.log("Error occured on selecting all values on " + columnName + ": " + args.get_message());
reject("Error occured on selecting all values on " + columnName + ": " + args.get_message());
}
);
});
-
Official comment
Dear DieterJauslin,
this is much easier for a multi lookup field. You can simply use our initial value expression on the newform and you have to set it as an array of item IDs.
To get them you can use our QueryList function as Assignment Expression as follows:
[[@Web.QueryList([[@Web.ServerRelativeUrl]]+'/Lists/yourSubList', 'CAMLQueryToGetTheItemsFromTheSublist', 0, 'ID', 'Scope=Recursive')]].map(x => x.ID)Kind regardsskybow Support -
AMAZING! ;)
Works as expected!
Thank you so much!
Best wishesDieter
0
Please sign in to leave a comment.
Comments
2 comments