Quick Start (MTOM): Creating a policy using the web service API

The following C# code example creates a new policy named Allow Copy. The policy set to which the policy is added is named Global Policy Set. This code example exposes a user-defined method named getFileContent that accepts the location of a PDRL XML file and returns a string representation of the XML data located within the file. An example of the PDRL document (SamplePolicy.xml) that this quick start references is shown in the Creating Policies section. (See Creating Policies.)

/** 
    * Ensure that you create a .NET project that uses  
    * MS Visual Studio 2008 and version 3.5 of the .NET 
    * framework. This is required to invoke a  
    * LiveCycle service using MTOM. 
    *  
    * For information, see "Invoking LiveCycle using MTOM" in Programming with LiveCycle.   
    */ 
 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 
using System.IO; 
using CreatePolicy.ServiceReference1; 
 
namespace CreatePolicy 
{ 
       class Program 
       { 
           static void Main(string[] args) 
           { 
               try 
               { 
               //Create a RightsManagementServiceClient object 
               RightsManagementServiceClient rmClient = new RightsManagementServiceClient(); 
               rmClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://hiro-xp:8080/soap/services/RightsManagementService?blob=mtom"); 
 
               //Enable BASIC HTTP authentication 
               BasicHttpBinding b = (BasicHttpBinding)rmClient.Endpoint.Binding; 
               b.MessageEncoding = WSMessageEncoding.Mtom; 
               rmClient.ClientCredentials.UserName.UserName = "administrator"; 
               rmClient.ClientCredentials.UserName.Password = "password"; 
               b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
               b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
     
               //Create a PolicySpec object 
               PolicySpec policy = new PolicySpec(); 
         
            //Set the policys attributes 
            policy.name = "Allow Copy"; 
            policy.description = "This is a test policy"; 
            policy.policySetName="Global Policy Set";  
            policy.offlineLeasePeriod = 30; 
 
            //Call the getFileContent method to get a string value 
            //that represents SamplePolicy.xml 
            policy.policyXml = getFileContent("C:\\SamplePolicy.xml"); 
 
            //Register the policy 
            rmClient.registerPolicy(policy, "Global Policy Set"); 
              } 
 
               catch (Exception ee) 
               { 
                 Console.WriteLine(ee.Message); 
               } 
           } 
 
           static private String getFileContent(String fileName) 
        { 
            String xmlStr = null; 
            if (fileName.Length == 0) 
                throw new FileNotFoundException(); 
 
            try 
            { 
                // Create an instance of StreamReader to read from a file. 
                // The using statement also closes the StreamReader. 
                using (StreamReader sr = new StreamReader(fileName)) 
                { 
                    String line; 
                    // Read and display lines from the file until the end of 
                    // the file is reached. 
                    while ((line = sr.ReadLine()) != null) 
                    { 
                        xmlStr += line; 
                    } 
                } 
            } 
            catch 
            { 
                throw; 
            } 
            return xmlStr; 
        } 
    } 
 
      }