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_DIME has been set to the following URI: http://localhost:<port>/wsi/FNCEWS35DIME/wsdl.

using System;
using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Security.Tokens;
using classcreate_ex.CEWS_DIME;

namespace classcreate_ex
{
   /// <summary>
   /// Create a document subclass with new properties.
   /// </summary>
   class Class1
   {
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
         const string url = "http://localhost:9080/wsi/FNCEWS35DIME/"; // Change port number if necessary
         const string objectStore = "ContentEngineExs"; // Pre-existing object store
   
         // Create a wse-enabled web service object to provide access to SOAP header
         FNCEWS35ServiceWse wseService = new FNCEWS35ServiceWse();
         wseService.Url = url;
         SoapContext soapContext = wseService.RequestSoapContext;
		 
         // Add security token to SOAP header with your username and password
         UsernameToken token = new UsernameToken("username", "password", PasswordOption.SendPlainText);
         soapContext.Security.Tokens.Add(token);
		 
         // Add default locale info to SOAP header
         Localization defaultLocale = new Localization();
         defaultLocale.Locale = "en-US";
		 
         // Create Property Templates 
         ObjectValue objPropertyTemplateString = Util.CreatePropertyTemplate(wseService, objectStore, TemplateType.PropertyTemplateString, CardinalityType.Singleton, "CEWS_StringProp1", "en-US");
         ObjectValue objPropertyTemplateListofInteger = Util.CreatePropertyTemplate(wseService, objectStore, TemplateType.PropertyTemplateInteger32, CardinalityType.ListOf, "CEWS_ListofIntegerProp1", "en-US");
         ObjectValue objPropertyTemplateObject = Util.CreatePropertyTemplate(wseService, objectStore, 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 = objectStore;
         ObjectValue objDocClass = Util.CreateClassDef(wseService, objectStore, "CEWS_DocClass1", "en-US", objDocumentClassDef);
         Util.AddPropDefToClassDef(wseService, objectStore, objDocClass, objPropertyTemplateString, null);
         Util.AddPropDefToClassDef(wseService, objectStore, objDocClass, objPropertyTemplateListofInteger, null);
         string folderClassId = "{01A3A8CA-7AEC-11D1-A31B-0020AF9FBB1C}"; // Folder
         Util.AddPropDefToClassDef(wseService, objectStore, objDocClass, objPropertyTemplateObject, folderClassId);

      } //Main
   } // class1
     
   /// <summary>
   /// PropertyTemplate type enumeration.
   /// </summary>
   public enum TemplateType
   {
      PropertyTemplateBinary, 
      PropertyTemplateBoolean, 
      PropertyTemplateDateTime, 
      PropertyTemplateFloat64, 
      PropertyTemplateId, 
      PropertyTemplateInteger32, 
      PropertyTemplateString, 
      PropertyTemplateObject
   } // enum TemplateType
   
   /// <summary>
   /// Property Cardinality enumeration.
   /// </summary>
   public enum CardinalityType
   {
      Singleton = 0,
      Enumeration = 1,
      ListOf = 2
   } // enum CardinalityType
   
   /// <summary>
   /// Utility class for creation of PropertyTemplate and ClassDefinition objects.
   /// </summary>
   public class Util
   {
      public Util()
      {
      } // Util
	  
