Class Creation Example

The following code example illustrates how to create a document subclass and add new properties to it. This example assumes a web reference named CEWS has been set to the following URI: http://localhost:<port>/wsi/FNCEWS40MTOM/wsdl.

using System;      
using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Security.Tokens;
using Microsoft.Web.Services3.Design;
using classcreate_ex.CEWS;

namespace classcreate_ex
{
   /// <summary>
   /// Create a document subclass with new properties.
   /// </summary>
   class Class1
   {
      static void Main(string[] args)
      {
         const string strUrl = "http://localhost:9080/wsi/FNCEWS40MTOM/"; // Change port number if necessary
         const string strObjStoreName = "ContentEngineExs"; // Pre-existing object store
       
         // Create a wse-enabled web service object
         FNCEWS40ServiceWse wseService = new FNCEWS40ServiceWse();
		 
         // Set security token with your username and password
         UsernameToken token = new UsernameToken("username", "password", PasswordOption.SendPlainText);
         wseService.SetClientCredential(token);
         Policy MyPolicy = new Policy();
         MyPolicy.Assertions.Add(new UsernameOverTransportAssertion());
         wseService.SetPolicy(MyPolicy);
         wseService.Url = strUrl;
         wseService.RequireMtom = true;

         // Add default locale info to SOAP header
         Localization objDefaultLocale = new Localization();
         objDefaultLocale.Locale = "en-US";
		 
         // Create Property Templates 
         ObjectValue objPropertyTemplateString = Util.CreatePropertyTemplate(wseService, strObjStoreName, TemplateType.PropertyTemplateString, CardinalityType.Singleton, "CEWS_StringProp1", "en-US");
         ObjectValue objPropertyTemplateIntegerList = Util.CreatePropertyTemplate(wseService, strObjStoreName, TemplateType.PropertyTemplateInteger32, CardinalityType.ListOf, "CEWS_ListofIntegerProp1", "en-US");
         ObjectValue objPropertyTemplateObject = Util.CreatePropertyTemplate(wseService, strObjStoreName, TemplateType.PropertyTemplateObject, CardinalityType.Singleton, "CEWS_ObjectProp1", "en-US");
		 
         // Create a new Document subclass
         ObjectReference objDocumentClassDef = new ObjectReference();
         objDocumentClassDef.classId = "DocumentClassDefinition";
         objDocumentClassDef.objectId = "{01A3A8C2-7AEC-11D1-A31B-0020AF9FBB1C}"; // Document
         objDocumentClassDef.objectStore = strObjStoreName;
         ObjectValue objDocClassDef = Util.CreateClassDef(wseService, strObjStoreName, "CEWS_DocClass1", "en-US", objDocumentClassDef);
         Util.AddPropDefToClassDef(wseService, strObjStoreName, objDocClassDef, objPropertyTemplateString, null);
         Util.AddPropDefToClassDef(wseService, strObjStoreName, objDocClassDef, objPropertyTemplateIntegerList, null);
         string strFolderClassId = "{01A3A8CA-7AEC-11D1-A31B-0020AF9FBB1C}"; // Folder
         Util.AddPropDefToClassDef(wseService, strObjStoreName, objDocClassDef, objPropertyTemplateObject, strFolderClassId);

         Console.WriteLine("Press Enter to end");
         Console.ReadLine();  
      }
   }
     
   /// <summary>
   /// PropertyTemplate type enumeration.
   /// </summary>
   public enum TemplateType
   {
      PropertyTemplateBinary, 
      PropertyTemplateBoolean, 
      PropertyTemplateDateTime, 
      PropertyTemplateFloat64, 
      PropertyTemplateId, 
      PropertyTemplateInteger32, 
      PropertyTemplateString, 
      PropertyTemplateObject
   }
   
   /// <summary>
   /// Property Cardinality enumeration.
   /// </summary>
   public enum CardinalityType
   {
      Singleton = 0,
      Enumeration = 1,
      ListOf = 2
   }
   
   /// <summary>
   /// Utility class for creation of PropertyTemplate and ClassDefinition objects.
   /// </summary>
   public class Util
   {
      public Util()
      {
      }
	  
