Compartilhar via


Creating policies

To add policy metadata for a new service, complete the following steps:

  1. Add a method to create policies.

    Add a method that will add policy metadata for your methods.

    private void LoadPolicies()
    

{

}

  1. Create policies.

    In the LoadPolicies method, create a policy object for each of your service operations. To create a policy, supply the policy constructor with the following parameters.

    Parameter

    Description

    policyId

    A GUID that uniquely identifies the policy.

    resXAssemblyName

    A string that specifies the name of the resource assembly that contains the text resources for this policy.

    nameResXId

    A string that specifies the ID of the text resource that provides the name of the policy.

    rootBusinessObjectName
    ResXId

    A string that specifies the ID of the text resource that provides the name of the document type.

    The following code example creates the policy objects for the create, update, and delete operations of the sample Leads service. Notice how the first parameter uses the GUID value that was created earlier to identify the policy.

    Policy createLeadPolicy =
    new Policy(createLeadPolicyId,
    "Sample.PolicyResources",
    "CreateLeadPolicyName",
    "LeadRootObjectName");
    

Policy updateLeadPolicy = new Policy(updateLeadPolicyId, "Sample.PolicyResources", "UpdateLeadPolicyName", "LeadRootObjectName"); Policy deleteLeadPolicy = new Policy(deleteLeadPolicyId, "Sample.PolicyResources", "DeleteLeadPolicyName", "LeadRootObjectName");

  1. Add behaviors to the policy.

    For policies with behaviors, add behavior objects to the policy. Use the behavior key to retrieve the specified behavior object from the collection you created to store behaviors.

    The following code example shows how to use the behaviors dictionary collection object to retrieve a behavior from the behavior collection and add it to the createLeadPolicy object.

    createLeadPolicy.Behaviors.Add(behaviors[createQualifiedLeadBehaviorId]);
    
  1. Add policies to the Dynamics GP Service metadata.

    Use the ServiceFactory to instantiate a PolicyBusinessService object.

    PolicyBusinessService policyBusinessService =
    (PolicyBusinessService)ServiceFactory.GetServiceInstance(
        CommonConstants.PolicyBusinessService);
    
Use the PolicyBusinessService object to add your policy metadata to the Dynamics GP Service metadata.

The following code example create policies for the sample Leads service. Notice how the CreatePolicy method requires you to supply a context and policy object.

<pre class="checklistscript" IsFakePre="true" xmlns="http://www.w3.org/1999/xhtml">policyBusinessService.CreatePolicy(context, createLeadPolicy);

policyBusinessService.CreatePolicy(context, updateLeadPolicy); policyBusinessService.CreatePolicy(context, deleteLeadPolicy);