/// <summary> /// Construct a JSON packet representing this Outlook item, and despatch it to CRM. /// </summary> /// <param name="olItem">The Outlook item.</param> /// <param name="crmType">The type within CRM to which the item should be added.</param> /// <param name="entryId">??</param> /// <returns>The CRM id of the object created or modified.</returns> private string ConstructAndDespatchCrmItem(Outlook.AppointmentItem olItem, string crmType, string entryId) { eNameValue[] data = new eNameValue[8]; DateTime uTCDateTime = new DateTime(); DateTime time2 = new DateTime(); uTCDateTime = olItem.Start.ToUniversalTime(); time2 = olItem.End.ToUniversalTime(); string str = string.Format("{0:yyyy-MM-dd HH:mm:ss}", uTCDateTime); string str2 = string.Format("{0:yyyy-MM-dd HH:mm:ss}", time2); int num = olItem.Duration / 60; int num2 = olItem.Duration % 60; data[0] = clsSuiteCRMHelper.SetNameValuePair("name", olItem.Subject); data[1] = clsSuiteCRMHelper.SetNameValuePair("description", olItem.Body); data[2] = clsSuiteCRMHelper.SetNameValuePair("location", olItem.Location); data[3] = clsSuiteCRMHelper.SetNameValuePair("date_start", str); data[4] = clsSuiteCRMHelper.SetNameValuePair("date_end", str2); data[5] = clsSuiteCRMHelper.SetNameValuePair("duration_minutes", num2.ToString()); data[6] = clsSuiteCRMHelper.SetNameValuePair("duration_hours", num.ToString()); data[7] = String.IsNullOrEmpty(entryId) ? clsSuiteCRMHelper.SetNameValuePair("assigned_user_id", clsSuiteCRMHelper.GetUserId()) : clsSuiteCRMHelper.SetNameValuePair("id", entryId); /* The id of the newly created or modified CRM item */ return(clsSuiteCRMHelper.SetEntryUnsafe(data, crmType)); }
public static string ArchiveEmail(eNameValue[] Data) { try { string strUserID = clsSuiteCRMHelper.GetUserId(); if (strUserID == "") { SuiteCRMUserSession.Login(); } object data = new { @session = SuiteCRMUserSession.id, @module_name = "Emails", @name_value_list = Data }; eSetEntryResult _result = clsGlobals.GetResponse<eSetEntryResult>("set_entry", data); return _result.id.ToString(); } catch (System.Exception exception) { exception.Data.Clear(); return string.Empty; } }
/// <summary> /// Remove the item implied by this sync state from CRM. /// </summary> /// <param name="state">A sync state wrapping an item which has been deleted or marked private in Outlook.</param> protected virtual void RemoveFromCrm(SyncState state) { if (SyncDirection.AllowOutbound(Direction)) { var crmEntryId = state.CrmEntryId; if (state.ExistedInCrm && this.permissionsCache.HasImportAccess(state.CrmType)) { eNameValue[] data = new eNameValue[2]; data[0] = clsSuiteCRMHelper.SetNameValuePair("id", crmEntryId); data[1] = clsSuiteCRMHelper.SetNameValuePair("deleted", "1"); clsSuiteCRMHelper.SetEntryUnsafe(data, state.CrmType); } state.RemoveCrmLink(); } }
protected void RemoveFromCrm(SyncState state) { if (!SyncingEnabled) { return; } var crmEntryId = state.CrmEntryId; if (!string.IsNullOrEmpty(crmEntryId) && this.HasImportAccess(state.CrmType)) { eNameValue[] data = new eNameValue[2]; data[0] = clsSuiteCRMHelper.SetNameValuePair("id", crmEntryId); data[1] = clsSuiteCRMHelper.SetNameValuePair("deleted", "1"); clsSuiteCRMHelper.SetEntryUnsafe(data, state.CrmType); } state.RemoveCrmLink(); }
/// <summary> /// Construct a CRM representation of this mail item, without its attachments if any. /// </summary> /// <param name="mailItem">The mail item.</param> /// <param name="type">?unknown.</param> /// <returns>A CRM representation of the item, as a set of name/value pairs.</returns> private eNameValue[] ConstructCrmItem(Outlook.MailItem mailItem, string type) { eNameValue[] data = new eNameValue[13]; string category = mailItem.UserProperties[CRMCategoryPropertyName] != null ? mailItem.UserProperties[CRMCategoryPropertyName].Value : string.Empty; data[0] = clsSuiteCRMHelper.SetNameValuePair("name", mailItem.Subject ?? string.Empty); data[1] = clsSuiteCRMHelper.SetNameValuePair("date_sent", DateTimeOfMailItem(mailItem, type).ToString(EmailDateFormat)); data[2] = clsSuiteCRMHelper.SetNameValuePair("message_id", mailItem.EntryID); data[3] = clsSuiteCRMHelper.SetNameValuePair("status", "archived"); data[4] = clsSuiteCRMHelper.SetNameValuePair("description", mailItem.Body ?? string.Empty); data[5] = clsSuiteCRMHelper.SetNameValuePair("description_html", mailItem.HTMLBody ?? string.Empty); data[6] = clsSuiteCRMHelper.SetNameValuePair("from_addr", clsGlobals.GetSenderAddress(mailItem, type)); data[7] = clsSuiteCRMHelper.SetNameValuePair("to_addrs", mailItem.To); data[8] = clsSuiteCRMHelper.SetNameValuePair("cc_addrs", mailItem.CC); data[9] = clsSuiteCRMHelper.SetNameValuePair("bcc_addrs", mailItem.BCC); data[10] = clsSuiteCRMHelper.SetNameValuePair("reply_to_addr", mailItem.ReplyRecipientNames); data[11] = clsSuiteCRMHelper.SetNameValuePair("assigned_user_id", clsSuiteCRMHelper.GetUserId()); data[12] = clsSuiteCRMHelper.SetNameValuePair("category_id", category); return(data); }
public ArchiveResult SaveEmailToCrm(Outlook.MailItem mailItem, string type) { try { SaveMailItemIfNecessary(mailItem, type); eNameValue[] data = new eNameValue[12]; data[0] = clsSuiteCRMHelper.SetNameValuePair("name", mailItem.Subject ?? ""); data[1] = clsSuiteCRMHelper.SetNameValuePair("date_sent", DateTimeOfMailItem(mailItem, type).ToString("yyyy-MM-dd HH:mm:ss")); data[2] = clsSuiteCRMHelper.SetNameValuePair("message_id", mailItem.EntryID); data[3] = clsSuiteCRMHelper.SetNameValuePair("status", "archived"); data[4] = clsSuiteCRMHelper.SetNameValuePair("description", mailItem.Body ?? ""); data[5] = clsSuiteCRMHelper.SetNameValuePair("description_html", mailItem.HTMLBody); data[6] = clsSuiteCRMHelper.SetNameValuePair("from_addr", clsGlobals.GetSenderAddress(mailItem, type)); data[7] = clsSuiteCRMHelper.SetNameValuePair("to_addrs", mailItem.To); data[8] = clsSuiteCRMHelper.SetNameValuePair("cc_addrs", mailItem.CC); data[9] = clsSuiteCRMHelper.SetNameValuePair("bcc_addrs", mailItem.BCC); data[10] = clsSuiteCRMHelper.SetNameValuePair("reply_to_addr", mailItem.ReplyRecipientNames); data[11] = clsSuiteCRMHelper.SetNameValuePair("assigned_user_id", clsSuiteCRMHelper.GetUserId()); string crmEmailId; try { crmEmailId = clsSuiteCRMHelper.SetEntry(data, "Emails"); } catch (System.Exception firstFailure) { Log.Warn("1st attempt to upload email failed", firstFailure); data[5] = clsSuiteCRMHelper.SetNameValuePair("description_html", ""); try { crmEmailId = clsSuiteCRMHelper.SetEntry(data, "Emails"); } catch (System.Exception secondFailure) { Log.Warn("2nd attempt to upload email failed", secondFailure); return(ArchiveResult.Failure(new[] { firstFailure, secondFailure })); } } mailItem.Categories = "SuiteCRM"; mailItem.Save(); var warnings = new List <System.Exception>(); if (settings.ArchiveAttachments) { foreach (Outlook.Attachment attachment in mailItem.Attachments) { try { clsSuiteCRMHelper.UploadAttachment( new clsEmailAttachments { DisplayName = attachment.DisplayName, FileContentInBase64String = GetAttachmentBytes(attachment, mailItem) }, crmEmailId); } catch (System.Exception problem) { Log.Warn("Failed to upload email attachment", problem); warnings.Add(problem); } } } return(ArchiveResult.Success(crmEmailId, warnings)); } catch (System.Exception failure) { Log.Warn("Could not upload email to CRM", failure); return(ArchiveResult.Failure(failure)); } }
public string archiveEmail(MailItem itemFromID) { try { string body = string.Empty; string subject = string.Empty; string hTMLBody = itemFromID.HTMLBody; eNameValue[] data = new eNameValue[12]; body = itemFromID.Body; subject = itemFromID.Subject; string type = this.type; if (type == null) { goto Label_017D; } if (!(type == "autoINBOUND")) { if (type == "autoOUTBOUND") { goto Label_0129; } if (type == "SendArchive") { goto Label_0153; } goto Label_017D; } data[1] = clsSuiteCRMHelper.SetNameValuePair("date_sent", itemFromID.SentOn.ToString("yyyy-MM-dd HH:mm:ss")); goto Label_01A5; Label_0129: data[1] = clsSuiteCRMHelper.SetNameValuePair("date_sent", itemFromID.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")); goto Label_01A5; Label_0153: data[1] = clsSuiteCRMHelper.SetNameValuePair("date_sent", itemFromID.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")); goto Label_01A5; Label_017D: data[1] = clsSuiteCRMHelper.SetNameValuePair("date_sent", itemFromID.SentOn.ToString("yyyy-MM-dd HH:mm:ss")); Label_01A5: data[0] = clsSuiteCRMHelper.SetNameValuePair("name", subject); data[2] = clsSuiteCRMHelper.SetNameValuePair("message_id", itemFromID.EntryID); data[3] = clsSuiteCRMHelper.SetNameValuePair("status", "archived"); data[4] = clsSuiteCRMHelper.SetNameValuePair("description", body); data[5] = clsSuiteCRMHelper.SetNameValuePair("description_html", hTMLBody); data[6] = clsSuiteCRMHelper.SetNameValuePair("from_addr", clsGlobals.GetSenderAddress(itemFromID, this.type)); data[7] = clsSuiteCRMHelper.SetNameValuePair("to_addrs", itemFromID.To); data[8] = clsSuiteCRMHelper.SetNameValuePair("cc_addrs", itemFromID.CC); data[9] = clsSuiteCRMHelper.SetNameValuePair("bcc_addrs", itemFromID.BCC); data[10] = clsSuiteCRMHelper.SetNameValuePair("reply_to_addr", itemFromID.ReplyRecipientNames); data[11] = clsSuiteCRMHelper.SetNameValuePair("assigned_user_id", clsSuiteCRMHelper.GetUserId()); string str = clsSuiteCRMHelper.ArchiveEmail(data); if (str.Length < 0x24) { data[7] = clsSuiteCRMHelper.SetNameValuePair("description_html", ""); str = clsSuiteCRMHelper.ArchiveEmail(data); if (str.Length < 0x24) { return("-1"); } } else { } if (settings.ArchiveAttachmentsDefault) { try { if (itemFromID.Attachments.Count > 0) { foreach (Attachment attachment in itemFromID.Attachments) { if (!clsSuiteCRMHelper.UploadAttahcment(new SuiteCRMClient.clsEmailAttachments { DisplayName = attachment.DisplayName, FileContentInBase64String = Globals.ThisAddIn.Base64Encode(attachment, itemFromID) }, str)) { } } } } catch (System.Exception exception) { exception.Data.Clear(); } } return(str); } catch (System.Exception exception2) { exception2.Data.Clear(); return("-1"); } }
/// <summary> /// Add this Outlook item, which may not exist in CRM, to CRM. /// </summary> /// <param name="olItem">The outlook item to add.</param> /// <param name="entryId">The id of this item in CRM, if known (in which case I should be doing /// an update, not an add).</param> private void AddOrUpdateItemFromOutlookToCrm(Outlook.ContactItem oItem, string sID = null) { if (!SyncingEnabled) { return; } if (oItem == null) { return; } try { string _result = String.Empty; eNameValue[] data = new eNameValue[19]; data[0] = clsSuiteCRMHelper.SetNameValuePair("email1", oItem.Email1Address); data[1] = clsSuiteCRMHelper.SetNameValuePair("title", oItem.JobTitle); data[2] = clsSuiteCRMHelper.SetNameValuePair("phone_work", oItem.BusinessTelephoneNumber); data[3] = clsSuiteCRMHelper.SetNameValuePair("phone_home", oItem.HomeTelephoneNumber); data[4] = clsSuiteCRMHelper.SetNameValuePair("phone_mobile", oItem.MobileTelephoneNumber); data[5] = clsSuiteCRMHelper.SetNameValuePair("phone_fax", oItem.BusinessFaxNumber); data[6] = clsSuiteCRMHelper.SetNameValuePair("department", oItem.Department); data[7] = clsSuiteCRMHelper.SetNameValuePair("primary_address_city", oItem.BusinessAddressCity); data[8] = clsSuiteCRMHelper.SetNameValuePair("primary_address_state", oItem.BusinessAddressState); data[9] = clsSuiteCRMHelper.SetNameValuePair("primary_address_postalcode", oItem.BusinessAddressPostalCode); data[10] = clsSuiteCRMHelper.SetNameValuePair("primary_address_country", oItem.BusinessAddressCountry); data[11] = clsSuiteCRMHelper.SetNameValuePair("primary_address_street", oItem.BusinessAddressStreet); data[12] = clsSuiteCRMHelper.SetNameValuePair("description", oItem.Body); data[13] = clsSuiteCRMHelper.SetNameValuePair("last_name", oItem.LastName); data[14] = clsSuiteCRMHelper.SetNameValuePair("first_name", oItem.FirstName); data[15] = clsSuiteCRMHelper.SetNameValuePair("account_name", oItem.CompanyName); data[16] = clsSuiteCRMHelper.SetNameValuePair("salutation", oItem.Title); data[17] = string.IsNullOrEmpty(sID) ? clsSuiteCRMHelper.SetNameValuePair("assigned_user_id", clsSuiteCRMHelper.GetUserId()) : clsSuiteCRMHelper.SetNameValuePair("id", sID); /* If it was created in Outlook and doesn't exist in CRM, (in which case it won't yet have a * magic SShouldSync property) then we need to guarantee changes made in CRM are copied back */ Outlook.UserProperty syncProperty = oItem.UserProperties["SShouldSync"]; string shouldSync = syncProperty == null? Boolean.TrueString.ToLower() : syncProperty.Value; data[18] = clsSuiteCRMHelper.SetNameValuePair("sync_contact", shouldSync); _result = clsSuiteCRMHelper.SetEntryUnsafe(data, "Contacts"); EnsureSynchronisationPropertiesForOutlookItem(oItem, DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"), shouldSync, _result); this.LogItemAction(oItem, "ContactSyncing.AddToCrm, saving"); oItem.Save(); var state = AddOrGetSyncState(oItem); state.OModifiedDate = DateTime.UtcNow; state.CrmEntryId = _result; } catch (Exception ex) { Log.Error("ThisAddIn.AddContactToS", ex); } }
private void AddToCrm(Outlook.TaskItem oItem, string sID = "") { Log.Warn("AddTaskToS"); //if (!settings.SyncCalendar) // return; if (oItem == null) { return; } try { string _result = String.Empty; eNameValue[] data = new eNameValue[7]; string strStatus = String.Empty; string strImportance = String.Empty; switch (oItem.Status) { case Outlook.OlTaskStatus.olTaskNotStarted: strStatus = "Not Started"; break; case Outlook.OlTaskStatus.olTaskInProgress: strStatus = "In Progress"; break; case Outlook.OlTaskStatus.olTaskComplete: strStatus = "Completed"; break; case Outlook.OlTaskStatus.olTaskDeferred: strStatus = "Deferred"; break; } switch (oItem.Importance) { case Outlook.OlImportance.olImportanceLow: strImportance = "Low"; break; case Outlook.OlImportance.olImportanceNormal: strImportance = "Medium"; break; case Outlook.OlImportance.olImportanceHigh: strImportance = "High"; break; } DateTime uTCDateTime = new DateTime(); DateTime time2 = new DateTime(); uTCDateTime = oItem.StartDate.ToUniversalTime(); if (oItem.DueDate != null) { time2 = oItem.DueDate.ToUniversalTime(); } string body = String.Empty; string str, str2; str = str2 = String.Empty; if (oItem.Body != null) { body = oItem.Body.ToString(); var times = this.ParseTimesFromTaskBody(body); if (times != null) { uTCDateTime = uTCDateTime.Add(times[0]); time2 = time2.Add(times[1]); //check max date, date must has value ! if (uTCDateTime.ToUniversalTime().Year < 4000) { str = string.Format("{0:yyyy-MM-dd HH:mm:ss}", uTCDateTime.ToUniversalTime()); } if (time2.ToUniversalTime().Year < 4000) { str2 = string.Format("{0:yyyy-MM-dd HH:mm:ss}", time2.ToUniversalTime()); } } else { str = oItem.StartDate.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss"); str2 = oItem.DueDate.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss"); } } else { str = oItem.StartDate.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss"); str2 = oItem.DueDate.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss"); } //str = "2016-11-10 11:34:01"; //str2 = "2016-11-19 11:34:01"; string description = String.Empty; if (!string.IsNullOrEmpty(body)) { int lastIndex = body.LastIndexOf("#<"); if (lastIndex >= 0) { description = body.Remove(lastIndex); } else { description = body; } } Log.Warn("\tdescription= " + description); data[0] = clsSuiteCRMHelper.SetNameValuePair("name", oItem.Subject); data[1] = clsSuiteCRMHelper.SetNameValuePair("description", description); data[2] = clsSuiteCRMHelper.SetNameValuePair("status", strStatus); data[3] = clsSuiteCRMHelper.SetNameValuePair("date_due", str2); data[4] = clsSuiteCRMHelper.SetNameValuePair("date_start", str); data[5] = clsSuiteCRMHelper.SetNameValuePair("priority", strImportance); if (sID == String.Empty) { data[6] = clsSuiteCRMHelper.SetNameValuePair("assigned_user_id", clsSuiteCRMHelper.GetUserId()); } else { data[6] = clsSuiteCRMHelper.SetNameValuePair("id", sID); } _result = clsSuiteCRMHelper.SetEntryUnsafe(data, "Tasks"); Outlook.UserProperty oProp = oItem.UserProperties["SOModifiedDate"]; if (oProp == null) { oProp = oItem.UserProperties.Add("SOModifiedDate", Outlook.OlUserPropertyType.olText); } oProp.Value = DateTime.UtcNow; Outlook.UserProperty oProp2 = oItem.UserProperties["SEntryID"]; if (oProp2 == null) { oProp2 = oItem.UserProperties.Add("SEntryID", Outlook.OlUserPropertyType.olText); } oProp2.Value = _result; string entryId = oItem.EntryID; oItem.Save(); var sItem = ItemsSyncState.FirstOrDefault(a => a.OutlookItem.EntryID == entryId); if (sItem != null) { sItem.OutlookItem = oItem; sItem.OModifiedDate = DateTime.UtcNow; sItem.CrmEntryId = _result; } else { ItemsSyncState.Add(new TaskSyncState { CrmEntryId = _result, OModifiedDate = DateTime.UtcNow, OutlookItem = oItem }); } Log.Warn("\tdate_start= " + str + ", date_due=" + str2); } catch (Exception ex) { Log.Error("ThisAddIn.AddTaskToS", ex); } }
public static eSetEntryResult SetOpportunitiesEntry(eNameValue[] Data) { string strUserID = clsSuiteCRMHelper.GetUserId(); if (strUserID == "") { SuiteCRMUserSession.Login(); } object data = new { @session = SuiteCRMUserSession.id, @module_name = "Opportunities", @name_value_list = Data }; eSetEntryResult _result = clsGlobals.GetResponse<eSetEntryResult>("set_entry", data); return _result; }