3

I made the target audiences column in a list to required. but when I add a new list item without specifying the audience , it still gets created. When I edit the item, the target audiences field has a GUID in place generated automatically. Why is this happening? Am I missing something?

Thanks!

1 Answer 1

3

You are correct, unfortunately, SharePoint by default fills this field with the text "No targeting" when it's left empty which causes it to pass the required validation part.

enter image description here

If you want to make sure that Audience field is set as required, you need to write custom code in the newform/editform in the PreSaveAction method. Just edit the page and add a script editor/content editor webpart on it.

Code would be something like below:

function PreSaveAction(){

    var AudienceField = $('.ms-inputuserfield[title=Audience Editor]');
            if(AudienceField.html() === ''){
                $(AudienceField).append('<p class="ErrorMSG" style="color:red;">You must specify a value for the above required field</p>')
    return false;

            } else {
                return true;
            }
}

You can also take a look at JSlink to make sure its required. Code would be like below:

mjh.MyCustomFieldValidator = function () {
  mjh.MyCustomFieldValidator.prototype.Validate = function (value) {
    var isError = false;
    var errorMessage = "";

    if (value == "No Tageting") {
      isError = true;
      errorMessage = "Target Audience cant be empty";
    }
    return new SPClientForms.ClientValidation.ValidationResult(isError, errorMessage);
  };
};




var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx);

// register the field validators
var fieldValidators = new SPClientForms.ClientValidation.ValidatorSet();

if (formCtx.fieldSchema.Required) {
  fieldValidators.RegisterValidator(
    new SPClientForms.ClientValidation.RequiredValidator()
  );
} fieldValidators.RegisterValidator(new mjh.MyCustomFieldValidator());

formCtx.registerValidationErrorCallback(formCtx.fieldName, mjh.onError);
formCtx.registerClientValidator(formCtx.fieldName, fieldValidators);

Reference - Applying JSLink to list fields

Introduction to Client forms validation

Target audience not required

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.