/// <summary> /// AsNameValues is used in transmission to CRM as well as for comparison, so it should NOT /// access our cache of recipient addresses. /// </summary> /// <returns>A set of name/value pairs suitable for transmitting to CRM.</returns> public override NameValueCollection AsNameValues() { string statusString; string name; switch (this.Status) { case Outlook.OlMeetingStatus.olMeetingCanceled: statusString = "Not Held"; name = this.subject.StartsWith(CancelledPrefix) ? this.subject : $"{CancelledPrefix}: {this.subject}"; break; default: statusString = this.start < DateTime.Now ? "Held" : "Planned"; name = this.subject; break; } NameValueCollection data = new NameValueCollection { RestAPIWrapper.SetNameValuePair("name", name), RestAPIWrapper.SetNameValuePair("description", this.body), RestAPIWrapper.SetNameValuePair("location", this.location), RestAPIWrapper.SetNameValuePair("date_start", string.Format("{0:yyyy-MM-dd HH:mm:ss}", this.start.ToUniversalTime())), RestAPIWrapper.SetNameValuePair("date_end", string.Format("{0:yyyy-MM-dd HH:mm:ss}", this.end.ToUniversalTime())), RestAPIWrapper.SetNameValuePair("duration_minutes", (this.duration % 60).ToString()), RestAPIWrapper.SetNameValuePair("duration_hours", (this.duration / 60).ToString()), RestAPIWrapper.SetNameValuePair("outlook_id", this.globalId), RestAPIWrapper.SetNameValuePair("status", statusString) }; if (CrmId.IsValid(this.organiser)) { data.Add(RestAPIWrapper.SetNameValuePair("assigned_user_id", this.organiser.ToString())); } if (CrmId.IsInvalid(CrmEntryId)) { /* A Guid can be constructed from a 32 digit hex string. The globalId is a * 112 digit hex string. It appears from inspection that the least significant * bytes are those that vary between examples, with the most significant bytes * being invariant in the samples we have to hand. */ CrmEntryId = CrmId.Get(new Guid(this.globalId.Substring(this.globalId.Length - 32))); data.Add(RestAPIWrapper.SetNameValuePair("new_with_id", true)); } data.Add(RestAPIWrapper.SetNameValuePair("id", CrmEntryId.ToString())); return(data); }
public override string Perform() { string result; /* #223: ensure that the state has a crmId that is null or empty. * If not null or empty then this is not a new item: do nothing and exit. */ if (CrmId.IsInvalid(syncState.CrmEntryId)) { if (syncState.TxState == TransmissionState.Queued) { try { CrmId returnedCrmId = this.synchroniser.AddOrUpdateItemFromOutlookToCrm(syncState); result = $"synced new item as {returnedCrmId}.\n\t{syncState.Description}"; } catch (WebException wex) { if (wex.Status == WebExceptionStatus.ProtocolError) { using (HttpWebResponse response = wex.Response as HttpWebResponse) { switch (response.StatusCode) { case HttpStatusCode.RequestTimeout: case HttpStatusCode.ServiceUnavailable: throw new ActionRetryableException($"Temporary error ({response.StatusCode})", wex); default: throw new ActionFailedException($"Permanent error ({response.StatusCode})", wex); } } } else { throw new ActionRetryableException("Temporary network error", wex); } } } else { result = $"State is {syncState.TxState}; not retransmitting"; } } else { result = $"item was already synced as {syncState.CrmEntryId}; aborted.\n{this.syncState.Description}"; } return(result); }
/// <summary> /// Set the CRM id for this item to this value. /// </summary> /// <param name="olItem">The Outlook item under consideration.</param> /// <param name="crmId">The value to set.</param> public static void SetCrmId(this Outlook.AppointmentItem olItem, CrmId crmId) { Outlook.UserProperty property = olItem.UserProperties[SyncStateManager.CrmIdPropertyName]; if (property == null) { property = olItem.UserProperties.Add(SyncStateManager.CrmIdPropertyName, Outlook.OlUserPropertyType.olText); SyncStateManager.Instance.SetByCrmId(crmId, SyncStateManager.Instance.GetOrCreateSyncState(olItem)); } if (CrmId.IsInvalid(crmId)) { property.Delete(); } else { property.Value = crmId.ToString(); } }