我希望执行以下操作(我使用.net 4.5,在线crm-2015)
我已经把这段代码写下来了。代码为copied from the internet。
// 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);
现在出现问题:
设施日历上没有任何内容(工作时间)。
如果您通过CRM用户界面执行相同操作,则会在休息日显示红色标记,在我的情况下没有任何指示。
你能告诉我我做错了什么,你将如何编写用于安排设施和设备休假的代码。
答案 0 :(得分:0)
一些问题。
首先,必须删除下面的代码。这个变量用于创建不必要的记录。
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);
其次,您必须创建root/leaf calendar rule。
Entity calendarRule1 = new Entity("calendarrule");
// duration of 8 hours
calendarRule1.Attributes["duration"] = 480;
calendarRule1.Attributes["effort"] = 2.0;
calendarRule1.Attributes["issimple"] = true;
calendarRule1.Attributes["offset"] = 0;
calendarRule1.Attributes["rank"] = 0;
// subcode 6= vacation
calendarRule1.Attributes["subcode"] = 6;
// time code 2 = unavailable
calendarRule1.Attributes["timecode"] = 2;
calendarRule1.Attributes["timezonecode"] = -1;
EntityCollection innerCalendarRules = new EntityCollection();
innerCalendarRules.EntityName = "calendarrule";
innerCalendarRules.Entities.Add(calendarRule1);
newInnerCalendar.Attributes["calendarrules"] = innerCalendarRules;
newInnerCalendar.Attributes["calendarid"] = innerCalendarId;
organizationProxy.Update(newInnerCalendar);