Friday 3 February 2012

Making an Appointment Request from a Silverlight Web Resource in MS Dynamics CRM 2011

I’ve been looking at upgrading one of our Apps to MS Dynamics CRM 2011 and one of the things that we do a lot of is appointment booking.

At the moment, we have an iFrame showing an ASP.NET webpage with some buttons that when pressed, make an ajax call to a web method hosted in the ISV folder. Once the appointment is selected, the actual booking of the appointment is handled by a plug-in. I wanted to see whether we could remove the iFrame and use a Silverlight webresource.

I have to say that I found it a bit confusing and it took me quite a few attempts to get the RequestName right, this post by Jamie Miller sent me in the right direction. The search for appointments is started by a user clicking a button.

Note that this code, assumes that you have followed the "Use the SOAP Endpoint for Web Resources with Silverlight" walkthrough from the SDK, web link can be found here.

Here is the method that makes the appointment request:

private void checkButton_Click(object sender, RoutedEventArgs e)
{           
    AppointmentRequest appReq = new AppointmentRequest
    {
        Objectives = new ObservableCollection<ObjectiveRelation>(),
        RequiredResources = new ObservableCollection<RequiredResource>(),
        AppointmentsToIgnore = new ObservableCollection<AppointmentsToIgnore>(),
        Constraints = new ObservableCollection<ConstraintRelation>(),
        Sites = new ObservableCollection<Guid>(),
        Duration = 60,
        Direction = SearchDirection.Forward,
        NumberOfResults = 5,
        ServiceId = new Guid("DD535FD0-F84B-E111-8F2F-00505688095F"),
        SearchWindowStart = DateTime.UtcNow,
        SearchWindowEnd = DateTime.UtcNow.AddDays(7.0),
        AnchorOffset = 300
    };
   
    OrganizationRequest req = new OrganizationRequest() { RequestName = "Search" };
    
    req["AppointmentRequest"] = appReq;

    IOrganizationService service = SilverlightUtility.GetSoapService();

    service.BeginExecute(req, new AsyncCallback(GetAppReqResult), service);
}
As this is Silverlight, an asynchronous callback is needed to actually invoke the request, which is done by the method below.
private void GetAppReqResult(IAsyncResult res)
{
   try
   {
     OrganizationResponse resp = 
                          ((IOrganizationService)res.AsyncState).EndExecute(res);
   
     SearchResults results = (SearchResults)resp["SearchResults"];
   
     this.Dispatcher.BeginInvoke(() => ProcessAppointments(results));        
   
   }
   catch (Exception)
   {
       MessageBox.Show("Error Occurred");
   }
}

Not shown here is the ProcessAppointments method, which updates a DataGrid object in the Silverlight Web Resource.

No comments:

Post a Comment