I want to set the Validation Settings of a List with PnPJS. Is this possible?
alternatively: Is it possible to provision a List with specific Validation Settings in place?
Thanks!
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityI want to set the Validation Settings of a List with PnPJS. Is this possible?
alternatively: Is it possible to provision a List with specific Validation Settings in place?
Thanks!
You can use the below code to set validation for a list field using JSOM,
$(document)
.ready(function()
{
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', SetValidation);
});
varoList, clientContext;
function SetValidation()
{
//Get client context ,web and list object
clientContext = new SP.ClientContext.get_current();
varoWeb = clientContext.get_web();
oList = oWeb.get_lists()
.getByTitle('DemoList');
//Load the client context and execute the batch
clientContext.load(oList);
clientContext.executeQueryAsync(Success, Failure);
}
function Success()
{
//Set validation formula
oList.set_validationFormula("[StartDate] < TODAY()");
oList.set_validationMessage('Validation Failed ! Start Date should be less than Today.');
oList.update();
//Load the client context and execute the batch
clientContext.load(oList);
clientContext.executeQueryAsync(OnSuccess, OnFailure);
}
function Failure(sender, args)
{
console.log('Request failed. ' + args.get_message() + '\n'
args.get_stackTrace());
}
function OnSuccess()
{
console.log("List validation formula has been set for this list");
}
function OnFailure(sender, args)
{
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
} <
/script>
Using the above code you will have an idea on setting the validation rule, now refer the below link to do the same using pnp js.
https://www.c-sharpcorner.com/article/listlibrary-fieldcolumn-manipulation-using-pnp-js-core/
To know more about the jsom implementation refer the below link,