/// <summary>
/// Clears the calender rules.
/// </summary>
/// <param name="service">The service.</param>
/// <param name="bookableResourceId">The bookable resource identifier.</param>
/// <param name="startDate">The start date.</param>
/// <param name="endDate">The end date.</param>
public static void ClearCalenderRules(IOrganizationService service, Guid bookableResourceId, DateTime startDate, DateTime endDate)
{
using (var context = new CrmServiceContext(service))
{
var bookableResource = context.BookableResourceSet.Where(b => b.Id == bookableResourceId).FirstOrDefault();
if (bookableResource?.CalendarId != null)
{
Entity entity = service.Retrieve("calendar", bookableResource.CalendarId.Id, new ColumnSet(true));
EntityCollection entityCollection = (EntityCollection)entity.Attributes["calendarrules"];
int num = 0;
List<int> list = new List<int>();
foreach (Entity current in entityCollection.Entities)
{
DateTime dateTime2 = Convert.ToDateTime(current["starttime"]);
if (dateTime2 >= startDate && dateTime2 <= endDate)
{
list.Add(num);
}
num++;
}
list.Sort();
list.Reverse();
for (int i = 0; i < list.Count; i++)
{
entityCollection.Entities.Remove(entityCollection.Entities[list[i]]);
}
entity.Attributes["calendarrules"] = entityCollection;
service.Update(entity);
}
}
}
Monday, February 27, 2017
Deleting Calendar Rules in Microsoft Dynamics CRM 2016
How to remove all NewLines from a variable in SQL Server
Declare @A NVarChar(500);
Set @A = N' 12345
25487
154814 ';
Set @A = Replace(@A,CHAR(13)+CHAR(10),' ');
Print @A;
Creating Calendar Rules in Microsoft Dynamics CRM 2016
/// <summary>
/// Creates the calender.
/// </summary>
/// <param name="service">The service.</param>
/// <param name="bookableResourceId">The bookable resource identifier.</param>
/// <param name="startTime">The start time.</param>
/// <param name="durationInMinutes">The duration in minutes.</param>
private static void CreateCalender(IOrganizationService service, Guid bookableResourceId, DateTime startTime, int durationInMinutes)
{
using (var context = new CrmServiceContext(service))
{
var bookableResource = context.BookableResourceSet.Where(b => b.Id == bookableResourceId).FirstOrDefault();
// Get the user calendar
var calendar = context.CalendarSet.First(r => r.Id == bookableResource.CalendarId.Id);
// Retrieve the calendar of the user
Entity userCalendarEntity = service.Retrieve("calendar", calendar.Id, new ColumnSet(true));
// Retrieve the calendar rules defined in the calendar
EntityCollection calendarRules = (EntityCollection)userCalendarEntity.Attributes["calendarrules"];
// Create a new inner calendar
Entity newInnerCalendar = new Entity("calendar");
newInnerCalendar.Attributes["businessunitid"] = new EntityReference("businessunit", ((Microsoft.Xrm.Sdk.EntityReference)(userCalendarEntity["businessunitid"])).Id);
Guid innerCalendarId = service.Create(newInnerCalendar);
// Create a new calendar rule and assign the inner calendar id to it
Entity calendarRule = new Entity("calendarrule");
//calendarRule.Attributes["duration"] = durationInMinutes;
calendarRule.Attributes["duration"] = 1440; // 24hrs in minutes
//It specifies the extent of the Calendar rule,generally an Integer value.
calendarRule.Attributes["effort"] = 1.0;
calendarRule.Attributes["extentcode"] = 1;
calendarRule.Attributes["pattern"] = "FREQ=DAILY;COUNT=1";
//Rank is an Integer value which specifies the Rank value of the Calendar rule
calendarRule.Attributes["rank"] = 0;
// Timezone code to be set which the calendar rule will follow
calendarRule.Attributes["timezonecode"] = bookableResource.TimeZone;
//Specifying the InnerCalendar Id
calendarRule.Attributes["innercalendarid"] = new EntityReference("calendar", innerCalendarId);
//Start time for the created Calendar rule
calendarRule.Attributes["starttime"] = startTime.Date;
calendarRules.Entities.Add(calendarRule);
// assign all the calendar rule back to the user calendar
userCalendarEntity.Attributes["calendarrules"] = calendarRules;
// update the user calendar entity that has the new rule
service.Update(userCalendarEntity);
// Calendar rule for Working Day
Entity workingHourcalendarRule = new Entity("calendarrule");
workingHourcalendarRule.Attributes["duration"] = durationInMinutes;
//Effort available for a resource (User) during the time described by the calendar rule i.e. Capacity part in the Calendar rule
workingHourcalendarRule.Attributes["effort"] = 1.0;
// It is a Flag used in vary-by-day calendar rules.
workingHourcalendarRule.Attributes["issimple"] = true;
workingHourcalendarRule.Attributes["offset"] = startTime.Hour * 60 + startTime.Minute; //to indicate start time
//Rank of the Calendar Rule
workingHourcalendarRule.Attributes["rank"] = 0;
//Sub Type of the Calendar rule.For setting Work hours it is 1.
workingHourcalendarRule.Attributes["subcode"] = 1;
//Type of calendar rule such as working hours, break, holiday, or time off. 0 for working hours
workingHourcalendarRule.Attributes["timecode"] = 0;
// Local time zone for the calendar rule.
workingHourcalendarRule.Attributes["timezonecode"] = -1;
//Specifying the InnerCalendar Id
workingHourcalendarRule.Attributes["calendarid"] = new EntityReference("calendar", innerCalendarId);
EntityCollection innerCalendarRules = new EntityCollection();
innerCalendarRules.EntityName = "calendarrule";
innerCalendarRules.Entities.Add(workingHourcalendarRule);
newInnerCalendar.Attributes["calendarrules"] = innerCalendarRules;
newInnerCalendar.Attributes["calendarid"] = innerCalendarId;
service.Update(newInnerCalendar);
}
}
Subscribe to:
Posts (Atom)