| class Program { static CrmService CurrentCrmService { get { CrmService service = new CrmService(); service.Credentials = System.Net.CredentialCache.DefaultCredentials; service.CrmAuthenticationTokenValue = new CrmAuthenticationToken(); service.CrmAuthenticationTokenValue.OrganizationName = "AscentiumCrmDev"; return service; } } static void Main(string[] args) { CrmService service = Program.CurrentCrmService; // Retrieve a "known" piece of equipment - It is assumed this equipment has been created via the UI // When CRM's UI creates a new equipment, it gets a default "All Day" Calendar Rule defined // This sample will override that calendar rule with most specific details equipment testResource = (equipment)service.Retrieve(EntityName.equipment.ToString(), new Guid("905B57D3-60DA-DC11-9EBF-0003FF0E509E"), new AllColumns()); #region Create Level 0 Rule // ====================================================================== // ROOT - The "Level 0" Rule defines the OCCURANCE and the RANGE (start/end time) of the rule // ====================================================================== // Create an "empty" calendar, we will use this later to bind to the Level 1 rule Guid innerCalendarId = Program.CreateEmptyCalendar(); // Create rule to depict 8 am - 5pm schedule calendarrule workingHoursRule = new calendarrule(); // This is a "root" rule and we want it to be the base on which we build workingHoursRule.rank = new CrmNumber(0); // User's timezone and description of the rule workingHoursRule.timezonecode = new CrmNumber(4); // Provide a user readable description workingHoursRule.description = "L0 - Root Rule"; // One full day (1440 minutes) workingHoursRule.duration = new CrmNumber((int)TimeSpan.FromDays(1).TotalMinutes); // Happens "daily, repeating every day" - AKA "all days" workingHoursRule.pattern = "FREQ=DAILY;INTERVAL=1;"; // "Since the first day CRM is aware of" workingHoursRule.starttime = CrmDateTime.FromUniversal(CrmDateTime.MinValue); // The inner "empty" calendar will be used later with a Level 1 Rule workingHoursRule.innercalendarid = new Lookup(EntityName.calendar.ToString(), innerCalendarId); // Get the Root Calendar, so we can update it calendar cal = (calendar)service.Retrieve(EntityName.calendar.ToString(), testResource.calendarid.Value, new AllColumns()); // Append our Rule cal.calendarrules = Program.AddRuleToArray(cal.calendarrules, workingHoursRule); // Update the Calendar so it adds our new rule service.Update(cal); #endregion #region Create Level 1 Rule - To hold the working hours // ================================================================ // LEAF - The "Level 1" Rule defines the SCOPE and DETAILS (availability, etc) of the rule // ================================================================= calendarrule level1Rule = new calendarrule(); level1Rule.issimple = new CrmBoolean(true); // This is a base rule that can be overridden level1Rule.rank = new CrmNumber(0); level1Rule.timezonecode = new CrmNumber(4); // Day starts at midnight or Offset of "0", we need to start at 9AM level1Rule.offset = new CrmNumber((int)TimeSpan.FromHours(9).TotalMinutes); // The person will work for 8 hours so 8 * 60 level1Rule.duration = new CrmNumber((int)TimeSpan.FromHours(8).TotalMinutes); level1Rule.description = "L1 - Daily Working Hours Rule"; // The person is available and able to be scheduled during this time level1Rule.timecode = new CrmNumber((int)TimeCode.Available); level1Rule.subcode = new CrmNumber((int)SubCode.Schedulable); calendar level0Cal = (calendar)service.Retrieve(EntityName.calendar.ToString(), innerCalendarId, new AllColumns()); level0Cal.calendarrules = Program.AddRuleToArray(level0Cal.calendarrules, level1Rule); // Update the Calendar so it adds our new rule service.Update(level0Cal); #endregion #region Create another Level 1 Rule - To Define a "break" in the working hours // ================================================================== // LEAF - The "Level 1" Rule defines the SCOPE and DETAILS (availability, etc) of the rule // =================================================================== calendarrule lunchBreak = new calendarrule(); lunchBreak.issimple = new CrmBoolean(true); // Since the hours for the day are defined, we have to raise the importance of this to override 12-1 lunchBreak.rank = new CrmNumber(1); lunchBreak.timezonecode = new CrmNumber(4); // Break starts at 12 NOON lunchBreak.offset = new CrmNumber((int)TimeSpan.FromHours(12).TotalMinutes); // Break lasts 1 hour lunchBreak.duration = new CrmNumber((int)TimeSpan.FromHours(1).TotalMinutes); lunchBreak.description = "L1 - Lunch Break Rule"; // They are not available during lunch lunchBreak.timecode = new CrmNumber((int)TimeCode.Unavailable); // Because it is a break lunchBreak.subcode = new CrmNumber((int)SubCode.Break); level0Cal = (calendar)service.Retrieve(EntityName.calendar.ToString(), innerCalendarId, new AllColumns()); level0Cal.calendarrules = Program.AddRuleToArray(level0Cal.calendarrules, lunchBreak); // Update the Calendar so it adds our new rule service.Update(level0Cal); #endregion Console.WriteLine("Complete - Press any key to continue..."); Console.ReadLine(); } /// <summary> /// Extends the Calendar Rule Array with one additional item /// </summary> static calendarrule[] AddRuleToArray(calendarrule[] currentRules, calendarrule ruleToAdd) { calendarrule[] newRules = new calendarrule[currentRules.Length + 1]; currentRules.CopyTo(newRules, 0); newRules[newRules.Length - 1] = ruleToAdd; return newRules; } /// <summary> /// Creates a "empty" Calendar and returns its CRM ID /// </summary> static Guid CreateEmptyCalendar() { CrmService service = Program.CurrentCrmService; // Determine the current user WhoAmIResponse currentUser = (WhoAmIResponse)service.Execute(new WhoAmIRequest()); calendar newCal = new calendar(); newCal.name = "Empty Calendar"; newCal.description = "Holdering Calendar"; newCal.businessunitid = new Lookup(); newCal.businessunitid.Value = currentUser.BusinessUnitId; // Create the Calendar Guid id = service.Create(newCal); return id; } } |