通过c#在Dynamics CRM 2013 SP1中设备的日历和CalendarRules

时间:2016-12-08 13:45:53

标签: c# dynamics-crm-2013

我正在开发一种更加动态的方法,为多种资源设置工作时间,而不是开箱即用的Dynamics CRM功能。

不幸的是我在使代码工作时遇到了问题。我用了一些例子让自己走了,可以在这里找到: Example 1 Example 2

 tracingService.Trace("Retrieving Resource Reference");
                var resourceReference = resource.Contains("dp_resource") ? (EntityReference)resource.Attributes["dp_resource"] : null;
                if (resourceReference == null)
                    continue;

                tracingService.Trace("Retrieving Equipment");
                var equipment = service.Retrieve("equipment", (Guid)resourceReference.Id, new ColumnSet(new String[] { "f1_user", "calendarid" }));

                tracingService.Trace("Retrieving User Reference");
                var userReference = equipment.Contains("f1_user") ? (EntityReference)equipment.Attributes["f1_user"] : null;

                tracingService.Trace("Retrieving User");
                var user = service.Retrieve("systemuser", userReference.Id, new ColumnSet(new String[] { "calendarid", "businessunitid" }));

                tracingService.Trace("Retrieving TimeZoneCode");
                var timezone = HelperFunctions.UserSettings.RetrieveTimeZoneCodeByUserId(service, user.Id, new ColumnSet("timezonecode", "localeid"));
                tracingService.Trace("timezone -" + timezone.Value.ToString());

                tracingService.Trace("Creating Empty Calendar");
                var calendar = this.CreateEmptyCalendar(service, user);

                // Create rule to depict 8 am – 5pm  schedule  
                // This is a “root” rule and we want it to be the base on which we build  
                // User’s timezone and description of the rule  
                // Provide a user readable description  
                // One full day (1440 minutes)  
                // “Since the first day CRM is aware of”  

                tracingService.Trace("Creating Calendar Rule");
                var workHourRule = new Entity("calendarrule");
                workHourRule.Attributes.Add("extentcode", 1);
                workHourRule.Attributes.Add("rank", 0);
                workHourRule.Attributes.Add("timezonecode", (int)timezone);
                workHourRule.Attributes.Add("description", "Level 0 – Root Rule");
                workHourRule.Attributes.Add("duration", 510);
                workHourRule.Attributes.Add("pattern", "FREQ=DAILY;INTERVAL=1;COUNT=5");
                workHourRule.Attributes.Add("starttime", new DateTime(2016, 12, 8, 9, 0, 0, DateTimeKind.Utc));
                workHourRule.Attributes.Add("innercalendarid", calendar.ToEntityReference());

                tracingService.Trace("Retrieving Calendar for Equipment");
                var equipmentCalendar = HelperFunctions.Calendar.RetrieveCalendar(service, ((EntityReference)equipment["calendarid"]).Id, new ColumnSet(true));

                tracingService.Trace("Retrieving Calendar Rules from Equipment");
                var calendarRules = equipmentCalendar.Contains("calendarrules") ? (EntityCollection)equipmentCalendar["calendarrules"] : new EntityCollection();

                //tracingService.Trace("Remove old CalendarRules");
                //calendarRules.Entities.Clear();

                tracingService.Trace("Add Calendar Rule to Calendar");
                calendarRules.Entities.Add(workHourRule);

                tracingService.Trace("Set CalendarRules attribute on Equipment Calendar");
                if (equipmentCalendar.Contains("calendarrules"))
                    equipmentCalendar["calendarrules"] = calendarRules;
                else
                    equipmentCalendar.Attributes.Add("calendarrules", calendarRules);

                tracingService.Trace("Update Equipment Calendar");
                service.Update(equipmentCalendar);

我在工作流程中收到以下消息:

Plugin Trace:

[somenamespace.FieldOne: somenamespace.FieldOne.CreateWorkHours]
[somenamespace.FieldOne (1.0.0.3): somenamespace.FieldOne.CreateWorkHours]
Start CreateWorkHours Workflow execution
Retrieving Work Hour Generation
Retrieving StartDate and EndDate of Work Hour Generation
Validating
Retrieving Work Hour Resources
Retrieving Work Hour Items
Retrieving Work Hour Resource
Retrieving Resource Reference
Retrieving Equipment
Retrieving User Reference
Retrieving User
Retrieving TimeZoneCode
timezone -110
Creating Empty Calendar
Creating Calendar Rule
Retrieving Calendar for Equipment
Retrieving Calendar Rules from Equipment
Remove old CalendarRules
Add Calendar Rule to Calendar
Set CalendarRules attribute on Equipment Calendar
Update Equipment Calendar


Error Message:

Unhandled Exception: System.Activities.InvalidWorkflowException: This operation isn’t supported
   at somenamespace.FieldOne.CreateWorkHours.Execute(CodeActivityContext executionContext)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

三天前我也遇到了同样的问题,发现我正在创建内部日历并将此日历设置为主日历规则。

创建日历时,默认日历类型设置为0。内部日历的日历类型应设置为-1,一旦我在内部日历中设置了此值,问题就解决了。