Thursday 30 January 2014

Adding event in Calendar Web part Using Sequential Workflow in Sharepoint using C# to simultaneously add event in to another Calendar Web part in different Site Collection

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;

namespace EventsCalendar.AddEventsCalendarWorkflow
{
    public sealed partial class AddEventsCalendarWorkflow : SequentialWorkflowActivity
    {
        public AddEventsCalendarWorkflow()
        {
            InitializeComponent();
        }


        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();
        public string Title, Location, Description, AllDayEvent, Recurrence, StartTime, EndTime, RecurrenceData, WMICountry, WMISharedGroups;
        DateTime? DayStartTime = null;
        DateTime? DayEndTime = null;

        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            try
            {
                WMICountry = workflowProperties.Item["WMICountry"] != null ? workflowProperties.Item["WMICountry"].ToString() : "";
                if (WMICountry != "")
                {
                    SPFieldLookupValueCollection countries = new SPFieldLookupValueCollection(WMICountry);

                    //loop all new collection (if new country is added)
                    foreach (SPFieldLookupValue country in countries)
                    {
                        //get country details
                        SPListItem countryItem = workflowProperties.Web.Lists["Country Sites"].GetItemById(country.LookupId);

                        string email = Convert.ToString(countryItem["Contact Email"]);
                        string siteURL = Convert.ToString(countryItem["Site URL"]);
                        string active = Convert.ToString(countryItem["Active?"]);

                        //insert into record here
                        if (!string.IsNullOrEmpty(siteURL) && active.Trim().ToLower() == "true")
                        {
                            // Open the site
                            SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                using (SPSite site = new SPSite(siteURL))
                                {
                                    using (SPWeb web = site.OpenWeb())
                                    {
                                        Title = workflowProperties.Item["Title"] != null ? workflowProperties.Item["Title"].ToString() : "";
                                        Location = workflowProperties.Item["Location"] != null ? workflowProperties.Item["Location"].ToString() : "";
                                        Description = workflowProperties.Item["Description"] != null ? workflowProperties.Item["Description"].ToString() : "";
                                        AllDayEvent = workflowProperties.Item["All Day Event"] != null ? workflowProperties.Item["All Day Event"].ToString() : "";
                                        Recurrence = workflowProperties.Item["Recurrence"] != null ? workflowProperties.Item["Recurrence"].ToString() : "";
                                        StartTime = workflowProperties.Item["Start Time"] != null ? workflowProperties.Item["Start Time"].ToString() : "";
                                        EndTime = workflowProperties.Item["End Time"] != null ? workflowProperties.Item["End Time"].ToString() : "";
                                        RecurrenceData = workflowProperties.Item["RecurrenceData"] != null ? workflowProperties.Item["RecurrenceData"].ToString() : "";


                                        SPListItemCollection listItems = web.Lists["BVHO Calendar"].Items;
                                        SPListItem Calendarlistitem = listItems.Add();

                                        //SPFieldCollection allFields = Calendarlistitem.Fields;

                                        //string Body = string.Empty;

                                        //foreach (SPField field in allFields)
                                        //{
                                        //    Body = Body + "," + string.Format(@"{0} - {1}", field.Title, field.InternalName);
                                        //}

                                        //InsertError(string.Format(@"https://wmiregtpportal.mcfcloud.com/country/CentroAmerica"), "Events Fields " , Body, "");

                                        SPListItem item = workflowProperties.Item;
                                        SPFile spFile = workflowProperties.Item.File;
                                        SPAttachmentCollection objAttchments = item.Attachments;

                                        if (item.Attachments != null)
                                        {
                                            foreach (string fileName in objAttchments)
                                            {
                                                // Perform action on the extracted attachment
                                                SPFile file = item.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix +
                                                                                                      fileName);
                                                byte[] fileData = file.OpenBinary();
                                                Calendarlistitem.Attachments.Add(fileName, fileData);
                                            }
                                        }

                                        Calendarlistitem["Title"] = Title;
                                        Calendarlistitem["Location"] = Location;
                                        Calendarlistitem["Description"] = Description;
                                        Calendarlistitem["fAllDayEvent"] = AllDayEvent;
                                        Calendarlistitem["PublisherItemID"] = workflowProperties.ItemId;

                                        //recurring event
                                        //1. Recurrence = trrue and data should be in XML format
                                        if (RecurrenceData.Contains("<recurrence>"))
                                        {
                                            Calendarlistitem["RecurrenceData"] = RecurrenceData;
                                            Calendarlistitem["EventType"] = 1;
                                            Calendarlistitem["UID"] = System.Guid.NewGuid();
                                            Calendarlistitem["TimeZone"] = 13;
                                            Calendarlistitem["fRecurrence"] = -1;
                                            Calendarlistitem["XMLTZone"] = "<timeZoneRule>" +
                                                "<standardBias>480</standardBias>" +
                                                "<additionalDaylightBias>-60</additionalDaylightBias>" +
                                                "<standardDate><transitionRule  month='10' day='su' weekdayOfMonth='last' />" +
                                                "<transitionTime>2:0:0</transitionTime></standardDate>" +
                                                "<daylightDate><transitionRule  month='4' day='su' weekdayOfMonth='first' />" +
                                                "<transitionTime>2:0:0</transitionTime>" +
                                                "</daylightDate></timeZoneRule>";
                                        }

                                        if (StartTime != "")
                                        {
                                            Calendarlistitem["EventDate"] = StartTime;
                                        }
                                        else
                                        {
                                            Calendarlistitem["EventDate"] = DayStartTime;
                                        }

                                        if (EndTime != "")
                                        {
                                            Calendarlistitem["EndDate"] = EndTime;
                                        }
                                        else
                                        {
                                            Calendarlistitem["EndDate"] = DayEndTime;
                                        }

                                        Calendarlistitem.Update();
                                    }
                                }
                            });
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                InsertError(string.Format(@"https://wmiregtpportal.mcfcloud.com/country/CentroAmerica"), "Events Error Message " + workflowProperties.ItemId, Ex.Message + "~" + Ex.StackTrace, "");
            }
        }



        public void InsertError(string spSiteURL, string title, string body, string Expires)
        {
            DateTime? dateExpires = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(spSiteURL))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["Errors"];

                        SPListItem item = list.Items.Add();

                        item["Title"] = title;
                        item["Body"] = body;

                        if (Expires != "")
                        {
                            item["Expires"] = Expires;
                        }
                        else
                        {
                            item["Expires"] = dateExpires;
                        }

                        item.Update();
                    }
                }
            });
        }

    }
}





No comments:

Post a Comment