      /// <summary>
      /// Create a PropertyTemplate object.
      /// </summary>
      /// <param name="wseService">Reference to Web Service object.</param>
      /// <param name="objectStore">Object Store name.</param>
      /// <param name="templateType">Type of PropertyTemplate.</param>
      /// <param name="cardinality">Cardinality.</param>
      /// <param name="displayName">Property's DisplayName.</param>
      /// <param name="localeName">Display Name's LocaleName.</param>
      /// <returns>Reference to created PropertyTemplate object.</returns>
      public static ObjectValue CreatePropertyTemplate(FNCEWS35ServiceWse wseService, string objectStore, TemplateType templateType, CardinalityType cardinality, string displayName, string localeName)
      {
         // Allocate property array for PropertyTemplate object
         int propCount = (templateType == TemplateType.PropertyTemplateObject) ? 4 : 5;
         ModifiablePropertyType[] elemActionProperties = new ModifiablePropertyType[propCount];
   
         // DisplayNames dependent object
         DependentObjectType[] propertyTemplateDisplayNames = new DependentObjectType[1]; 
         ModifiablePropertyType[] localizedStringProps = new ModifiablePropertyType[2];
		 
         // LocalizedText property
         SingletonString propLocalizedText = new SingletonString();
         propLocalizedText.propertyId = "LocalizedText";
         propLocalizedText.Value = displayName;
         localizedStringProps[0] = propLocalizedText;
   
         // LocaleName property
         SingletonString propLocaleName = new SingletonString();
         propLocaleName.propertyId = "LocaleName";
         propLocaleName.Value = localeName;
         localizedStringProps[1] = propLocaleName;
   
         // LocalizedString dependent object 
         DependentObjectType objLocalizedString = new DependentObjectType();
         objLocalizedString.classId = "LocalizedString";
         objLocalizedString.objectStore = objectStore;
         objLocalizedString.Property = localizedStringProps; 
         objLocalizedString.dependentAction = DependentObjectTypeDependentAction.Insert;
         objLocalizedString.dependentActionSpecified = true;
         propertyTemplateDisplayNames[0] = objLocalizedString;
         
         // DisplayNames property
         ListOfObject propDisplayNames = new ListOfObject();
         propDisplayNames.propertyId = "DisplayNames";
         propDisplayNames.Value = propertyTemplateDisplayNames;
         elemActionProperties[0] = propDisplayNames;
         
         // Cardinality property
         SingletonInteger32 propCardinality = new SingletonInteger32();
         propCardinality.propertyId = "Cardinality";
         propCardinality.Value = (int)cardinality;
         propCardinality.ValueSpecified = true;
         elemActionProperties[1] = propCardinality;
   
         // IsValueRequired property
         SingletonBoolean propIsHidden = new SingletonBoolean();
         propIsHidden.propertyId = "IsHidden";
         propIsHidden.Value = false;
         propIsHidden.ValueSpecified = true;
         elemActionProperties[2] = propIsHidden;
		 
         // IsValueRequired property
         SingletonBoolean propIsValueRequired = new SingletonBoolean();
         propIsValueRequired.propertyId = "IsValueRequired";
         propIsValueRequired.Value = false;
         propIsValueRequired.ValueSpecified = true;
         elemActionProperties[3] = propIsValueRequired;
		 
         // IsPersistent property: read-only on Object properties.
         if (templateType != TemplateType.PropertyTemplateObject)
         {
            SingletonBoolean propIsPersistent = new SingletonBoolean();
            propIsPersistent.propertyId = "IsPersistent";
            propIsPersistent.Value = true;
            propIsPersistent.ValueSpecified = true;
            elemActionProperties[4] = propIsPersistent;
         }
   
         // Allocate a CreateAction object
         CreateAction verbCreate = new CreateAction();
         verbCreate.classId = templateType.ToString();
		 
         // Construct ChangeRequestType item
         ChangeRequestType elemChangeRequestType = new ChangeRequestType();
         elemChangeRequestType.Action = new ActionType[1]{verbCreate};
         elemChangeRequestType.ActionProperties = elemActionProperties;
         elemChangeRequestType.TargetSpecification = new ObjectReference();
         elemChangeRequestType.TargetSpecification.classId = "ObjectStore";
         elemChangeRequestType.TargetSpecification.objectStore = objectStore;
		 
         // Construct ExecuteChangesRequest
         ExecuteChangesRequest elemExecuteChangesRequest = new ExecuteChangesRequest();
         elemExecuteChangesRequest.ChangeRequest = new ChangeRequestType[1]{elemChangeRequestType};
         elemExecuteChangesRequest.refresh = true;
         elemExecuteChangesRequest.refreshSpecified = true;
   
         // Issue ExecuteChanges
         ChangeResponseType[] elemChangeResponseTypeArray = wseService.ExecuteChanges(elemExecuteChangesRequest);
         Console.WriteLine("Created PropertyTemplate '{0}'", displayName);
   
         return elemChangeResponseTypeArray[0];
      } // CreatePropertyTemplate
   
