How to read Organisation Business Closure in MS CRM Using C#
Some time in CRM one needs to read Organisation Business Closure in code to know whether any organisation level holiday is defined there or not.
For Ex: In scheduler services which needs to only run on the days business is ON.
Below is the C# code snippet that can be utilise to achieve such functionality:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static Boolean ISTodayOrgHolidayInOrgBusinessClosure(DateTime datetime, IOrganizationService service)
{
Guid orgId = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).OrganizationId;
Entity org = service.Retrieve("organization", orgId, new Microsoft.Xrm.Sdk.Query.ColumnSet("businessclosurecalendarid"));
QueryExpression query = new QueryExpression("calendar");
query.ColumnSet = new ColumnSet(true);
query.Criteria = new FilterExpression();
query.Criteria.AddCondition(new ConditionExpression("calendarid", ConditionOperator.Equal, org["businessclosurecalendarid"].ToString()));
EntityCollection OrgBusinessClosureCalendarCollection = service.RetrieveMultiple(query);
if (OrgBusinessClosureCalendarCollection.Entities.Count> 0)
{
Entity BusinessClosureCalendar = OrgBusinessClosureCalendarCollection.Entities[0];
if (BusinessClosureCalendar != null)
{
foreach (Entity closure in BusinessClosureCalendar.GetAttributeValue<EntityCollection>("calendarrules").Entities)
{
DateTime intervalStart = closure.GetAttributeValue<DateTime>("effectiveintervalstart");
DateTime intervalEnd = closure.GetAttributeValue<DateTime>("effectiveintervalend");
if ((DateTime.Now >= intervalStart) && (datetime <= intervalEnd))
{
return true; //return is holiday
}
}
}
}
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Hope it would be helpful.
Comments are highly appreciated..!!!
Happy CRMing














