Modifying Exchange items



You can modify any elements of calendar, contact, and task items that you can set in ColdFusion. For mail message, you can change the Importance, Sensitivity, and IsRead flags, and you can move the mail messages between folders.

Note: If an item has attachments and you specify attachments when you modify the item, the new attachments are added to the previous attachments; they do not replace them. Use the deleteAttachments action to remove any obsolete or changed attachments.

Modifying calendar, contact, and task items

You can modify calendar, contact, and task items by using the cfexchangecalendar, cfexchangecontact, or cfexchangetask tag with an action attribute value of modify. You specify a contact, event, or task attribute with a structure that contains the item properties that you want to change, and their new values. You do not have to specify the values for properties that you are not changing. To change the end time of a calendar task, for example, you specify only an EndTime field in the event attribute structure.

The following example lets you create, and then modify a calendar event. When you first submit the form, ColdFusion creates the calendar event and redisplays the form with the data you entered. Accept the event before you modify the form and resubmit it. When you submit the form a second time, ColdFusion sends the modification information. For information about accepting events, see Working with meetings and appointments.

The following example resends all the event data (to limit the example length), but you could change the example so that it only sends modified data. This example also omits recurrence information to keep the code relatively simple:

<!--- Initialize the form.eventID to 0, to indicate a new event. ---> 
<!--- The EventID field is a hidden field managed by this application. ---> 
<cfparam name="form.eventID" default="0"> 
 
<!--- If the form was submitted, populate an event structure from it. ---> 
<cfif isDefined("Form.Submit")> 
    <cfscript> 
        sEvent=StructNew(); 
        sEvent.AllDayEvent="false"; 
        sEvent.Subject=Form.subject; 
        if (IsDefined("Form.allDay")) { 
            sEvent.AllDayEvent="true"; 
            sEvent.StartTime=createDateTime(Year(Form.date), Month(Form.date), 
                Day(Form.date), 8, 0, 0); 
        } 
        else { 
            sEvent.StartTime=createDateTime(Year(Form.date), Month(Form.date), 
                Day(Form.date), Hour(Form.startTime), Minute(Form.startTime), 0); 
            sEvent.EndTime=createDateTime(Year(Form.date), Month(Form.date), 
                Day(Form.date), Hour(Form.endTime), Minute(Form.endTime), 0); 
        } 
        sEvent.Location=Form.location; 
        sEvent.RequiredAttendees=Form.requiredAttendees; 
        sEvent.OptionalAttendees=Form.optionalAttendees; 
        //sEvent.Resources=Form.resources; 
        if (Form.reminder NEQ "") { 
            sEvent.Reminder=Form.reminder; 
        } 
        else { 
            sEvent.Reminder=0; 
        } 
        sEvent.Importance=Form.importance; 
        sEvent.Sensitivity=Form.sensitivity; 
        sEvent.message=Form.Message; 
    </cfscript> 
     
    <!--- If the form is being submitted for the first time,  
            create an event. ---> 
    <cfif form.eventID EQ 0> 
    <!--- Create the event in Exchange ---> 
        <cfexchangecalendar action="create"  
            username ="#user1#" 
            password="#password1#" 
            server="#exchangeServerIP#" 
            event="#sEvent#" 
            result="theUID"> 
        <!--- Display the new event UID and set form.eventID to it. ---> 
        <cfif isDefined("theUID")> 
            <cfoutput>Event Added. UID is #theUID#</cfoutput> 
            <cfset Form.eventID = theUID > 
        </cfif> 
 
    <cfelse> 
    <!--- The form is being resubmitted with new data; update the event. ---> 
        <cfexchangecalendar action="modify"  
            username ="#user1#" 
            password="#password1#" 
            server="#exchangeServerIP#" 
            event="#sEvent#" 
            uid="#Form.eventID#"> 
        <cfoutput>Event ID #Form.eventID# Updated.</cfoutput> 
 
    </cfif> 
