This section explains how to add custom validation methods to verify the parameters of an OData service, such as, checking for all required properties in a collection, or if a property has the required data type.
You can add a custom validation method to verify various parameters for the properties of the service that you call in your GWM Outlook add-in application. Validation enforces the required actions that have been applied to the properties of a service.
To add a custom validation method, open the class file generated for your add-in (this has the template name that you defined at the time of creating the template), as it has a validation method, and add a helper method to it.
We use an example based on the OData service, GWDEMO.
For example, you can add a validation method to check the attribute, max length for the property,BusinessPartnerKey as required by the OData service. The max length specifies the maximum number of charaters that the user is allowed to provide for the value required by property, BusinessPartnerKey.
Do the following changes in the generated code to valildate max length for the BusinessPartnerKey:
/// <summary> /// This method validates the properties against its allowed max length /// </summary> /// <param name="entity">BusinessPartner entity</param> /// <returns>Validation message </returns> private string ValidateMaxLength(GWDEMO.BusinessPartner entity) { StringBuilder validationMessage = new StringBuilder(); if (!string.IsNullOrEmpty(entity.BusinessPartnerKey) && entity.BusinessPartnerKey.Length > 32) { validationMessage.Append("Maximum length allowed for BusinessPartnerKey is 32"); } return validationMessage.ToString(); }
/// <summary> /// this method will create the list of the validation messages displayed to the user /// </summary> /// <param name="entity">BusinessPartner entity</param> /// <returns>Validation message</returns> public IEnumerable<string> ValidationMessages(GWDEMO.BusinessPartner entity ) { List<string> validationWarnings = new List<string>(); string validationMessage = ValidateMandatoryProperty(entity); if (!string.IsNullOrEmpty(validationMessage)) { validationWarnings.Add("Following Mandatory Parameters cannot be null : " + validationMessage); } validationMessage = ValidateMaxLength(entity); if (!string.IsNullOrEmpty(validationMessage)) { validationWarnings.Add(validationMessage); } return validationWarnings.AsEnumerable<string>(); }