/// <summary> /// Retrieve <c>members</c> from <c>List (marketing list)</c>. /// <para> /// Please note that if your <c>list</c> is <c>dynamic</c> it must has "Fullname" (for <c>Contact</c>, <c>Lead</c>) or "Name" (for <c>Account</c>) attribute in its query. /// Otherwise <see cref="ListMemberItemDetail.Name"/> will be <see cref="string.Empty"/>. /// </para> /// </summary> /// <param name="listId">Marketing List Id</param> /// <param name="itemPerPage"> /// Record count per page. If marketling list has more than value, method works in loop. /// It's default value <c>5000</c> and recommended range is between <c>500</c> - <c>5000</c> for better performance. /// </param> /// <returns> /// <see cref="ListMemberResult"/> for data. /// </returns> public ListMemberResult GetMemberList(Guid listId, int itemPerPage = 5000) { ExceptionThrow.IfGuidEmpty(listId, "listId"); ExceptionThrow.IfNegative(itemPerPage, "itemPerPage"); ExceptionThrow.IfEquals(itemPerPage, "itemPerPage", 0); ListMemberResult result = new ListMemberResult(); var list = this.OrganizationService.Retrieve(this.EntityName, listId, new ColumnSet("type", "membertype", "query")); if (list != null) { ListTypeCode listType = (ListTypeCode)Convert.ToInt32(list.GetAttributeValue <bool>("type")); ListMemberTypeCode membertype = (ListMemberTypeCode)list.GetAttributeValue <int>("membertype"); string query = list.GetAttributeValue <string>("query"); switch (listType) { case ListTypeCode.Static: result = PopulateStaticList(listId, membertype, itemPerPage); break; case ListTypeCode.Dynamic: result = PopulateDynamicList(query, membertype, itemPerPage); break; } } return(result); }
/// <summary> /// Add a <c>Contract Detail</c> to <c>Contract</c> with basic data. /// <c>Customer</c>, <c>Start Date</c> and <c>End Date</c> properties will be copied from <c>Contract</c> data. /// </summary> /// <param name="contractId"><c>Contract</c> Id</param> /// <param name="title">Title (description) of contract line</param> /// <param name="totalPrice">Total Price</param> /// <param name="totalAllotments">Total number of <c>minutes</c> or <c>case (incident)</c> allowed for the contract line.</param> /// <returns>Created record Id (<see cref="System.Guid"/>)</returns> public Guid Add(Guid contractId, string title, decimal totalPrice, int totalAllotments) { ExceptionThrow.IfGuidEmpty(contractId, "contractId"); ExceptionThrow.IfNullOrEmpty(title, "title"); ContractHelper contractHelper = new ContractHelper(this.OrganizationService); var contract = contractHelper.Get(contractId, "customerid", "activeon", "expireson"); ExceptionThrow.IfNull(contract, "contract", string.Format("'{0}' not found", contractId)); ExceptionThrow.IfNull(contract.GetAttributeValue <EntityReference>("customerid"), "contract.CustomerId"); ExceptionThrow.IfGuidEmpty(contract.GetAttributeValue <EntityReference>("customerid").Id, "contract.CustomerId"); ExceptionThrow.IfEquals(contract.GetAttributeValue <DateTime>("activeon"), "contract.StartDate", DateTime.MinValue); ExceptionThrow.IfEquals(contract.GetAttributeValue <DateTime>("expireson"), "contract.EndDate", DateTime.MinValue); Entity entity = new Entity(this.EntityName); entity["contractid"] = new EntityReference("contract", contractId); entity["title"] = title; entity["customerid"] = contract.GetAttributeValue <EntityReference>("customerid"); entity["activeon"] = contract.GetAttributeValue <DateTime>("activeon"); entity["expireson"] = contract.GetAttributeValue <DateTime>("expireson"); entity["price"] = new Money(totalPrice); entity["totalallotments"] = totalAllotments; return(this.OrganizationService.Create(entity)); }
/// <summary> /// Appointment <c>Start date</c> /// </summary> /// <param name="date"></param> /// <returns><see cref="XrmAppointment"/></returns> public XrmAppointment ScheduledStart(DateTime date) { ExceptionThrow.IfEquals(date, "ScheduledStart", DateTime.MinValue); _start = date; return(this); }
/// <summary> /// Appointment End date /// </summary> /// <param name="date"></param> /// <returns></returns> public XrmRecurringAppointment EndDate(DateTime date) { ExceptionThrow.IfEquals(date, "EndDate", DateTime.MinValue); _endRangeDate = date; return(this); }
/// <summary> /// Appointment <c>End date</c> /// </summary> /// <param name="date"></param> /// <returns><see cref="XrmAppointment"/></returns> public XrmAppointment ScheduledEnd(DateTime date) { ExceptionThrow.IfEquals(date, "ScheduledEnd", DateTime.MinValue); _end = date; return(this); }
/// <summary> /// Appointment End Time (only time) /// </summary> /// <param name="time">Only Time format.</param> /// <returns></returns> public XrmRecurringAppointment EndTime(DateTime time) { ExceptionThrow.IfEquals(time, "EndTime", DateTime.MinValue); _endTime = time; return(this); }
/// <summary> /// Recurrence with <c>end date</c>. /// </summary> /// <param name="date"></param> /// <returns></returns> public XrmRecurringAppointment EndWithDate(DateTime date) { ExceptionThrow.IfEquals(date, "EndWithDate", DateTime.MinValue); SetEndPattern(); _recurrenceEndPattern = RecurrenceEndPatternType.WithEnddate; _endRangeDate = date; return(this); }
/// <summary> /// Recurrence with <c>occurence(s)</c>. /// </summary> /// <param name="occurence"></param> /// <returns></returns> public XrmRecurringAppointment EndAfterXOccurences(int occurence) { ExceptionThrow.IfNegative(occurence, "occurence"); ExceptionThrow.IfEquals(occurence, "occurence", 0); SetEndPattern(); _recurrenceEndPattern = RecurrenceEndPatternType.Occurrences; _occurence = occurence; return(this); }
/// <summary> /// <c>Daily</c> recurrence with <c>every X days</c>. /// Please note that this includes alldays (weekdays and weekend). /// </summary> /// <param name="interval"><c>every X days</c> interval value.</param> /// <returns></returns> public XrmRecurringAppointment Daily(int interval) { ExceptionThrow.IfEquals(interval, "interval", 0); ExceptionThrow.IfNegative(interval, "interval"); SetRecurrence(); _recurrencePattern = RecurrencePatternType.Daily; _interval = interval; return(this); }
/// <summary> /// <c>Weekly</c> recurrence. /// </summary> /// <param name="interval"></param> /// <param name="daysOfWeek"></param> /// <returns></returns> public XrmRecurringAppointment Weekly(int interval, DayOfWeek daysOfWeek) { ExceptionThrow.IfEquals(interval, "interval", 0); ExceptionThrow.IfNegative(interval, "interval"); SetRecurrence(); _recurrencePattern = RecurrencePatternType.Weekly; _interval = interval; _dayOfWeeksValue = (int)daysOfWeek; return(this); }
/// <summary> /// <c>Monthly</c> recurrence. /// </summary> /// <param name="option"></param> /// <param name="day"></param> /// <param name="interval"></param> /// <returns></returns> public XrmRecurringAppointment Monthly(MonthlyOption option, DayOfWeek day, int interval) { ExceptionThrow.IfEquals(interval, "interval", 0); ExceptionThrow.IfNegative(interval, "interval"); SetRecurrence(); _recurrencePattern = RecurrencePatternType.Monthly; _monthlyOption = option; _interval = interval; _dayOfWeeksValue = (int)day; return(this); }
/// <summary> /// <c>Yearly</c> recurrence with exact date. /// </summary> /// <param name="month"></param> /// <param name="day"></param> /// <param name="interval"><c>Recur every X years</c> interval.</param> /// <returns></returns> public XrmRecurringAppointment Yearly(Month month, int day, int interval) { ExceptionThrow.IfEquals(day, "day", 0); ExceptionThrow.IfNegative(day, "day"); SetRecurrence(); _isFirstOption = true; _recurrencePattern = RecurrencePatternType.Yearly; _interval = interval; _dayNumber = day; _month = month; return(this); }
/// <summary> /// <c>Monthly</c> recurrence with exact day. /// </summary> /// <param name="onDay">Excat day number</param> /// <param name="interval"><c>Every X months</c> interval value.</param> /// <returns></returns> public XrmRecurringAppointment Monthly(int onDay, int interval) { ExceptionThrow.IfEquals(onDay, "onDay", 0); ExceptionThrow.IfNegative(onDay, "onDay"); ExceptionThrow.IfEquals(interval, "interval", 0); ExceptionThrow.IfNegative(interval, "interval"); SetRecurrence(); _isFirstOption = true; _recurrencePattern = RecurrencePatternType.Monthly; _interval = interval; _dayNumber = onDay; return(this); }
/// <summary> /// Distribute <c>CampaignActivity</c> with <c>Appointment</c>. /// </summary> /// <param name="id"><c>CampaignActvity</c> Id</param> /// <param name="subject">Appointment Subject</param> /// <param name="location">Appointment Location</param> /// <param name="description">Appointment Description</param> /// <param name="scheduledStart">Appointment Start date</param> /// <param name="scheduledEnd">Appointment End date</param> /// <param name="isAllDayEvent"> /// Set <c>true</c> if this event for all day /// </param> /// <param name="propagationOwnershipOptions"></param> /// <param name="ownerTypeCode"></param> /// <param name="ownerId"></param> /// <param name="queueId"></param> /// <param name="doesPropagate">Set <c>true</c>, whether the activity is both created and executed. Otherwise set <c>false</c>.</param> /// <param name="useAsync"> /// Set <c>true</c>, if you want use an asynchronous job to distribute activities. Otherwise set <c>false</c>. /// This field's default value is <c>true</c>. /// </param> /// <param name="validateActivity"></param> /// <returns> /// Returns created <c>BulkOperation</c> Id in <see cref="DistributeCampaignActivityResponse.BulkOperationId"/> property. /// </returns> public DistributeCampaignActivityResponse DistributeByAppointment(Guid id, string subject, string location, string description, DateTime scheduledStart, DateTime scheduledEnd, bool isAllDayEvent, PropagationOwnershipOptions propagationOwnershipOptions, PrincipalType ownerTypeCode, Guid ownerId, Guid?queueId, bool doesPropagate = true, bool useAsync = true, bool validateActivity = true) { ExceptionThrow.IfNullOrEmpty(subject, "subject"); ExceptionThrow.IfEquals(scheduledStart, "scheduledStart", DateTime.MinValue); ExceptionThrow.IfEquals(scheduledEnd, "scheduledEnd", DateTime.MinValue); Entity entity = new Entity("appointment"); entity["subject"] = subject; entity["location"] = location; entity["isalldayevent"] = isAllDayEvent; entity["scheduledstart"] = scheduledStart; entity["scheduledend"] = scheduledEnd; entity["description"] = description; return(Distribute(id, entity, propagationOwnershipOptions, ownerTypeCode, ownerId, queueId, null, doesPropagate, useAsync, false, validateActivity)); }
/// <summary> /// Add a <c>Contract Detail</c> to <c>Contract</c> with required data. /// </summary> /// <param name="contractId"><c>Contract</c> Id</param> /// <param name="customerType"><see cref="CustomerType"/></param> /// <param name="customerId">Customer Id</param> /// <param name="title">Title (description) of contract line</param> /// <param name="startDate">Start date</param> /// <param name="endDate">End date</param> /// <param name="totalPrice">Total price</param> /// <param name="totalAllotments">Total number of <c>minutes</c> or <c>case (incident)</c> allowed for the contract line.</param> /// <returns> /// Created record Id (<see cref="System.Guid"/>) /// </returns> public Guid Add(Guid contractId, CustomerType customerType, Guid customerId, string title, DateTime startDate, DateTime endDate, decimal totalPrice, int totalAllotments) { ExceptionThrow.IfGuidEmpty(contractId, "contractId"); ExceptionThrow.IfGuidEmpty(customerId, "customerId"); ExceptionThrow.IfNullOrEmpty(title, "title"); ExceptionThrow.IfEquals(startDate, "startdate", DateTime.MinValue); ExceptionThrow.IfEquals(endDate, "endDate", DateTime.MinValue); Entity entity = new Entity(this.EntityName); entity["contractid"] = new EntityReference("contract", contractId); entity["title"] = title; entity["customerid"] = new EntityReference(customerType.Description(), customerId); entity["activeon"] = startDate; entity["expireson"] = endDate; entity["price"] = new Money(totalPrice); entity["totalallotments"] = totalAllotments; return(this.OrganizationService.Create(entity)); }
/// <summary> /// <c>Reschedule</c> an appointment. /// <para> /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.reschedulerequest(v=crm.7).aspx /// </para> /// </summary> /// <param name="appointmentId"></param> /// <param name="start">Start time</param> /// <param name="end">End time</param> /// <returns> /// <see cref="RescheduleResponse"/> /// </returns> public RescheduleResponse Reschedule(Guid appointmentId, DateTime start, DateTime end) { ExceptionThrow.IfGuidEmpty(appointmentId, "appointmentId"); ExceptionThrow.IfEquals(start, "start", DateTime.MinValue); ExceptionThrow.IfEquals(start, "start", DateTime.MaxValue); ExceptionThrow.IfEquals(end, "end", DateTime.MinValue); ExceptionThrow.IfEquals(end, "end", DateTime.MaxValue); Entity target = new Entity("appointment"); target["activityid"] = appointmentId; target["scheduledstart"] = start; target["scheduledend"] = end; RescheduleRequest request = new RescheduleRequest() { Target = target }; return((RescheduleResponse)this.OrganizationService.Execute(request)); }
/// <summary> /// Change <c>Business Unit</c> of specified <c>System User</c>. /// <para> /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.setbusinesssystemuserrequest(v=crm.8).aspx /// </para> /// </summary> /// <param name="userId"><c>System User</c> Id</param> /// <param name="businessUnitId">New <c>Business Unit</c> id</param> /// <param name="reassignedUserId"> /// Target <c>SystemUser</c> to which the instances of entities previously owned by the user are to be assigned. /// If you do not want to re-assign it, you can set <c>null</c> or <see cref="Guid.Empty"/>. In this case the records will remain on the current owner (specified <c>SystemUser</c> on <paramref name="userId"/>) /// </param> public void ChangeBusinessUnit(Guid userId, Guid businessUnitId, Guid?reassignedUserId = null) { ExceptionThrow.IfGuidEmpty(userId, "userId"); ExceptionThrow.IfGuidEmpty(businessUnitId, "businessUnitId"); Guid principalId = userId; if (reassignedUserId.HasValue) { ExceptionThrow.IfGuidEmpty(reassignedUserId.Value, "reassignedUserId"); ExceptionThrow.IfEquals(reassignedUserId.Value, "reassignedUserId", userId); principalId = reassignedUserId.Value; } SetBusinessSystemUserRequest request = new SetBusinessSystemUserRequest() { BusinessId = businessUnitId, UserId = userId, ReassignPrincipal = new EntityReference(this.EntityName, principalId) }; this.OrganizationService.Execute(request); }
/// <summary> /// Validate <see cref="XrmRecurringAppointment"/> required fields by patterns and throws exception if values not expected. /// </summary> public void Validate() { ExceptionThrow.IfEquals(_startTime, "StartTime", DateTime.MinValue); ExceptionThrow.IfEquals(_endTime, "EndTime", DateTime.MinValue); ExceptionThrow.IfEquals(_startRangeDate, "StartRange", DateTime.MinValue); #region | Validate Recurrence Pattern | switch (_recurrencePattern) { case RecurrencePatternType.Daily: if (_isEveryWeekday) { ExceptionThrow.IfEquals(_dayOfWeeksValue, "Days Of Week", 0); ExceptionThrow.IfNegative(_dayOfWeeksValue, "Days Of Week"); ExceptionThrow.IfNotExpectedValue(_dayOfWeeksValue, "Days Of Week", 62); } else { ExceptionThrow.IfEquals(_interval, "Interval", 0); ExceptionThrow.IfNegative(_interval, "Interval"); } break; case RecurrencePatternType.Weekly: ExceptionThrow.IfEquals(_dayOfWeeksValue, "Days Of Week", 0); ExceptionThrow.IfNegative(_dayOfWeeksValue, "Days Of Week"); ExceptionThrow.IfGreaterThan(_dayOfWeeksValue, "Days Of Week", 127); ExceptionThrow.IfEquals(_interval, "Interval", 0); ExceptionThrow.IfNegative(_interval, "Interval"); break; case RecurrencePatternType.Monthly: ExceptionThrow.IfEquals(_interval, "Interval", 0); ExceptionThrow.IfNegative(_interval, "Interval"); if (_isFirstOption) { ExceptionThrow.IfEquals(_dayNumber, "Day", 0); ExceptionThrow.IfNegative(_dayNumber, "Day"); } else { ExceptionThrow.IfEquals(_dayOfWeeksValue, "Days Of Week", 0); ExceptionThrow.IfNegative(_dayOfWeeksValue, "Days Of Week"); ExceptionThrow.IfGreaterThan(_dayOfWeeksValue, "Days Of Week", 127); } break; case RecurrencePatternType.Yearly: ExceptionThrow.IfEquals(_interval, "Interval", 0); ExceptionThrow.IfNegative(_interval, "Interval"); ExceptionThrow.IfEquals((int)_month, "Month", 0); ExceptionThrow.IfNegative((int)_month, "Month"); if (_isFirstOption) { ExceptionThrow.IfEquals(_dayNumber, "Day", 0); ExceptionThrow.IfNegative(_dayNumber, "Day"); } else { ExceptionThrow.IfEquals(_dayOfWeeksValue, "Days Of Week", 0); ExceptionThrow.IfNegative(_dayOfWeeksValue, "Days Of Week"); ExceptionThrow.IfGreaterThan(_dayOfWeeksValue, "Days Of Week", 127); } break; } #endregion #region | Validate Recurrence End Pattern | switch (_recurrenceEndPattern) { case RecurrenceEndPatternType.NoEndDate: break; case RecurrenceEndPatternType.Occurrences: ExceptionThrow.IfNegative(_occurence, "Occurence"); ExceptionThrow.IfEquals(_occurence, "Occurence", 0); break; case RecurrenceEndPatternType.WithEnddate: ExceptionThrow.IfEquals(_endRangeDate, "EndRange", DateTime.MinValue); break; } #endregion }