1

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!

1

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,

https://www.c-sharpcorner.com/article/set-validation-formula-for-lists-in-sharepoint-2016-and-office-365-using-javascr/

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.