SharePointAds TextOnly

Thursday 15 December 2011

How to create Calendar recurring event from SharePoint Designer 2010 workflow?


Do you want to create new calendar recurring event from SharePoint workflow? If yes, then you are at correct place.
You can easily create simple calendar event (which doesn’t have recurring properties/values), Some days before I was trying to create new recurring event from SPD workflow using “Create Item” action, and found that we can’t create recurring event from SPD workflows because when we’re trying to add new calendar list item from ‘Create Item’ OOB workflow action, at that point we can’t assign the recurring rules values to ‘Recurrence’ column and there is not any available way to pass recurrence rule xml values.
SPD_WF
I found an interesting article which creates new calendar event with recurring parameters programatically(http://msdn.microsoft.com/en-us/library/ms434156.aspx). But I was looking to create calendar events from SPD workflows and I decided to create workflow custom action to insert new recurring events. If you don’t know how to create custom action for SPD workflows then look at here (http://msdn.microsoft.com/en-us/office365trainingcourse_lab_3_2_topic3#_Toc290553044), this is very simple and easier solution to create custom custom actions from VS. You just have to make following changes,
  1. Add new class file to the solution for recurring event action (I created as “CreateCalendarRecurrenceEvent.cs”)
  2. Your ‘Element.xml’ file would be as following,  

    <?xml version="1.0" encoding="utf-8"?>

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">

      <WorkflowActions>

        <Action Name="Create Recurring Event"

                SandboxedFunction="true"

                Assembly="$SharePoint.Project.AssemblyFullName$"

                ClassName="SPDRecurringEventAction.CreateCalendarRecurrenceEvent"

                FunctionName="CalendarRecurrenceEvent"

                AppliesTo="all"

                Category="My Custom Workflow Activities">


          <RuleDesigner Sentence="Create New calendar recurring event with title : %1 and rule: %2 (Exception to %3)">

            <FieldBind Id="1" Field="eventTitle" Text="Event Name" DesignerType="TextBox" />

            <FieldBind Id="2"  Field="recurrenceRule" Text="Recurrence Rule" DesignerType="TextBox" />

            <FieldBind Id="3" Field="Except" Text="Exception" DesignerType="ParameterNames" />

          </RuleDesigner>


          <Parameters>

            <Parameter Name="__Context"

                       Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext, Microsoft.SharePoint.WorkflowActions"

                       Direction="In" DesignerType="Hide"/>


            <Parameter Name="eventTitle" Type="System.String, mscorlib" Direction="In"

                       DesignerType="TextBox" Description="Event Name" />


            <Parameter Name="recurrenceRule" Type="System.String, mscorlib" Direction="In"

                       DesignerType="TextBox" Description="Recurrence Rule" />


            <Parameter Name="Except" Type="System.String, mscorlib" Direction="Out"

                       DesignerType="ParameterNames" Description="Exception encountered"/>

          </Parameters>

        </Action>

       

      </WorkflowActions>

    </Elements>


  3. In your new class add following method to create new recurring event.
class CreateCalendarRecurrenceEvent

    {

        public Hashtable CalendarRecurrenceEvent(SPUserCodeWorkflowContext context, 
string eventTitle, string recurrenceRule)

        {

            Hashtable results = new Hashtable();

            int iYear = 0, iMonth = 0, iDay = 0;

            results["Except"] = string.Empty;

            try

            {

                using (SPSite site = new SPSite(context.CurrentWebUrl))

                {

                    using (SPWeb web = site.OpenWeb())

                    {


                        iYear = DateTime.Now.Year; iMonth = DateTime.Now.Month; iDay = DateTime.Now.Day;

                        DateTime startTime = new DateTime(iYear, iMonth, iDay, 8, 0, 0); //

                        DateTime endTime = startTime.AddHours(6);


                        SPList cal = web.Lists["Calendar"];

                        SPListItem calEvent = cal.Items.Add();

                        calEvent["Title"] = eventTitle;

                        calEvent["RecurrenceData"] = recurrenceRule;

                        calEvent["EventType"] = 1;

                        calEvent["EventDate"] = startTime;

                        calEvent["EndDate"] = endTime;

                        calEvent["UID"] = System.Guid.NewGuid();

                        calEvent["Recurrence"] = 1;
                        calEvent.Update();

                    }

                }

            }

            catch (Exception ex)

            {

                results["Except"] = ex.ToString();

            }

            return results;

        }

    }


(NOTE: I have used default calendar list to create new recurring event, if you want to create recurring event in another calendar list then change 'Calendar' list name.
)
Build and deploy action and follow the steps given at  http://msdn.microsoft.com/en-us/office365trainingcourse_lab_3_2_topic3#_Toc290553044

In SharePoint Designer workflow, add your custom activity, assign Name and Recurrence Rule pattern as in XML format. You can check more recurrence patterns from Here.
(NOTE :  Rule value must be in single line text, I have used as " <recurrence><rule><firstDayOfWeek>su</firstDayOfWeek><repeat><daily dayFrequency='2' /></repeat><windowEnd>2011-12-30T09:00:00Z</windowEnd></rule></recurrence>
  ")


I have created SPD custom activity solution on Codeplex, you can download actual project solution from here (http://spdcustomaction.codeplex.com/releases/view/78835)

Enjoy custom activity to create new recurring events from SharePoint Designer workflow.