/// <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);
}
}
Monday, February 27, 2017
Creating Calendar Rules in Microsoft Dynamics CRM 2016
Subscribe to:
Post Comments (Atom)
If your records do not appear through schedule board, please add the type as Inner Calendar type (-1) in calendar record.
ReplyDelete