We are trying to publish items programmatically, hence we would like to run all the validation rules applicable as per the item template to check, if the sitecore item has no validation errors. Is there anything available out of the sitecore to check this for an item ? We are trying to achive what sitecore does here in content editor, programatically. https://doc.sitecore.com/xp/en/users/102/sitecore-experience-platform/validate-an-item.html
1 Answer
Found an old answer on SO that still seems to be valid:
var validatorItem = Sitecore.Data.Database.GetDatabase("master").GetItem("/sitecore/system/Settings/Validation Rules/Item Rules/Item/Duplicate Name");
var validator = ValidatorManager.BuildValidator(validatorItem, Sitecore.Context.Item);
validator.Validate(new ValidatorOptions(false));
if(!validator.IsValid)
{
Response.Write("Error level: " + validator.Result.ToString() + "<br />");
Response.Write("Error Message: " + validator.Name + "<br />");
}
else
{
Response.Write("All ok !");
}
Reference here: https://stackoverflow.com/questions/8261765/how-to-validate-an-item-in-code