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,,,!

3 comments:

  1. very useful information, the post shared was very nice.
    Thanks for sharing.
    Microsoft Dynamics AX Online Training

    ReplyDelete
  2. But it does not work for contacts

    ReplyDelete
  3. Harrah's Philadelphia Casino & Racetrack - Mapyro
    Harrah's Philadelphia Casino & Racetrack, 서울특별 출장샵 777 Casino Dr, 문경 출장안마 Philadelphia, PA 18702. Directions · (484) 734-5000. Call Now · More Info. Hours, Accepts 인천광역 출장안마 Credit Cards, 양주 출장마사지 Attire, 전라남도 출장샵 Wi-Fi,

    ReplyDelete