Folder Creation Example

The following code example illustrates how to create a folder. 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 foldercreate_ex.CEWS;
 
namespace foldercreate_ex
{
   /// <summary>
   /// Create a folder.
   /// </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
         string strFolderName = "TestFolder"; // Name of the folder to create
         System.DateTime dateCreated = new System.DateTime();
   
         // 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";
		 
         // Build the Create action for a folder object
         CreateAction objCreate = new CreateAction();
         objCreate.classId = "Folder";
		 
         // Assign the action to the ChangeRequestType element
         ChangeRequestType objChangeRequestType = new ChangeRequestType();
         objChangeRequestType.Action = new ActionType[1];
         objChangeRequestType.Action[0] = (ActionType)objCreate; // Assign Create action
         
         // Specify the target object (an object store) for the actions
         objChangeRequestType.TargetSpecification = new ObjectReference(); 
         objChangeRequestType.TargetSpecification.classId = "ObjectStore";
         objChangeRequestType.TargetSpecification.objectId = strObjStoreName; 
         objChangeRequestType.id = "1";
		 
         // Build a list of properties to set in the new doc
         ModifiablePropertyType[] objInputProps = new ModifiablePropertyType[2];
		 
         // Specify and set a string-valued property for the FolderName property
         SingletonString prpFolderName = new SingletonString();
         prpFolderName.propertyId = "FolderName";
         prpFolderName.Value = strFolderName; // Name of the new folder
         objInputProps[0] = prpFolderName; // Add to property list
		 
         // Create an object reference to the root folder
         ObjectReference objRootFolder = new ObjectReference();
         objRootFolder.classId = "Folder";
         objRootFolder.objectId = "{0F1E2D3C-4B5A-6978-8796-A5B4C3D2E1F0}";
         objRootFolder.objectStore = strObjStoreName;
		 
         // Specify and set an object-valued property for the Parent property
         SingletonObject prpParent = new SingletonObject();
         prpParent.propertyId = "Parent"; 
         prpParent.Value = (ObjectEntryType)objRootFolder; // Set its value to the RootFolder object
         objInputProps[1] = prpParent; // Add to property list
         
         // Assign list of folder properties to set in ChangeRequestType element
         objChangeRequestType.ActionProperties = objInputProps;
		 
         // Build a list of properties to exclude on the new folder object that will be returned
         string[] excludeProps = new string[2];
         excludeProps[0] = "Owner";
         excludeProps[1] = "DateLastModified";
		 
         // Assign the list of excluded properties to the ChangeRequestType element
         objChangeRequestType.RefreshFilter = new PropertyFilterType();
         objChangeRequestType.RefreshFilter.ExcludeProperties = excludeProps;
		 
         // Create array of ChangeRequestType elements and assign ChangeRequestType element to it 
         ChangeRequestType[] objChangeRequestTypeArray = new ChangeRequestType[1];
         objChangeRequestTypeArray[0] = objChangeRequestType;
		 
         // Create ChangeResponseType element array 
         ChangeResponseType[] objChangeResponseTypeArray = null;
         
         // Build ExecuteChangesRequest element and assign ChangeRequestType element array to it
         ExecuteChangesRequest objExecuteChangesRequest = new ExecuteChangesRequest(); 
         objExecuteChangesRequest.ChangeRequest = objChangeRequestTypeArray;
         objExecuteChangesRequest.refresh = true; // return a refreshed object
         objExecuteChangesRequest.refreshSpecified = true;
		 
         try
         {
            // Call ExecuteChanges operation to implement the folder creation
            objChangeResponseTypeArray = wseService.ExecuteChanges(objExecuteChangesRequest);
         }
         catch(System.Net.WebException ex)
         {
            Console.WriteLine("An exception occurred while creating a folder: [" + ex.Message + "]");
            return;
         }
		 
         // The new folder object should be returned, unless there is an error
         if (objChangeResponseTypeArray==null || objChangeResponseTypeArray.Length < 1)
         {
            Console.WriteLine("A valid object was not returned from the ExecuteChanges operation");
            return;
         }
		 
         // Capture value of the FolderName property in the returned doc object
         foreach (PropertyType prpProperty in objChangeResponseTypeArray[0].Property)
         {
            // If property found, store its value
            if (prpProperty.propertyId == "FolderName")
            {
               strFolderName = ((SingletonString)prpProperty).Value;
               break;
            }
         }
		 
         // Capture value of the DateCreated property in the returned doc object
         foreach (PropertyType prpProperty in objChangeResponseTypeArray[0].Property)
         {
            // If property found, store its value
            if (prpProperty.propertyId == "DateCreated")
            {
               dateCreated = ((SingletonDateTime)prpProperty).Value;
               break;
            }
         }
		 
         Console.WriteLine("The folder " + strFolderName + " was successfully created " + dateCreated + ".");
         Console.WriteLine("Press Enter to end");
         Console.ReadLine();   
		 
      }
   }
}