      /// <summary>
      /// Create a PropertyTemplate object.
      /// </summary>
      /// <param name="wseService">Reference to web service object.</param>
      /// <param name="strObjStoreName">Object store name.</param>
      /// <param name="templateType">Type of property template.</param>
      /// <param name="cardinality">Cardinality of property.</param>
      /// <param name="strDisplayName">Property's display name.</param>
      /// <param name="strLocaleName">Display name's locale name.</param>
      /// <returns>Reference to created PropertyTemplate object.</returns>
      public static ObjectValue CreatePropertyTemplate(FNCEWS40ServiceWse wseService, string strObjStoreName, TemplateType templateType, CardinalityType cardinality, string strDisplayName, string strLocaleName)
      {
         // Allocate property array for PropertyTemplate object
         int intPropertyCount = (templateType == TemplateType.PropertyTemplateObject) ? 4 : 5;
         ModifiablePropertyType[] objActionProperties = new ModifiablePropertyType[intPropertyCount];
   
         // DisplayNames dependent object
         DependentObjectType[] objPTDisplayNames = new DependentObjectType[1]; 
         ModifiablePropertyType[] prpLocalizedStringProps = new ModifiablePropertyType[2];
		 
         // LocalizedText property
         SingletonString prpLocalizedText = new SingletonString();
         prpLocalizedText.propertyId = "LocalizedText";
         prpLocalizedText.Value = strDisplayName;
         prpLocalizedStringProps[0] = prpLocalizedText;
   
         // LocaleName property
         SingletonString prpLocaleName = new SingletonString();
         prpLocaleName.propertyId = "LocaleName";
         prpLocaleName.Value = strLocaleName;
         prpLocalizedStringProps[1] = prpLocaleName;
   
         // LocalizedString dependent object 
         DependentObjectType objLocalizedString = new DependentObjectType();
         objLocalizedString.classId = "LocalizedString";
         objLocalizedString.objectStore = strObjStoreName;
         objLocalizedString.Property = prpLocalizedStringProps; 
         objLocalizedString.dependentAction = DependentObjectTypeDependentAction.Insert;
         objLocalizedString.dependentActionSpecified = true;
         objPTDisplayNames[0] = objLocalizedString;
         
         // DisplayNames property
         ListOfObject prpDisplayNames = new ListOfObject();
         prpDisplayNames.propertyId = "DisplayNames";
         prpDisplayNames.Value = objPTDisplayNames;
         objActionProperties[0] = prpDisplayNames;
         
         // Cardinality property
         SingletonInteger32 prpCardinality = new SingletonInteger32();
         prpCardinality.propertyId = "Cardinality";
         prpCardinality.Value = (int)cardinality;
         prpCardinality.ValueSpecified = true;
         objActionProperties[1] = prpCardinality;
   
         // IsValueRequired property
         SingletonBoolean prpIsHidden = new SingletonBoolean();
         prpIsHidden.propertyId = "IsHidden";
         prpIsHidden.Value = false;
         prpIsHidden.ValueSpecified = true;
         objActionProperties[2] = prpIsHidden;
		 
         // IsValueRequired property
         SingletonBoolean prpIsValueRequired = new SingletonBoolean();
         prpIsValueRequired.propertyId = "IsValueRequired";
         prpIsValueRequired.Value = false;
         prpIsValueRequired.ValueSpecified = true;
         objActionProperties[3] = prpIsValueRequired;
		 
         // IsPersistent property: read-only on Object properties.
         if (templateType != TemplateType.PropertyTemplateObject)
         {
            SingletonBoolean prpIsPersistent = new SingletonBoolean();
            prpIsPersistent.propertyId = "IsPersistent";
            prpIsPersistent.Value = true;
            prpIsPersistent.ValueSpecified = true;
            objActionProperties[4] = prpIsPersistent;
         }
   
         // Allocate a CreateAction object
         CreateAction objCreate = new CreateAction();
         objCreate.classId = templateType.ToString();
		 
         // Construct ChangeRequestType item
         ChangeRequestType objChangeRequestType = new ChangeRequestType();
         objChangeRequestType.Action = new ActionType[1]{objCreate};
         objChangeRequestType.ActionProperties = objActionProperties;
         objChangeRequestType.TargetSpecification = new ObjectReference();
         objChangeRequestType.TargetSpecification.classId = "ObjectStore";
         objChangeRequestType.TargetSpecification.objectStore = strObjStoreName;
		 
         // Construct ExecuteChangesRequest
         ExecuteChangesRequest objExecuteChangesRequest = new ExecuteChangesRequest();
         objExecuteChangesRequest.ChangeRequest = new ChangeRequestType[1]{objChangeRequestType};
         objExecuteChangesRequest.refresh = true;
         objExecuteChangesRequest.refreshSpecified = true;
   
         // Issue ExecuteChanges
         ChangeResponseType[] objChangeResponseTypeArray = wseService.ExecuteChanges(objExecuteChangesRequest);
         Console.WriteLine("Created PropertyTemplate '{0}'", strDisplayName);
   
         return objChangeResponseTypeArray[0];
      } // CreatePropertyTemplate
   
