public void SetAppointment(string _company, string _responsibleEployee) { bool isUpdateOutlookEntryID = false; int taskProirity = 0; int sensitivity = 0; int activityTimeType = 0; bool noSync = false; bool noAddList = false; deletedCRMItemCounter = 0; updatedCRMItemCounter = 0; savedCRMItemCounter = 0; insertedOutlookItemCounter = 0; deletedOutlookItemCounter = 0; WebOutlookCrm.smmActivities.SMMACTIVITIESDataTable smmAct = service.GetsmmActivities(_company, _responsibleEployee); Outlook.AppointmentItem oAppointment = null; Outlook.Folder calFolder = null; //Outlook.NameSpace ns = outlookApp.GetNamespace("MAPI"); //Outlook.MAPIFolder calFolder = ns.GetFolderFromID(outlookCalendarID, outlookCalendarStoreID); calFolder = GetFolder(outlookCalendarID, outlookCalendarStoreID); //Outlook.MAPIFolder folder = calFolder; foreach (WebOutlookCrm.smmActivities.SMMACTIVITIESRow item in smmAct) { if (log) { service.WriteInfo("++++++WebService: foreach SetAppointment"); service.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); } noSync = false; noAddList = false; oAppointment = null; bool insert = false; oAppointment = GetAppointment(item.OUTLOOKENTRYID, outlookCalendarStoreID); if (oAppointment == null) { if (log) { service.WriteInfo("++++++WebService: oAppointment == null"); service.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); } if (string.IsNullOrEmpty(item.OUTLOOKENTRYID)) { isUpdateOutlookEntryID = true; insert = true; oAppointment = (Outlook.AppointmentItem)calFolder.Items.Add(Outlook.OlItemType.olAppointmentItem); if (log) { service.WriteInfo("++++++WebService: string.IsNullOrEmpty(item.OUTLOOKENTRYID)"); service.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); service.WriteInfo("++++++WebService: noSync: " + noSync); } //oAppointment = (Outlook.AppointmentItem)outlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem); } else { // ez az ág mikor nem találja az outlookban viszont van OutlikEntryID-ja tehát az outlookból lett törölve if (log) { service.WriteInfo("++++++WebService: string.IsNullOrEmpty(item.OUTLOOKENTRYID) != "); service.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); } noSync = true; insert = false; noAddList = service.DeleteActivity(item.OUTLOOKENTRYID, comapny); //System.Windows.Forms.MessageBox.Show("The " + item.ACTIVITYNUMBER + " activities deleted!"); if (log) { service.WriteInfo("++++++WebService: " + "The " + item.ACTIVITYNUMBER + " activities deleted!"); } deletedCRMItemCounter++; } } else { insert = false; if (log) { service.WriteInfo("++++++WebService: oAppointment != null"); service.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); } isUpdateOutlookEntryID = false; if (oAppointment.LastModificationTime > item.MODIFIEDDATETIME.ToLocalTime()) { if (IsdifferentRecord(oAppointment, item)) { if (log) { service.WriteInfo("++++++WebService: oAppointment.LastModificationTime > item.MODIFIEDDATETIME.ToLocalTime()"); service.WriteInfo("++++++WebService: " + oAppointment.LastModificationTime + " " + item.MODIFIEDDATETIME.ToLocalTime()); service.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); } taskProirity = 0; sensitivity = 0; activityTimeType = 0; OutlookMgt.TASKPRIORITYConvertOutlookOlImportanceToInt(ref taskProirity, oAppointment); OutlookMgt.SENSITIVITYConvertOutlookOlSensitivityToInt(ref sensitivity, oAppointment); OutlookMgt.ACTIVITYTIMETYPEConvertOutlookOlBusyStatusToInt(ref activityTimeType, oAppointment); noSync = service.UpdateActivity(oAppointment.Start.ToUniversalTime(), oAppointment.End.ToUniversalTime(), empId, oAppointment.AllDayEvent, oAppointment.BillingInformation, oAppointment.Body, oAppointment.Subject, oAppointment.Categories, taskProirity, oAppointment.Location, oAppointment.Mileage, oAppointment.ReminderSet, oAppointment.ReminderMinutesBeforeStart, oAppointment.Resources, oAppointment.ResponseRequested, sensitivity, activityTimeType, networkAlias, comapny, oAppointment.EntryID, item.ACTIVITYNUMBER); if (log) { service.WriteInfo("++++++WebService: " + "The " + item.ACTIVITYNUMBER + " activities updated!"); } updatedCRMItemCounter++; } } } if (!noSync) { try { if (IsdifferentRecord(oAppointment, item)) { if (log) { service.WriteInfo("++++++WebService: Create new appointment from activity: " + item.ACTIVITYNUMBER); } oAppointment.Subject = item.PURPOSE.ToString(); oAppointment.Start = Convert.ToDateTime(item.STARTDATETIME.ToLocalTime()); oAppointment.AllDayEvent = Convert.ToBoolean(item.ALLDAY); oAppointment.BillingInformation = item.BILLINGINFORMATION; oAppointment.Body = item.USERMEMO.ToString(); oAppointment.Categories = item.OUTLOOKCATEGORIES; oAppointment.End = Convert.ToDateTime(item.ENDDATETIME.ToLocalTime()); OutlookMgt.TASKPRIORITYConvertIntToOutlookOlImportance(item.TASKPRIORITY, ref oAppointment); oAppointment.Location = item.LOCATION; oAppointment.Mileage = item.MILEAGE; oAppointment.ReminderSet = Convert.ToBoolean(item.REMINDERACTIVE); oAppointment.ReminderMinutesBeforeStart = item.REMINDERMINUTES; oAppointment.Resources = item.OUTLOOKRESOURCES; oAppointment.ResponseRequested = Convert.ToBoolean(item.RESPONSEREQUESTED); OutlookMgt.SENSITIVITYIntToConvertOutlookOlSensitivity(item.SENSITIVITY, ref oAppointment); OutlookMgt.ACTIVITYTIMETYPEConvertIntToOutlookOlBusyStatus(item.ACTIVITYTIMETYPE, ref oAppointment); oAppointment.Save(); if (log) { service.WriteInfo("++++++WebService: Appointment saved."); service.WriteInfo("++++++WebService: oAppointment Entry ID: " + oAppointment.EntryID); service.WriteInfo("++++++WebService: CalFolder Store ID:" + calFolder.StoreID); } if (isUpdateOutlookEntryID) { service.UpdateActivityOutlookEntryId(item.ACTIVITYNUMBER, comapny, oAppointment.EntryID); } if (insert) { insertedOutlookItemCounter++; } else { savedOutlookItemCounter++; } } } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); } } if (!noAddList) { if (oAppointment.EntryID != null) { smmActivitiList.Add(oAppointment.EntryID, item.ACTIVITYNUMBER); } } } this.SearchAppointments(calFolder); }
public static Boolean IsdifferentRecord(Outlook.AppointmentItem oAppointment, Model.ActivityData item) { Boolean differenetRec = false; if (oAppointment.Start != item.STARTDATETIME.ToLocalTime()) { differenetRec = true; } if (oAppointment.End != item.ENDDATETIME.ToLocalTime()) { differenetRec = true; } if (string.IsNullOrEmpty(oAppointment.BillingInformation)) { if (string.IsNullOrEmpty(item.BILLINGINFORMATION)) { } else { differenetRec = true; } } else { if (oAppointment.BillingInformation != item.BILLINGINFORMATION) { differenetRec = true; } } if (string.IsNullOrEmpty(oAppointment.Body)) { if (string.IsNullOrEmpty(item.USERMEMO)) { } else { differenetRec = true; } } else { if (oAppointment.Body.ToString() != item.USERMEMO) { differenetRec = true; } } if (string.IsNullOrEmpty(oAppointment.Subject)) { if (string.IsNullOrEmpty(item.PURPOSE)) { } else { differenetRec = true; } } else { if (oAppointment.Subject.ToString() != item.PURPOSE.ToString()) { differenetRec = true; } } if (string.IsNullOrEmpty(oAppointment.Categories)) { if (string.IsNullOrEmpty(item.OUTLOOKCATEGORIES)) { } else { differenetRec = true; } } else { if (oAppointment.Categories.ToString() != item.OUTLOOKCATEGORIES) { differenetRec = true; } } if (string.IsNullOrEmpty(oAppointment.Location)) { if (string.IsNullOrEmpty(item.LOCATION)) { } else { differenetRec = true; } } else { if (oAppointment.Location.ToString() != item.LOCATION.ToString()) { differenetRec = true; } } if (string.IsNullOrEmpty(oAppointment.Mileage)) { if (string.IsNullOrEmpty(item.MILEAGE)) { } else { differenetRec = true; } } else { if (oAppointment.Mileage.ToString() != item.MILEAGE.ToString()) { differenetRec = true; } } if (oAppointment.ReminderMinutesBeforeStart != item.REMINDERMINUTES) { differenetRec = true; } if (string.IsNullOrEmpty(oAppointment.Resources)) { if (string.IsNullOrEmpty(item.OUTLOOKRESOURCES)) { } else { differenetRec = true; } } else { if (oAppointment.Resources.ToString() != item.OUTLOOKRESOURCES) { differenetRec = true; } } if (oAppointment.AllDayEvent != Convert.ToBoolean(item.ALLDAY)) { differenetRec = true; } if (oAppointment.ReminderSet != Convert.ToBoolean(item.REMINDERACTIVE)) { differenetRec = true; } if (oAppointment.ResponseRequested != Convert.ToBoolean(item.RESPONSEREQUESTED)) { differenetRec = true; } if (oAppointment.Importance != OutlookMgt.TASKPRIORITYConvertIntToOutlookOlImportance(item.TASKPRIORITY)) { differenetRec = true; } if (oAppointment.Sensitivity != OutlookMgt.SENSITIVITYIntToConvertOutlookOlSensitivity(item.SENSITIVITY)) { differenetRec = true; } if (oAppointment.BusyStatus != OutlookMgt.ACTIVITYTIMETYPEConvertIntToOutlookOlBusyStatus(item.ACTIVITYTIMETYPE)) { differenetRec = true; } return(differenetRec); }
private String syncFromCrmToOutlook(string _company, string _responsibleEployee) { printLogMsg("Synchronizing activities from CRM to Outlook..."); if (log) { serviceProxy.WriteInfo("++++++ syncFromCrmToOutlook: CRM ---> Outlook"); } smmActivityList = new Dictionary <string, string>(); bool isUpdateOutlookEntryID = false; int taskProirity = 0; int sensitivity = 0; int activityTimeType = 0; bool noSync = false; bool noAddList = false; try { //XElement booksFromFile = XElement.Load(@"c:\\act.xml"); //String xmlStr = booksFromFile.ToString(); String soapMsg = File.ReadAllText(@"c:\\act.xml"); XmlDocument document = new XmlDocument(); document.LoadXml(soapMsg); //loading soap message as string XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable); manager.AddNamespace("d", "http://someURL"); XmlNodeList xnList = document.SelectNodes("//bookHotelResponse", manager); int nodes = xnList.Count; //foreach (XmlNode xn in xnList) //{ // Status = xn["d:bookingStatus"].InnerText; //} XmlRootAttribute xRoot = new XmlRootAttribute(); xRoot.ElementName = "DocumentElement"; xRoot.IsNullable = true; WebOutlookCrm.smmActivities.SMMACTIVITIESDataTable acts; XmlSerializer ser = new XmlSerializer(typeof(WebOutlookCrm.smmActivities.SMMACTIVITIESDataTable), xRoot); StreamReader reader = new StreamReader("c:\\act.xml"); acts = (WebOutlookCrm.smmActivities.SMMACTIVITIESDataTable)ser.Deserialize(reader); reader.Close(); Stopwatch swGetActs = new Stopwatch(); swGetActs.Start(); string Responsible = _responsibleEployee; /*if (Responsible.ToLower() == "krost") * { * Responsible += "_test"; * }*/ List <Model.ActivityData> smmAct = serviceProxy.GetsmmActivities(_company, Responsible); swGetActs.Stop(); //printLogMsg(String.Format("service.GetsmmActivities: {0}", swGetActs.ElapsedMilliseconds)); Outlook.AppointmentItem oAppointment = null; Outlook.Folder calFolder = GetFolder(outlookCalendarID, outlookCalendarStoreID); progressFromCrm.Minimum = 0; progressFromCrm.Maximum = 1; progressFromCrm.Value = 0; progressFromCrm.Step = 1; if (smmAct.Count == 0) { progressFromCrm.PerformStep(); } else { progressFromCrm.Maximum = smmAct.Count; } foreach (Model.ActivityData item in smmAct) { Stopwatch swAct = new Stopwatch(); swAct.Start(); if (item.STARTDATETIME > item.ENDDATETIME) { printLogMsg(string.Format("Wrong activity setting: end date is earlier than start date! act#: {0}, start: {1}, end: {2}", item.ACTIVITYNUMBER, item.STARTDATETIME.ToLocalTime(), item.ENDDATETIME.ToLocalTime())); printLogMsg("Activity skipped."); continue; } if (log) { //printLogMsg(String.Format("Checking activity: {0} (actnum={1})", item.PURPOSE, item.ACTIVITYNUMBER)); serviceProxy.WriteInfo("++++++ Activity found in CRM : ActivityNumber: " + item.ACTIVITYNUMBER); } noSync = false; noAddList = false; oAppointment = null; bool insert = false; Stopwatch swGetApp = new Stopwatch(); swGetApp.Start(); oAppointment = GetAppointment(item.OUTLOOKENTRYID, outlookCalendarStoreID); swGetApp.Stop(); Stopwatch swCheckApp = new Stopwatch(); swCheckApp.Start(); if (oAppointment == null) { if (log) { serviceProxy.WriteInfo("++++++ Appointment not found in Outlook for Activity: " + item.ACTIVITYNUMBER); } if (string.IsNullOrEmpty(item.OUTLOOKENTRYID)) { isUpdateOutlookEntryID = true; insert = true; oAppointment = (Outlook.AppointmentItem)calFolder.Items.Add(Outlook.OlItemType.olAppointmentItem); if (log) { serviceProxy.WriteInfo("++++++WebService: string.IsNullOrEmpty(item.OUTLOOKENTRYID)"); serviceProxy.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); serviceProxy.WriteInfo("++++++WebService: noSync: " + noSync); } } else { // ez az ág mikor nem találja az outlookban viszont van OutlookEntryID-ja tehát az outlookból lett törölve if (log) { serviceProxy.WriteInfo("++++++WebService: string.IsNullOrEmpty(item.OUTLOOKENTRYID) != "); serviceProxy.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); } noSync = true; insert = false; noAddList = serviceProxy.DeleteActivity(item.OUTLOOKENTRYID, company); if (log) { printLogMsg("Activity removed from CRM: " + item.ACTIVITYNUMBER); serviceProxy.WriteInfo("++++++WebService: " + "The " + item.ACTIVITYNUMBER + " activities deleted!"); } deletedCRMItemCounter++; } } //B19443_CrmChangesToPlugin MGY 2018.05.31 Begin else if (oAppointment.Sensitivity != Microsoft.Office.Interop.Outlook.OlSensitivity.olPrivate) //B19443_CrmChangesToPlugin MGY 2018.05.31 End { insert = false; if (log) { serviceProxy.WriteInfo("++++++WebService: oAppointment != null"); serviceProxy.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); } isUpdateOutlookEntryID = false; if (oAppointment.LastModificationTime > item.MODIFIEDDATETIME.ToLocalTime()) { if (OutlookMgt.IsdifferentRecord(oAppointment, item)) { if (log) { serviceProxy.WriteInfo("++++++WebService: oAppointment.LastModificationTime > item.MODIFIEDDATETIME.ToLocalTime()"); serviceProxy.WriteInfo("++++++WebService: " + oAppointment.LastModificationTime + " " + item.MODIFIEDDATETIME.ToLocalTime()); serviceProxy.WriteInfo("++++++WebService: ActNumber: " + item.ACTIVITYNUMBER); } taskProirity = 0; sensitivity = 0; activityTimeType = 0; OutlookMgt.TASKPRIORITYConvertOutlookOlImportanceToInt(ref taskProirity, oAppointment); OutlookMgt.SENSITIVITYConvertOutlookOlSensitivityToInt(ref sensitivity, oAppointment); OutlookMgt.ACTIVITYTIMETYPEConvertOutlookOlBusyStatusToInt(ref activityTimeType, oAppointment); noSync = serviceProxy.UpdateActivity(oAppointment.Start.ToUniversalTime(), oAppointment.End.ToUniversalTime(), empId, oAppointment.AllDayEvent, oAppointment.BillingInformation, oAppointment.Body, oAppointment.Subject, oAppointment.Categories, taskProirity, oAppointment.Location, oAppointment.Mileage, oAppointment.ReminderSet, oAppointment.ReminderMinutesBeforeStart, oAppointment.Resources, oAppointment.ResponseRequested, sensitivity, activityTimeType, networkAlias, company, oAppointment.EntryID, item.ACTIVITYNUMBER); if (log) { serviceProxy.WriteInfo("++++++WebService: " + "The " + item.ACTIVITYNUMBER + " activities updated!"); printLogMsg("Activity modified in CRM: " + item.ACTIVITYNUMBER); } updatedCRMItemCounter++; } } } //B19443_CrmChangesToPlugin MGY 2018.05.31 Begin else { noSync = true; noAddList = serviceProxy.DeleteActivity(item.OUTLOOKENTRYID, company); if (log) { printLogMsg("Appoointment has became Private, therefore Activity removed from CRM: " + item.ACTIVITYNUMBER); serviceProxy.WriteInfo("++++++WebService: " + "The " + item.ACTIVITYNUMBER + " activities deleted!"); } deletedCRMItemCounter++; } //B19443_CrmChangesToPlugin MGY 2018.05.31 End swCheckApp.Stop(); Stopwatch swCheckApp2 = new Stopwatch(); swCheckApp2.Start(); Stopwatch swIsDiff = new Stopwatch(); Stopwatch swAppFill = new Stopwatch(); if (!noSync) { try { swIsDiff.Start(); bool isDiff = OutlookMgt.IsdifferentRecord(oAppointment, item); swIsDiff.Stop(); if (isDiff) { if (log) { serviceProxy.WriteInfo("++++++WebService: Update appointment from activity: " + item.ACTIVITYNUMBER); } if (!oAppointment.IsRecurring) { swAppFill.Start(); oAppointment.Subject = item.PURPOSE.ToString(); oAppointment.Start = Convert.ToDateTime(item.STARTDATETIME.ToLocalTime()); oAppointment.AllDayEvent = Convert.ToBoolean(item.ALLDAY); oAppointment.BillingInformation = item.BILLINGINFORMATION; oAppointment.Body = item.USERMEMO.ToString(); oAppointment.Categories = item.OUTLOOKCATEGORIES; oAppointment.End = Convert.ToDateTime(item.ENDDATETIME.ToLocalTime()); OutlookMgt.TASKPRIORITYConvertIntToOutlookOlImportance(item.TASKPRIORITY, ref oAppointment); oAppointment.Location = item.LOCATION; oAppointment.Mileage = item.MILEAGE; oAppointment.ReminderSet = Convert.ToBoolean(item.REMINDERACTIVE); oAppointment.ReminderMinutesBeforeStart = item.REMINDERMINUTES; oAppointment.Resources = item.OUTLOOKRESOURCES; oAppointment.ResponseRequested = Convert.ToBoolean(item.RESPONSEREQUESTED); OutlookMgt.SENSITIVITYIntToConvertOutlookOlSensitivity(item.SENSITIVITY, ref oAppointment); OutlookMgt.ACTIVITYTIMETYPEConvertIntToOutlookOlBusyStatus(item.ACTIVITYTIMETYPE, ref oAppointment); //B11796_OL2013CrmPluginScopeOfBusRels MGY 2015.11.13 Begin if (oAppointment.Body == null) { oAppointment.Body = " "; } oAppointment.Body = custDataInfo(oAppointment.Body, item); //B11796_OL2013CrmPluginScopeOfBusRels MGY 2015.11.13 End Outlook.UserProperty prop = oAppointment.UserProperties.Add("ActivityNum", Outlook.OlUserPropertyType.olText, false, Outlook.OlFormatText.olFormatTextText); prop.Value = item.ACTIVITYNUMBER; oAppointment.Save(); swAppFill.Stop(); } if (log) { serviceProxy.WriteInfo("++++++WebService: Appointment saved."); serviceProxy.WriteInfo("++++++WebService: oAppointment Entry ID: " + oAppointment.EntryID); serviceProxy.WriteInfo("++++++WebService: CalFolder Store ID:" + calFolder.StoreID); printLogMsg("Activity saved in Outlook: " + item.ACTIVITYNUMBER); } if (isUpdateOutlookEntryID) { serviceProxy.UpdateActivityOutlookEntryId(item.ACTIVITYNUMBER, company, oAppointment.EntryID); } if (insert) { insertedOutlookItemCounter++; } else { savedOutlookItemCounter++; } } } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); } } swCheckApp2.Stop(); if (!noAddList) { if (oAppointment.EntryID != null) { if (!smmActivityList.ContainsKey(oAppointment.EntryID)) { smmActivityList.Add(oAppointment.EntryID, item.ACTIVITYNUMBER); } } } progressFromCrm.PerformStep(); swAct.Stop(); /*printLogMsg(String.Format("GetAppointment: {0, 4}; CheckApp: {1,4}; isDiff: {2,4}; AppFill: {3,4}; Activity process time: {4}", * swGetApp.ElapsedMilliseconds, * swCheckApp.ElapsedMilliseconds, * swIsDiff.ElapsedMilliseconds, * swAppFill.ElapsedMilliseconds, * swAct.ElapsedMilliseconds));*/ } //end foreach } catch (InvalidOperationException ex) { printLogMsg(string.Format("Error transferring activities to Outlook! Error message: {0}", ex.Message)); } catch (Exception ex) { printLogMsg(string.Format("Error transferring activities to Outlook! Error message: {0}", ex.Message)); } String message = String.Empty; //if (updatedCRMItemCounter != 0) { message = message + "Number of activities updated in CRM: " + updatedCRMItemCounter + "\n"; } //if (deletedCRMItemCounter != 0) { message = message + "Number of activities deleted from CRM: " + deletedCRMItemCounter + "\n"; } //if (insertedOutlookItemCounter != 0) { message = message + "Number of activities transferred to Outlook: " + insertedOutlookItemCounter + "\n"; } //if (savedOutlookItemCounter != 0) { message = message + "Number of activities updated in Outlook: " + savedOutlookItemCounter; } return(message); }