The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
We continue the example in this part II of the article series by showing how we can use the CRM Web Service to create and then update a specific user's calendar information. In this example, the user is passed as a parameter to the Console Application we develop using C# on Visual Studio 2008, and we use his / her assigned calendar as a template since it contains information on the work hours / days and time off to be copied / cloned to another target user that is also passed as a parameter.
This practice will help automate the process of assigning new calendars that are copied from existing ones to newly added users who belong to an existing business unit and have a specific role assigned to them that requires a standard calendar for work days / time etc ..
We ended up in Part I showing a straightforward way of creating a C# Console Application that uses Microsoft Dynamics CRM 4.0 SDK Web Services for Discovery and CRM Main Service to connect, query, and locate a specific user and inspect his /her assigned roles to look for a sales person role as an example role.
Using the Code
The attached code download is a continuation of the Part I Console Application created with Visual Studio 2008 and compiled in Debug build mode. However, since I am using a method (Main method) to contain the bulk code for easy follow-up of the steps of calls to different SDK APIs in this download source C# file, I have used the Visual Studio
#region editor directive to segregate all the code in containers that can be collapsed and expanded as desired be code readers. All the names of regions are descriptive enough to explain the function of the code block, which I hope will allow every one to understand the steps from connecting to the CRM Server Web Services to all API calls until a Calendar is copied from a template user to a target user.
Also, to automate the usage of this Console Application, this Part II code has four parameters, not two as in Part I. The first and second parameters,
args, are used to run the Console Application against a CRM Server that is on-premise and uses Active Directory authentication. The third parameter,
args, is a domain formatted log on name in the format Domain\LogOn Name for the user to be used as a template user to copy calendar information to other users who happen to have the same role and belong to the same business unit (in our example, it is the Salesperson role). The fourth and last parameter,
args, is the target user logon name to have a new calendar cloned from the template user.
The following Visual Studio Editor snapshot shows all the code regions and explains in a step by step approach how to create and then update the target user's calendar for work days / hours that have been read from another user info as a template.
As shown in the above snapshot of all code regions:
- The code starts with processing the four passed-in parameters.
- Connect / Configure CRM 4.0 Discovery and CRM Web Services.
- Check Source / Template User Role.
- Read and copy template user's Calendar. (With extra unit test code.)
- Read and store template User ID.
- Read and store relevant business unit.
- Read and store target user information.
- Create a new Calendar entity for later use.
- Update the newly created Calendar entity with Calendar Rules from the template user.
- Attach the newly created / updated calendar to the target user.
Points of Interest
- It is worth mentioning here that understanding how Microsoft Dynamics CRM 4.0 internally stores Calendars along with their associated Calendar Rules is rather complex, and on the long run, should not be rely on the SQL Server backend schema for CRM, as it is owned by Microsoft, and any changes to it or its data relationships in a manual ad-hoc fashion can be invalidated by the next CRM 4.0 Roll update or version of the product.
- So the best way to deal with calendars and their rules is to basically use the WYSWYG GUI interface to make changes or create rules for user calendars, then use any standard user calendar (could be a dummy user) as a template for new users created who belong to the same business unit and have the same role as this code project demonstrates.
- The CRM Entity SQL Server tables responsible for this mission are CalendarBase and CalendarRuleBase.
- Finally, the following critical pseudo code snippet demonstrates how we can create calendars and then assign calendar rules to them.
Note: We do not directly access the
Update methods on the calendar rules, but we use their calendar collection / array property as follows:
calendar myCalendar = new calendar();
myCalendar.businessunitid = ...;
myCalendar.description = ...;
myCalendar.isshared = new CrmBoolean();
myCalendar.name = "Work Days Hours";
Lookup tmpLookup = new Lookup();
tmpLookup.Value = ...;
tmpLookup.type = EntityName.businessunit.ToString();
myCalendar.organizationid = tmpLookup;
myCalendar.primaryuserid = new UniqueIdentifier();
myCalendar.primaryuserid.Value = targetUserId;
Guid newCalendarGuid = service.Create(myCalendar);
calendar targetCalendar = new calendar();
tmpLookup = new Lookup();
tmpLookup.type = EntityName.calendar.ToString();
tmpLookup.Value = newCalendarGuid;
targetCalendar.calendarrules = new calendarrule[fromCalendar.calendarrules.Length];
for (int i = 0; i < fromCalendar.calendarrules.Length; ++i)
targetCalendar.calendarrules[i] = fromCalendar.calendarrules[i];
targetCalendar.calendarrules[i].calendarruleid = new Key();
targetCalendar.calendarrules[i].calendarruleid.Value = System.Guid.NewGuid();
targetCalendar.calendarrules[i].businessunitid = new UniqueIdentifier();
targetCalendar.calendarrules[i].businessunitid.Value = myBusinessUnit.Value;
targetCalendar.calendarrules[i].calendarid = tmpLookup;
targetCalendar.calendarid = new Key();
targetCalendar.calendarid.Value = new Guid(newCalendarGuid.ToString());
Console.WriteLine("Calender Update Key: " + newCalendarGuid.ToString());
- Released version 1.0 of this article.