      /// <summary>
      /// Create a ClassDefinition object, subclassing another object.
      /// </summary>
      /// <param name="wseService">Reference to web service object.</param>
      /// <param name="strObjStoreName">Object store name.</param>
      /// <param name="strDisplayName">Display name of class definition to create.</param>
      /// <param name="strLocaleName">Locale name for display name.</param>
      /// <param name="objSuperclassDef">Object reference to class definition of object to subclass.</param>
      /// <returns>Reference to created ClassDefinition object.</returns>
      public static ObjectValue CreateClassDef(FNCEWS40ServiceWse wseService, string strObjStoreName, string strDisplayName, string strLocaleName, ObjectReference objSuperclassDef)
      {
         // Allocate property array for ClassDefinition object
         ModifiablePropertyType[] objActionProperties = new ModifiablePropertyType[1];
   
         // DisplayNames dependent object
         DependentObjectType[] objDisplayNames = new DependentObjectType[1];
   
         // Properties Collection for the LocalizedString object
         ModifiablePropertyType[] objProperties = new ModifiablePropertyType[2];
		 
         // LocalizedText property
         SingletonString prpLocalizedText = new SingletonString();
         prpLocalizedText.propertyId = "LocalizedText";
         prpLocalizedText.Value = strDisplayName;
         objProperties[0] = prpLocalizedText;
   
         // LocaleName property
         SingletonString prpLocaleName = new SingletonString();
         prpLocaleName.propertyId = "LocaleName";
         prpLocaleName.Value = strLocaleName;
         objProperties[1] = prpLocaleName;
   
         // LocalizedString dependent object 
         DependentObjectType objLocalizedString = new DependentObjectType();
         objLocalizedString.classId = "LocalizedString";
         objLocalizedString.objectStore = strObjStoreName;
         objLocalizedString.Property = objProperties; 
         objLocalizedString.dependentAction = DependentObjectTypeDependentAction.Insert;
         objLocalizedString.dependentActionSpecified = true;
         objDisplayNames[0] = objLocalizedString;
         
         // DisplayNames property
         ListOfObject prpDisplayNames = new ListOfObject();
         prpDisplayNames.propertyId = "DisplayNames";
         prpDisplayNames.Value = objDisplayNames;
         objActionProperties[0] = prpDisplayNames;
   
         // Allocate a CreateAction object
         CreateAction objCreate = new CreateAction();
         objCreate.classId = objSuperclassDef.classId;
   
         // Construct ChangeRequestType item
         ChangeRequestType objChangeRequestType = new ChangeRequestType();
         objChangeRequestType.Action = new ActionType[1]{objCreate};
         objChangeRequestType.ActionProperties = objActionProperties;
         objChangeRequestType.TargetSpecification = objSuperclassDef;
   
         // Construct ExecuteChangesRequest
         ExecuteChangesRequest executeChangesRequestItem = new ExecuteChangesRequest();
         executeChangesRequestItem.ChangeRequest = new ChangeRequestType[1]{objChangeRequestType};
         executeChangesRequestItem.refresh = true;
         executeChangesRequestItem.refreshSpecified = true;
		 
         // Issue ExecuteChanges
         ChangeResponseType[] response = wseService.ExecuteChanges(executeChangesRequestItem);
         Console.WriteLine("Created ClassDefinition '{0}'", strDisplayName);
   
         return response[0];
      } // CreateClassDef
   
