我有以下问题:
我需要删除或删除" Time Off Rule"来自属于"设备"的特定日历的日历规则。这需要完成,以便设备可用于在服务日历上安排。
不知何故,我无法理解如何做到这一点。
我能够检索需要删除的日历规则对象,但下一步是我。
请善意地告诉我:
可以通过C#(SDK)
执行类似操作的任何网络资源或代码部分。
以下代码给出了错误
您尝试删除的对象与另一个对象关联 无法删除。
//equip is of Equipment type and is already initialized
CrmEarlyBound.Calendar cal = (CrmEarlyBound.Calendar)svc.Retrieve("calendar", equip.CalendarId.Id, new ColumnSet(true));
Console.WriteLine("Got the user calendar");
List<CalendarRule> calendarRules = cal.CalendarRules.ToList();
Console.WriteLine("Got the calendar rules " + cal.CalendarRules.ToList().Count);
foreach (CalendarRule cr in cal.CalendarRules)
{
if (cr.Description == "Time Off Rule" && cr.StartTime.Value>=DateTime.Now)
{
Console.WriteLine(cr.StartTime);
Calendar calI = (Calendar)svc.Retrieve(cr.InnerCalendarId.LogicalName, cr.InnerCalendarId.Id, new ColumnSet(true));
//svc.Delete(cr.InnerCalendarId.LogicalName, cr.InnerCalendarId.Id);
}
}
答案 0 :(得分:0)
你快到了。必须删除calendarrules
集合&amp;更新calendar
实体。
public static void ClearCalenderRules(IOrganizationService service, Guid bookableResourceId, DateTime startDate, DateTime endDate)
{
using (var context = new CrmServiceContext(service))
{
var bookableResource = context.BookableResourceSet.Where(b => b.Id == bookableResourceId).FirstOrDefault();
if (bookableResource?.CalendarId != null)
{
Entity entity = service.Retrieve("calendar", bookableResource.CalendarId.Id, new ColumnSet(true));
EntityCollection entityCollection = (EntityCollection)entity.Attributes["calendarrules"];
int num = 0;
List<int> list = new List<int>();
foreach (Entity current in entityCollection.Entities)
{
DateTime dateTime2 = Convert.ToDateTime(current["starttime"]);
if (dateTime2 >= startDate && dateTime2 <= endDate)
{
list.Add(num);
}
num++;
}
list.Sort();
list.Reverse();
for (int i = 0; i < list.Count; i++)
{
entityCollection.Entities.Remove(entityCollection.Entities[list[i]]);
}
entity.Attributes["calendarrules"] = entityCollection;
service.Update(entity);
}
}
}