Creating behaviors
Add a method that creates the behavior, behavior option, and behavior parameter metadata for your policies. The following is the behavior information that the Dynamics Security Service console displays for the Create Lead Policy of the sample Leads service.
To add a behavior to a policy, complete the following steps:
Define behavior options.
Create an enumeration that specifies a value for each behavior option that the behavior supports. The following code example shows the behavior options enumeration for the CreateLead policy. Notice how the enumeration specifies the value of each data member.
private enum QualifiedLeadBehaviorOption : int
{ Qualified = 1, NotQualified = 2 }
Add a method to create behaviors.
Add a method to create behavior metadata and add it to the Dynamics GP Service.
private void LoadBehaviors()
{
}
Create behaviors.
In the LoadBehavior method, add the behaviors, behavior options, and behavior parameters for your policies. To begin, create a behavior object for each behavior. To create a behavior, supply the behaviors constructor with the following parameters.
Parameter
Description
behaviorId
A GUID that uniquely identifies the behavior. This is the GUID that you created earlier.
resXAssemblyName
A string that specifies the name of the resource assembly that contains the text resources for this behavior.
descriptionResXId
A string that specifies the ID of the text resource that provides the description of the behavior.
nameResXId
A string that specifies the ID of the text resource that provides the name of the behavior.
For more information about how to create a resource assembly that contains the name and description information that display in the Dynamics Security Console for a behavior, see Adding a policy resource assembly. The resource assembly also provides name and description information for the behavior options, behavior parameters, and policies.
The following code example creates a behavior object for the Create Lead Policy. Notice how the first parameter uses the GUID that was created earlier to uniquely identify this behavior.
Behavior createQualifiedLeadBehavior = new Behavior(createQualifiedLeadBehaviorId, "Sample.PolicyResources", "CreateQualifiedLeadBehaviorDescription", "CreateQualifiedLeadBehaviorName");
Create behavior options.
Create behavior options for each behavior object. To create a behavior option, supply the behavior option constructor with the following parameters.
Parameter
Description
behaviorId
A GUID that identifies the behavior that this option is part of.
behaviorOptionId
An integer that identifies this option for the behavior.
resXAssemblyName
A string that specifies the name of the resource assembly that contains the text resources for this behavior option.
descriptionResXId
A string that specifies the IDd of the text resource that provides the description for this behavior option.
nameResXId
A string that specifies the ID of the text resource that provides the name for this behavior option.
The following code example creates two behavior options. Notice how the second parameter uses the enumeration that was created earlier to uniquely identify each option for the behavior.
BehaviorOption notQualified = new BehaviorOption(createQualifiedLeadBehaviorId, (short)QualifiedLeadBehaviorOption.NotQualified, "Sample.PolicyResources", "NotQualifiedLeadOptionDescription", "NotQualifiedLeadOptionName");
BehaviorOption qualified = new BehaviorOption(createQualifiedLeadBehaviorId, (short)QualifiedLeadBehaviorOption.Qualified, "Sample.PolicyResources", "QualifiedLeadOptionDescription", "QualifiedLeadOptionName");
Create behavior parameters.
If a behavior option requires input from the user, create a behavior parameter to store what the user enters.
To create a behavior parameter, supply the behavior parameter constructor with the following parameters.
Parameter
Description
behaviorId
A GUID that identifies the behavior that the behavior parameter is part of.
behaviorOptionId
An integer that specifies the behavior option that requires the behavior parameter.
parameterId
An integer that identifies this parameter for the specified behavior option.
resXAssemblyName
A string that specifies the name of the resource assembly that contains the text resources for this behavior parameter.
descriptionResXId
A string that specifies the ID of the text resource that provides the description of the behavior parameter.
nameResXId
A string that specifies the ID of the text resource that provides the name of the behavior parameter.
The following code example creates a parameter for the Qualified behavior option. Notice how the second parameter specifies the behavior option and the third parameter specifies the ID of the behavior parameter. Also, notice how the value of the parameter is set to default to an empty string.
Hint: The Value property of a behavior property can only accept values that are a string data type.
Parameter qualifiedParam = new Parameter(createQualifiedLeadBehaviorId, (short)QualifiedLeadBehaviorOption.Qualified, (short)QualifiedLeadParameterId, "Sample.PolicyResources", "QualifiedLeadParameterDescription", "QualifiedLeadParameterName");
qualifiedParam.Value = string.Empty;
Add the parameters and options to the behaviors.
To begin, add the parameter to the Parameters collection of the behavior option.
qualified.Parameters.Add(qualifiedParam);
Then add the behavior options to the Options collection of the behavior.
<pre class="checklistscript" IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">createQualifiedLeadBehavior.Options.Add(notQualified);
createQualifiedLeadBehavior.Options.Add(qualified);
Configure the behaviors.
Set the default values of the behaviors and specify whether the behavior is 'internal' or 'external'.
createQualifiedLeadBehavior.SelectedOption = createQualifiedLeadBehavior.GetBehaviorOption( (short)QualifiedLeadBehaviorOption.NotQualified);
createQualifiedLeadBehavior.Internal = true;
Add behavior metadata to the Dynamics GP Service.
Use the ServiceFactory object to instantiate a PolicyBusinessService object.
PolicyBusinessService policyBusinessService = (PolicyBusinessService)ServiceFactory.GetServiceInstance( CommonConstants.PolicyBusinessService);
Use the PolicyBusinessService object to add the behavior to the Dynamics GP Service metadata.
<pre class="checklistscript" IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">policyBusinessService.CreateBehavior(context,
createQualifiedLeadBehavior);
Add behaviors to a collection.
Add your behaviors to the dictionary collection that you created earlier. You can use the dictionary collection object to retrieve individual behaviors using the behavior Id. You will use the dictionary collection to add behaviors to a policy.
The following code example adds the behavior to the behaviors collection. Notice how dictionary collection object uses the behavior ID to identify the behavior object that is being added to the collection.
behaviors.Add(createQualifiedLeadBehaviorId, createQualifiedLeadBehavior);