使用C#从CRM 2015日历中仅返回假日日期列表

时间:2017-08-22 11:30:00

标签: c# dynamics-crm

我是CRM 2015的新手,我想写一个连接到CRM的C#方法,只返回日历中的假日日期列表:

List<DateTime> lst = GetHolidayListFromCRM();
......
public List<DateTime> GetHolidayListFromCRM()
{
   IOrganizationService service = GetServiceProxy(USERNAME, PASSWORD, DOMAIN, CRM_URL);

   using (OrganizationServiceContext svcContext = new OrganizationServiceContext(service))
   {
       var info = svcContext.CreateQuery("calendar");
       //Some code here for processing and returning only holiday date list
   }
}

1 个答案:

答案 0 :(得分:0)

List<DateTime> ret = new List<DateTime>();

QueryExpression qe = new QueryExpression("calendar");
qe.Criteria.AddCondition("name", ConditionOperator.Equal, "Business Closure Calendar");
EntityCollection ec = service.RetrieveMultiple(qe);

if (ec.Entities.Count != 1) { return ret; }
Entity calendar = ec.Entities[0];
if(!calendar.Contains("calendarrules")) { return ret; }
EntityCollection rules = calendar.GetAttributeValue<EntityCollection>("calendarrules");

foreach (Entity rule in rules.Entities)
{
    ret.Add(rule.GetAttributeValue<DateTime>("startime"));
    // Console.Out.WriteLine("{0}:{1}", rule["starttime"], rule["name"]);
}

return ret;

注意:无法直接访问日历规则:

  1. http://www.inogic.com/blog/2014/08/calendars-and-expand-calendar-request-in-crm-2013-sp-1/
  2. https://community.dynamics.com/crm/f/117/t/185090
  3. https://msdn.microsoft.com/en-us/library/gg327912.aspx?f=255&MSPPError=-2147217396