Hi there Ladies and Gentlemen,
I have a requirement to do the following( I am using .net 4.5, online crm-2015)
1. A particular Equipment and Facility resource may not be available for scheduling on a given day(s)
2. It is to be done through code via C# in a plugin or a simple web application
3. User will choose the Equipment/Facility, the day(s) for the time off.
4. On the chosen day that resource should not be available for any Service activity(via service calendar in CRM UI).
I have written this code for the time off.
Code is copied from internet
nishantrana.me/.../programmatically-set-time-off-for-the-system-user-in-crm-2011
// svc is the IOrgService already instantiated
//Guid facilityID =this is the id for the facility
//Get the calendar id of the facility
Entity facilityEntity = svc.Retrieve(CrmEarlyBound.Equipment.EntityLogicalName, facilityID, new ColumnSet(new String[] { "calendarid" }));
// Retrieve the calendar of the facility(whole entity)
Entity facilityCalendarEntity = svc.Retrieve("calendar", ((Microsoft.Xrm.Sdk.EntityReference)(facilityEntity.Attributes["calendarid"])).Id
, new ColumnSet(true));
// Retrieve the calendar rules defined in the calendar
EntityCollection calendarRules = (EntityCollection)facilityCalendarEntity.Attributes["calendarrules"];
//Create a new inner calendar
CrmEarlyBound.Calendar newInnerCalendar = new CrmEarlyBound.Calendar();
newInnerCalendar.Type = new OptionSetValue(-1);
newInnerCalendar.Attributes["businessunitid"] = new EntityReference("businessunit", ((Microsoft.Xrm.Sdk.EntityReference)(userCalendarEntity["businessunitid"])).Id);
newInnerCalendar.Description = "my description";
newInnerCalendar.Name = "cal1";
Guid innerCalendarId = svc.Create(newInnerCalendar);
CrmEarlyBound.Calendar newInnerCalendar = new CrmEarlyBound.Calendar();
newInnerCalendar.Attributes["businessunitid"] = new EntityReference("businessunit", ((Microsoft.Xrm.Sdk.EntityReference)(facilityCalendarEntity["businessunitid"])).Id);
newInnerCalendar.Description = "my description";
newInnerCalendar.Name = "cal1";
Guid innerCalendarId = svc.Create(newInnerCalendar);
// Create a new calendar rule and assign the inner calendar id to it
Entity calendarRule = new Entity("calendarrule");
calendarRule.Attributes["description"] = "Time Off Rule";
calendarRule.Attributes["duration"] = 1440;
calendarRule.Attributes["extentcode"] = 2;
calendarRule.Attributes["pattern"] = "FREQ=DAILY;INTERVAL=1;COUNT=1";
calendarRule.Attributes["rank"] = 0;
calendarRule.Attributes["timezonecode"] = 85;
calendarRule.Attributes["innercalendarid"] = new EntityReference("calendar", innerCalendarId);
// starting at 12:00 on 7 May
calendarRule.Attributes["starttime"] = new DateTime(2016, 5, 10, 0, 0, 0, DateTimeKind.Utc);
calendarRules.Entities.Add(calendarRule);
//assign all the calendar rule back to the user calendar
facilityCalendarEntity.Attributes["calendarrules"] = calendarRules;
//update the user calendar entity that has the new rule
svc.Update(facilityCalendarEntity);
Now the problem's
1. Nothing shows up on the facility calendar(work hours).
2. If you do the same thing through CRM UI, a red mark is shown on the days
off, in my case no indication is there.
3. I am unable to change the working hours after creating this
record for the same facility.
4. Under Setting->Service Mgmt->Holiday Schedule. I can see
the new record, without any name, but when I try to open the same,
it gives a CRM error. You cannot delete this records too
5. Once I delete the Calendar rules(through code), then I can go back to Service Mgmt->Holiday Schedule and delete the records, once that is done, I can change the working hour of the facility too.
Could you guys please tell me what am I doing wrong and how will you write the code for scheduling time off for facility and equipment.
Any resource link would be helpful too.