      /// <summary>
      /// Add a Property to a Class.
      /// </summary>
      /// <param name="wseService">Reference to web service object.</param>
      /// <param name="strObjStoreName">Object store name.</param>
      /// <param name="objClassDefinition">Reference to ClassDefinition object.</param>
      /// <param name="objPropertyTemplate">Reference to PropertyTemplate object.</param>
      /// <param name="objectRequiredId">Value of RequiredClassId property if object property or null for all others.</param>
      /// <returns>Refreshed ClassDefinition object.</returns>
      public static ObjectValue AddPropDefToClassDef(FNCEWS40ServiceWse wseService, string strObjStoreName, WithObjectIdentityType objClassDefinition, WithObjectIdentityType objPropertyTemplate, string objectRequiredId)
      { 
         DependentObjectType objPropertyDefinition = new DependentObjectType(); 
         objPropertyDefinition.classId = "PropertyDefinition";
         objPropertyDefinition.dependentAction = DependentObjectTypeDependentAction.Insert;
         objPropertyDefinition.dependentActionSpecified = true;
		 
         SingletonObject objNewPropertyTemplate = new SingletonObject();
         objNewPropertyTemplate.propertyId = "PropertyTemplate";
   
         ObjectReference objRef = new ObjectReference();
         objRef.classId = objPropertyTemplate.classId;
         objRef.objectId = objPropertyTemplate.objectId;
         objRef.objectStore = objPropertyTemplate.objectStore;
         objNewPropertyTemplate.Value = objRef;
   
         // Set property metadata
         if (objPropertyTemplate.classId.Equals("PropertyTemplateObject"))
         {
            objPropertyDefinition.Property = new PropertyType[3];
            objPropertyDefinition.Property[0] = objNewPropertyTemplate;
   
            SingletonId prpRequiredClassId = new SingletonId();
            prpRequiredClassId.propertyId = "RequiredClassId";
            prpRequiredClassId.Value = objectRequiredId;
            objPropertyDefinition.Property[1] = prpRequiredClassId;
         }
         else
         {
            objPropertyDefinition.Property = new PropertyType[2];
            objPropertyDefinition.Property[0] = objNewPropertyTemplate;
         }
		 
         // Set PropertyDefinitions collection
         DependentObjectType[] objPropertyDefinitions = new DependentObjectType[1]; 
         objPropertyDefinitions[0] = objPropertyDefinition; 
   
         ListOfObject prpPropertyDefinitions = new ListOfObject();
         prpPropertyDefinitions.propertyId = "PropertyDefinitions";
         prpPropertyDefinitions.Value = objPropertyDefinitions;
		 
         ModifiablePropertyType[] objActionProperties = new ModifiablePropertyType[1];
         objActionProperties[0] = prpPropertyDefinitions;
   
         UpdateAction objUpdate = new UpdateAction();
         ChangeRequestType objChangeRequestType = new ChangeRequestType();
         objChangeRequestType.Action = new ActionType[1]{objUpdate};
         objChangeRequestType.ActionProperties = objActionProperties;
   
         ObjectReference objectReferenceItem = new ObjectReference();
         objectReferenceItem.classId = objClassDefinition.classId;
         objectReferenceItem.objectId = objClassDefinition.objectId;
         objectReferenceItem.objectStore = objClassDefinition.objectStore;
         objChangeRequestType.TargetSpecification = objectReferenceItem;
   
         ExecuteChangesRequest objExecuteChangesRequest = new ExecuteChangesRequest();
   
         objExecuteChangesRequest.ChangeRequest = new ChangeRequestType[1]{objChangeRequestType};
         objExecuteChangesRequest.refresh = true;
         objExecuteChangesRequest.refreshSpecified = true;
   
         ChangeResponseType[] objChangeResponseTypeArray = wseService.ExecuteChanges(objExecuteChangesRequest);
         return objChangeResponseTypeArray[0];
      } // AddPropDefToClassDef
   } // Util
}