Monday, 24 December 2012

Code to validate an appointment in CRM 2011


In this article , I am going to explain how to Validate an appointment

For Better Understanding I divided this article in two parts
(i)      Retrieve the appointment to be validated
(ii)     Use the Validate message

Namespace need to include 
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Crm.Sdk.Messages;


Retrieve an appointment
Guid _appointmentId = new Guid("0a4252a0-7e70-11d0-a5d6-28db04c10000");

ColumnSet cols = new ColumnSet("scheduledstart", "scheduledend", "statecode", "statuscode");
                    Appointment retrievedAppointment = (Appointment)_serviceProxy.Retrieve(Appointment.EntityLogicalName,
                                                               _appointmentId, cols);


Use the validate message
ValidateRequest validatedReq = new ValidateRequest();
validatedReq.Activities = new EntityCollection();
validatedReq.Activities.Entities.Add(retrievedAppointment);
validatedReq.Activities.MoreRecords = false;
validatedReq.Activities.PagingCookie = "";
validatedReq.Activities.EntityName = Appointment.EntityLogicalName;
ValidateResponse validateResp = (ValidateResponse)_serviceProxy.Execute(validatedReq);



Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
                                                    serverConfig.HomeRealmUri,
                                                    serverConfig.Credentials,
                                                    serverConfig.DeviceCredentials))
{
    // This statement is required to enable early-bound type support.
    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

           //Retrieve the appointment to be validated

            Guid _appointmentId = new Guid("0a4252a0-7e70-11d0-a5d6-28db04c10000");
            ColumnSet cols = new ColumnSet("scheduledstart", "scheduledend", "statecode", "statuscode");
            Appointment retrievedAppointment =          (Appointment)_serviceProxy.Retrieve(Appointment.EntityLogicalName,
                                                       _appointmentId, cols);
         
            //Use the Validate message

            ValidateRequest validatedReq = new ValidateRequest();
            validatedReq.Activities = new EntityCollection();
            validatedReq.Activities.Entities.Add(retrievedAppointment);
            validatedReq.Activities.MoreRecords = false;
            validatedReq.Activities.PagingCookie = "";
            validatedReq.Activities.EntityName = Appointment.EntityLogicalName;
            ValidateResponse validateResp = (ValidateResponse)_serviceProxy.Execute(validatedReq);


}