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.



8 comments:

  1. In some cases free SharePoint Workflow Scheduler can save you a lot of time. It allows automatic execution of SharePoint workflows ​by schedule.

    http://www.harepoint.com/Products/HarePointWorkflowScheduler/Default.aspx

    ReplyDelete
  2. Any idea why recurring events created are not shown in the calendar view?

    ReplyDelete
  3. This is very interesting. Wish it wasn't necessary, though. I tried downloading and using the solution from CodePlex. But, after installing it in the site collection, activating it and then activating the feature in the site, the custom activity never showed up in the list of available activities in SPD.

    Is this something I'm going to have to parse out myself?

    ReplyDelete
  4. Hi..
    i am beginner.can you please post basic things related to share point.so i can understand it better.
    thanks in advance

    ReplyDelete
  5. Has anyone attempt to convert this solution for visual studio 2013? I'm interested. Please let me know. Thanks in advance. Mike

    ReplyDelete
  6. Mike, I had developed this VS 2012, you can open the same code solution in VS 2013, it will automatically get convert.

    ReplyDelete
  7. I created a 2013 workflow using web service call to copy recurring event to parent calendar. The items copy, but it doesn't render as recurring until you hit Edit Item, then Save. Has anyone else run into this and is there a solution. If I go the route of creating the custom workflow as in this post, does the same issue occur as I stated?

    ReplyDelete
  8. Using the 2013 SP Designer workflow web service call, a recurring event can be POSTED to a parent calendar, but when the item is created in the parent calendar, it shows as a blue line across the calendar, but when you click on the item it shows recurring as empty. Then if you EDIT the item, you see all of the recurrenceData posted correctly. Only if you save from there, will the data post correctly to the calendar even though it was passed. Any insight on how to remedy this?

    ReplyDelete