Show TOC

Extending the Validation MethodsLocate this document in the navigation structure

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.

Prerequisites

You have a GWM Outlook add-in project.

Context

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.

Example

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.

Using the URL of the service, GWDEMO, open the service metadata document in your browser. In the Business Partner entity, there are two mandatory properties:
  • BusinessPartnerKey
  • Address

Do the following changes in the generated code to valildate max length for the BusinessPartnerKey:

Procedure

  1. In the Solution Explorer, expand your add-in project, and then choose the folder,Start of the navigation path <Your Project Name> Next navigation step Outlook Next navigation step Validator End of the navigation path.
    Note By default, the generated code has validation methods for all the mandatory parameters of the selected collection.
  2. Create a new helper method in the class Validator<Your_Template_Name>, for example, ValidatorCRM_App, to validate the max length of the fields as shown below:
    Note The example class name, ValidatorCRM_App is the template name provided when you created the template. The name of the class depends on the name of the GWM template provided during the GWM project creation.
    /// <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();
    }
    
  3. Modify ValidationMessages method in the Validator<Your_Template_Name> class to also handle validation for max length of properties:
    /// <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>();
    }
    
  4. Save your changes.