Thursday, 2 June 2016

How to read Organisation Business Closure in MS CRM Using C#

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 


Wednesday, 1 June 2016

How to read Resource Calendar in MS CRM using C#

How to read Resource (System User/ Facility-Equipment ) Calendar in MS CRM using C#

How to check Resource availability in MS CRM using C#


Many times one needs to schedule some tasks/functionalities according to the resource work hours in calendar. 

For Ex: Send some Notification Emails to Resources only in working hours or assigning some activities to resource (User/Facility-Equipment) with-in working hours .In such case we needs to read user's calendar to get the user availability at that time.

Below is the code snippet to get the CRM Resource availability in calendar at current time.


Function Parameters   : System User/ Facility-Equipment Entity
Return Type                 :  Boolean (True if available at current time otherwise false)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public static Boolean IsUser_ResourceAvailable(Entity User_Resource)
        {
            Guid currentUserId =null;
            if (User_Resource.LogicalName == "equipment")
            currentUserId = User_Resource.GetAttributeValue<Guid>("equipmentid");  
            else if (User_Resource.LogicalName == "systemuser")
            currentUserId = user.GetAttributeValue<Guid>("systemuserid");  

            QueryScheduleRequest scheduleRequest = new QueryScheduleRequest();
            scheduleRequest.ResourceId = currentUserId;
            scheduleRequest.Start = Convert.ToDateTime(DateTime.Now);
            scheduleRequest.End = Convert.ToDateTime(DateTime.Now);
            scheduleRequest.TimeCodes = new TimeCode[] { TimeCode.Available };
            QueryScheduleResponse scheduleResponse = (QueryScheduleResponse)service.Execute(scheduleRequest);
            if (scheduleResponse.TimeInfos.Length > 0)  // AVAILABLE RESOURCE
            {
                return true; //Return true if available
            }
            return false;
        }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
You can modify the code according to the requirement if you wants to check the availability of Resource at some future time in Calendar.

Hope this would be helpful.

Comments are highly appreciated.

Happy CRMing,,,!