質問

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!

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません sharepoint.stackexchange
scroll top