      /// <summary>
      /// Create a ClassDefinition object, subclassing another object.
      /// </summary>
      /// <param name="wseService">Reference to Web Service object.</param>
      /// <param name="objectStore">Object Store name.</param>
      /// <param name="displayName">DisplayName of ClassDefinition to create.</param>
      /// <param name="localeName">LocaleName for DisplayName.</param>
      /// <param name="superClassDef">ObjectReference to ClassDefinition of object to subclass.</param>
      /// <returns>Reference to created ClassDefinition object.</returns>
      public static ObjectValue CreateClassDef(FNCEWS35ServiceWse wseService, string objectStore, string displayName, string localeName, ObjectReference superClassDef)
      {
         // Allocate property array for ClassDefinition object
         ModifiablePropertyType[] elemActionProperties = 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 propLocalizedText = new SingletonString();
         propLocalizedText.propertyId = "LocalizedText";
         propLocalizedText.Value = displayName;
         objProperties[0] = propLocalizedText;
   
         // LocaleName property
         SingletonString propLocaleName = new SingletonString();
         propLocaleName.propertyId = "LocaleName";
         propLocaleName.Value = localeName;
         objProperties[1] = propLocaleName;
   
         // LocalizedString dependent object 
         DependentObjectType objLocalizedString = new DependentObjectType();
         objLocalizedString.classId = "LocalizedString";
         objLocalizedString.objectStore = objectStore;
         objLocalizedString.Property = objProperties; 
         objLocalizedString.dependentAction = DependentObjectTypeDependentAction.Insert;
         objLocalizedString.dependentActionSpecified = true;
         objDisplayNames[0] = objLocalizedString;
         
         // DisplayNames property
         ListOfObject propDisplayNames = new ListOfObject();
         propDisplayNames.propertyId = "DisplayNames";
         propDisplayNames.Value = objDisplayNames;
         elemActionProperties[0] = propDisplayNames;
   
         // Allocate a CreateAction object
         CreateAction action = new CreateAction();
         action.classId = superClassDef.classId;
   
         // Construct ChangeRequestType item
         ChangeRequestType elemChangeRequestType = new ChangeRequestType();
         elemChangeRequestType.Action = new ActionType[1]{action};
         elemChangeRequestType.ActionProperties = elemActionProperties;
         elemChangeRequestType.TargetSpecification = superClassDef;
   
         // Construct ExecuteChangesRequest
         ExecuteChangesRequest executeChangesRequestItem = new ExecuteChangesRequest();
         executeChangesRequestItem.ChangeRequest = new ChangeRequestType[1]{elemChangeRequestType};
         executeChangesRequestItem.refresh = true;
         executeChangesRequestItem.refreshSpecified = true;
		 
         // Issue ExecuteChanges
         ChangeResponseType[] response = wseService.ExecuteChanges(executeChangesRequestItem);
         Console.WriteLine("Created ClassDefinition '{0}'", displayName);
   
         return response[0];
      } // CreateClassDef
   
      /// <summary>
      /// Add a Property to a Class.
      /// </summary>
      /// <param name="wseService">Reference to Web Service object.</param>
      /// <param name="objectStore">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(FNCEWS35ServiceWse wseService, string objectStore, 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 propRequiredClassId = new SingletonId();
            propRequiredClassId.propertyId = "RequiredClassId";
            propRequiredClassId.Value = objectRequiredId;
            objPropertyDefinition.Property[1] = propRequiredClassId;
         }
         else
         {
            objPropertyDefinition.Property = new PropertyType[2];
            objPropertyDefinition.Property[0] = objNewPropertyTemplate;
         }
		 
         // Set PropertyDefinitions collection
         DependentObjectType[] objPropertyDefinitions = new DependentObjectType[1]; 
         objPropertyDefinitions[0] = objPropertyDefinition; 
   
         ListOfObject propPropertyDefinitions = new ListOfObject();
         propPropertyDefinitions.propertyId = "PropertyDefinitions";
         propPropertyDefinitions.Value = objPropertyDefinitions;
		 
         ModifiablePropertyType[] elemActionProperties = new ModifiablePropertyType[1];
         elemActionProperties[0] = propPropertyDefinitions;
   
         UpdateAction verbUpdate = new UpdateAction();
         ChangeRequestType elemChangeRequestType = new ChangeRequestType();
         elemChangeRequestType.Action = new ActionType[1]{verbUpdate};
         elemChangeRequestType.ActionProperties = elemActionProperties;
   
         ObjectReference objectReferenceItem = new ObjectReference();
         objectReferenceItem.classId = objClassDefinition.classId;
         objectReferenceItem.objectId = objClassDefinition.objectId;
         objectReferenceItem.objectStore = objClassDefinition.objectStore;
         elemChangeRequestType.TargetSpecification = objectReferenceItem;
   
         ExecuteChangesRequest elemExecuteChangesRequest = new ExecuteChangesRequest();
   
         elemExecuteChangesRequest.ChangeRequest = new ChangeRequestType[1]{elemChangeRequestType};
         elemExecuteChangesRequest.refresh = true;
         elemExecuteChangesRequest.refreshSpecified = true;
   
         ChangeResponseType[] elemChangeResponseTypeArray = wseService.ExecuteChanges(elemExecuteChangesRequest);
         return elemChangeResponseTypeArray[0];

      } // AddPropertyDefToClassDef
   } // class Util   
}