Choice Lists Example

The following code example demonstrates how to retrieve all of the choice lists (and their choice values) in a given object store. 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 choicelists_ex.CEWS;
 
namespace choicelists_ex
{

   /// <summary>
   /// Retrieve all the choice lists in an object store.
   /// </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";
         
         // Specify an ObjectStore object to retrieve
         ObjectSpecification objObjectSpecification = new ObjectSpecification();
         objObjectSpecification.classId = "ObjectStore";  // Symbolic name of object store class
         objObjectSpecification.objectStore = strObjStoreName;   // Object store name

         // Create an object request for the ObjectStore object
         ObjectRequestType[] objObjectRequestTypeArray = new ObjectRequestType[1];
         objObjectRequestTypeArray[0] = new ObjectRequestType();
         objObjectRequestTypeArray[0].SourceSpecification = objObjectSpecification; 
         objObjectRequestTypeArray[0].maxElements = -1;
         objObjectRequestTypeArray[0].id = "ObjectStore";

         // Create a property filter to limit the properties returned from the ObjectStore 
         // object and its object-valued properties to ChoiceLists, ChoiceValues, and Name
         PropertyFilterType objPropFilter = new PropertyFilterType();
         objPropFilter.maxRecursion = 10;
         objPropFilter.maxElements = -1;
         objPropFilter.maxRecursionSpecified = true;

         // Create filter element array to hold an IncludeProperties specification for 3 properties
         objPropFilter.IncludeProperties = new FilterElementType[3];

         // Create filter element for ChoiceLists property
         objPropFilter.IncludeProperties[0] = new FilterElementType();
         objPropFilter.IncludeProperties[0].Value = "ChoiceLists";

         // Create filter element for ChoiceValues property
         objPropFilter.IncludeProperties[1] = new FilterElementType();
         objPropFilter.IncludeProperties[1].Value = "ChoiceValues";

         // Create filter element for Name property
         objPropFilter.IncludeProperties[2] = new FilterElementType();
         objPropFilter.IncludeProperties[2].Value = "Name";       

         // Add property filter to object request 
         objObjectRequestTypeArray[0].PropertyFilter = objPropFilter;    

         // Create an object response for a GetObjects operation     
         ObjectResponseType[] objObjectResponseTypeArray;

         try
         {
            // Retrieve the ObjectStore object from the server   
            objObjectResponseTypeArray = wseService.GetObjects(objObjectRequestTypeArray);
         }
         catch (System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while requesting an object: [" + ex.Message + "]");
            return;
         }

         // Create an ObjectValue element to hold the properties collection of the retrieved ObjectStore object
         ObjectValue objObjectStore = null;
         if (objObjectResponseTypeArray[0] is SingleObjectResponse)
         {
            objObjectStore = ((SingleObjectResponse)objObjectResponseTypeArray[0]).Object;
         }

         // Get the ChoiceLists property of the ObjectStore object
         EnumOfObject prpChoiceLists = null;
         foreach (PropertyType prpProperty in objObjectStore.Property)
         {
            if (prpProperty.propertyId == "ChoiceLists")
            {
               prpChoiceLists = (EnumOfObject)prpProperty;
            }
         }

         // Get each ChoiceList object in the ChoiceLists property collection
         int numChoiceLists = (prpChoiceLists.Value == null) ? 0 : prpChoiceLists.Value.Length;
         for (int iLists = 0; iLists < numChoiceLists; iLists++)
         {
            // Get the Name property of each ChoiceList object
            SingletonString prpChoiceListName = null;
            foreach (PropertyType prpProperty in prpChoiceLists.Value[iLists].Property)
            {
               if (prpProperty.propertyId == "Name")
               {
                  prpChoiceListName = (SingletonString)prpProperty;
               }
            }
            Console.WriteLine("\nChoice list name: " + prpChoiceListName.Value + "\n");

            // Get the ChoiceValues property of each ChoiceList object
            ListOfObject prpChoiceValues = null;
            foreach (PropertyType prpProperty in prpChoiceLists.Value[iLists].Property)
            {
               if (prpProperty.propertyId == "ChoiceValues")
               {
                  prpChoiceValues = (ListOfObject)prpProperty;
               }
            }

            // Get each Choice object in the ChoiceValues property collection
            int numChoiceElements = (prpChoiceValues.Value == null) ? 0 : prpChoiceValues.Value.Length;
            for (int iChValues = 0; iChValues < numChoiceElements; iChValues++)
            {
               // Get the Choice object
               DependentObjectType objChoice = (DependentObjectType)prpChoiceValues.Value[iChValues];
               
               // Get the Name property of the Choice object
               SingletonString prpChoiceName = null;
               foreach (PropertyType prpProperty in objChoice.Property)
               {
                  if (prpProperty.propertyId == "Name")
                  {
                     prpChoiceName = (SingletonString)prpProperty;
                  }
               }
            
               // Print the Name property values of the ChoiceList and Choice objects
               Console.WriteLine("The number " + (iChValues + 1).ToString() + " element in <" + 
               prpChoiceListName.Value + ">" + " : " + prpChoiceName.Value );
            }
         }
         Console.WriteLine("Press Enter to end");
         Console.ReadLine();   
      }
   }
}