</cfif> 
 
<!--- A self-submitting form for the event information ---> 
<cfform format="xml" preservedata="true" style="width:500" height="600"> 
    <cfinput type="text" label="Subject" name="subject" style="width:435"> 
    <br /> 
    <cfinput type="checkbox" label="All Day Event" name="allDay"> 
    <cfinput type="datefield" label="Date" name="date" validate="date" 
        style="width:100"> 
    <cfinput type="text" label="Start Time" name="startTime" validate="time" 
        style="width:100"> 
    <cfinput type="text" label="End Time" name="endTime" validate="time" 
        style="width:100"><br /> 
    <cfinput type="text" label="Location" name="location" 
        style="width:435"><br /> 
    <cfinput type="text" label="Required Attendees" name="requiredAttendees" 
        style="width:435"><br /> 
    <cfinput type="text" label="Optional Attendees" name="optionalAttendees" 
        style="width:435"><br /> 
    <cfinput type="text" label="Resources" name="resources" 
        style="width:435"><br /> 
    <cfinput type="text" label="Reminder (minutes)" validate="integer" 
        name="reminder" style="width:200"> 
    <cfselect name="importance" label="Importance" style="width:100"> 
        <option value="normal">Normal</option> 
        <option value="high">High</option> 
        <option value="low">Low</option> 
    </cfselect>  
    <cfselect name="sensitivity" label="Sensitivity" style="width:100"> 
        <option value="normal">Normal</option> 
        <option value="company-confidential">Confidential</option> 
        <option value="personal">Personal</option> 
        <option value="private">Private</option> 
    </cfselect>  
    <cfinput type="textarea" label="Message" name="message" style="width:435; 
        height:100"> 
    <cfinput type="hidden" name="eventID" value="#Form.EventID#"> 
    <cfinput type="Submit" name="submit" value="Submit" > 
</cfform> 

Setting mail attributes

To set a mail message’s Importance, Sensitivity, or IsRead flag, use the cfexchangemail tag with an action attribute value of set. Specify only the flags that you are changing in the message attribute.

The following example snippet implements a catch-up operation by changing the IsRead flag to true on all mail messages that are directly in the Inbox and are more than two weeks old. The example does not change the flags on any messages in folders in the Inbox; to do so, use a separate cfexchangemail tag for each folder. For information on accessing and using multiple folders, see Getting and using folder names.

<!--- Create a structure with a true IsRead field ---> 
<cfset changeValues.IsRead="true"> 
 
<!--- Open the connection. ---> 
<cfexchangeConnection 
    action="open" 
    username ="#user1#" 
    password="#password1#" 
    server="#exchangeServerIP#" 
    connection="conn1"> 
 
<!--- Get the mail in the Inbox that is at least two weeks old. ---> 
<cfexchangemail action="get" name="oldMail" connection="conn1"> 
    <cfexchangefilter name="timeSent" from="01/01/2000"  
        to="#DateAdd("d","-14", Now())#"> 
</cfexchangemail> 
 
<!--- Loop through the resulting oldMail query and set the IsRead indicator 
            to true. ---> 
<cfloop query="oldMail"> 
    <cfexchangemail action="set"  
        connection="conn1"  
        message="#changeValues#" 
        uid="#oldMail.uid#"> 
</cfloop> 
 
<!--- Close the connection. ---> 
<cfexchangeConnection 
    action="close" 
    connection="conn1">

Moving mail between folders

To move a one or more mail messages from one folder to another, use the cfexchangemail tag move action, as shown in the following code snippet, which moves all messages with the subject “Rams and Ewes” from the Unread folder in the Inbox to the Sheep folder in the inbox.

<cfexchangemail action="move" connection="con1" folder="Inbox/Unread"  
        destinationfolder="Inbox/Sheep"> 
    <cfexchangefilter name="subject" value="Rams and Ewes"> 
</cfexchangemail>