Click here to Skip to main content
15,881,424 members
Articles / Desktop Programming / Win32

Microsoft Dynamics CRM 4.0 Create - Update Calendars - Part II

Rate me:
Please Sign up or sign in to vote.
4.25/5 (4 votes)
31 Oct 2009CPOL4 min read 40.9K   584   11   6
Using the Microsoft Dynamics CRM 4.0 Web Services SDK example.

Introduction

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 ..

Background

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[0] and args[1], are used to run the Console Application against a CRM Server that is on-premise and uses Active Directory authentication. The third parameter, args[2], 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[3], 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.

CodeRegions.jpg

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 Create and Update methods on the calendar rules, but we use their calendar collection / array property as follows:

C#
/* Now we create a new Calendar Entity for the target user */
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);

        
/* For data Consistency Update Calendar so that all 
   Calendar rules have the new Calendar Id Reference */
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)
{/* Note: All Calendar rules associated with this Calendar are copied along */

        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());
service.Update(targetCalendar);

History

  • Released version 1.0 of this article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Ayman Soft
Egypt Egypt
With more than 16 years of IT experience, traveled to 6 different countries and worked in many multi-culture environments. Since returning from Sydney To stay in Cairo i have been working for a Microsoft partner in the middle east, focusing on Microsoft Dynamics CRM 4.0 platform, a very interesting business application platform that has state of the art application design and architecture with rich extension tools for developers.
Hope to help some one out there with an idea or a code example .. Smile | :)

Comments and Discussions

 
GeneralMicrosoft Dynamics CRM training in Hyderabad Pin
Arrowsol Training30-Jul-12 22:22
Arrowsol Training30-Jul-12 22:22 
GeneralMicrosoft Dynamics CRM 4.0 + Word Document Attachment with Document that has fields from CRM Pin
ANAND SURENDAR14-Dec-10 17:07
ANAND SURENDAR14-Dec-10 17:07 
GeneralRe: Microsoft Dynamics CRM 4.0 + Word Document Attachment with Document that has fields from CRM Pin
Arrowsol Training16-Jul-12 19:03
Arrowsol Training16-Jul-12 19:03 
GeneralProblem in Copy work hour . Pin
Nitessh Kumar11-Oct-10 1:55
Nitessh Kumar11-Oct-10 1:55 
GeneralRe: Problem in Copy work hour . Pin
Arrowsol Training16-Jul-12 19:05
Arrowsol Training16-Jul-12 19:05 
GeneralCreate Account programmatically using Web Services Pin
elizas3-Feb-10 2:39
elizas3-Feb-10 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.