Folder Creation Example

The following code example illustrates how to create a folder. 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 foldercreate_ex.CEWS_DIME;

namespace foldercreate_ex
{
   /// <summary>
   /// Create a folder.
   /// </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
         string folderName = "TestFolder"; // Name of the folder to create
         System.DateTime dateCreated = new System.DateTime();
   
         // 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";
		 
         // Build the Create action for a folder object
         CreateAction verbCreate = new CreateAction();
         verbCreate.classId = "Folder";
		 
         // Assign the action to the ChangeRequestType element
         ChangeRequestType elemChangeRequestType = new ChangeRequestType();
         elemChangeRequestType.Action = new ActionType[1];
         elemChangeRequestType.Action[0] = (ActionType)verbCreate; // Assign Create action
         
         // Specify the target object (an object store) for the actions
         elemChangeRequestType.TargetSpecification = new ObjectReference(); 
         elemChangeRequestType.TargetSpecification.classId = "ObjectStore";
         elemChangeRequestType.TargetSpecification.objectId = objectStore; 
         elemChangeRequestType.id = "1";
		 
         // Build a list of properties to set in the new doc
         ModifiablePropertyType[] elemInputProps = new ModifiablePropertyType[2];
		 
         // Specify and set a string-valued property for the FolderName property
         SingletonString propFolderName = new SingletonString();
         propFolderName.propertyId = "FolderName";
         propFolderName.Value = folderName; // Name of the new folder
         elemInputProps[0] = propFolderName; // 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 = objectStore;
		 
         // Specify and set an object-valued property for the Parent property
         SingletonObject propParent = new SingletonObject();
         propParent.propertyId = "Parent"; 
         propParent.Value = (ObjectEntryType)objRootFolder; // Set its value to the RootFolder object
         elemInputProps[1] = propParent; // Add to property list
         
         // Assign list of folder properties to set in ChangeRequestType element
         elemChangeRequestType.ActionProperties = elemInputProps;
		 
         // 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
         elemChangeRequestType.RefreshFilter = new PropertyFilterType();
         elemChangeRequestType.RefreshFilter.ExcludeProperties = excludeProps;
		 
         // Create array of ChangeRequestType elements and assign ChangeRequestType element to it 
         ChangeRequestType[] elemChangeRequestTypeArray = new ChangeRequestType[1];
         elemChangeRequestTypeArray[0] = elemChangeRequestType;
		 
         // Create ChangeResponseType element array 
         ChangeResponseType[] elemChangeResponseTypeArray = null;
         
         // Build ExecuteChangesRequest element and assign ChangeRequestType element array to it
         ExecuteChangesRequest elemExecuteChangesRequest = new ExecuteChangesRequest(); 
         elemExecuteChangesRequest.ChangeRequest = elemChangeRequestTypeArray;
         elemExecuteChangesRequest.refresh = true; // return a refreshed object
         elemExecuteChangesRequest.refreshSpecified = true;
		 
         try
         {
            // Call ExecuteChanges operation to implement the folder creation
            elemChangeResponseTypeArray = wseService.ExecuteChanges(elemExecuteChangesRequest);
         }
         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 (elemChangeResponseTypeArray==null || elemChangeResponseTypeArray.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 propProperty in elemChangeResponseTypeArray[0].Property)
         {
            // If property found, store its value
            if (propProperty.propertyId == "FolderName")
            {
               folderName = ((SingletonString)propProperty).Value;
               break;
            }
         }
		 
         // Capture value of the DateCreated property in the returned doc object
         foreach (PropertyType propProperty in elemChangeResponseTypeArray[0].Property)
         {
            // If property found, store its value
            if (propProperty.propertyId == "DateCreated")
            {
               dateCreated = ((SingletonDateTime)propProperty).Value;
               break;
            }
         }
		 
         Console.WriteLine("The folder " + folderName + " was successfully created " + dateCreated + ".");
		 
         } //Main
   } // class1
}