Thursday, 11 October 2012

Code to book an appointment in crm 2011


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

For Better Understanding I divided this article in four parts
(i)      Get the current user information
(ii)     Create the ActivityParty instance
(iii)    Create the appointment instance
(iv)    Book request 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;

Get the current user information
WhoAmIRequest userRequest = new WhoAmIRequest();
WhoAmIResponse userResponse = (WhoAmIResponse)_serviceProxy.Execute(userRequest);


Create the ActivityParty instance
ActivityParty party = new ActivityParty
{
    PartyId = new EntityReference(SystemUser.EntityLogicalName, userResponse.UserId)
};


Create the appointment instance
Appointment appointment = new Appointment
{
    Subject = " Appointment 1",
    Description = "Appointment to Book.",
    ScheduledStart = DateTime.Now.AddHours(1),
    ScheduledEnd = DateTime.Now.AddHours(2),
    Location = "Office",
    RequiredAttendees = new ActivityParty[] { party },
    Organizer = new ActivityParty[] { party }
};


Book Request message
BookRequest book = new BookRequest
{
    Target = appointment
};
BookResponse booked = (BookResponse)_serviceProxy.Execute(book);
Guid _appointmentId = booked.ValidationResult.ActivityId;


Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
                                                    serverConfig.HomeRealmUri,
                                                    serverConfig.Credentials,
                                                    serverConfig.DeviceCredentials))
{
    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());


    // Get the current user information
    WhoAmIRequest userRequest = new WhoAmIRequest();
    WhoAmIResponse userResponse = (WhoAmIResponse)_serviceProxy.Execute(userRequest);

    // Create the ActivityParty instance.
    ActivityParty party = new ActivityParty
    {
        PartyId = new EntityReference(SystemUser.EntityLogicalName, userResponse.UserId)
    };


    // Create the appointment instance.
    Appointment appointment = new Appointment
    {
        Subject = " Appointment 1",
        Description = "Appointment to Book.",
        ScheduledStart = DateTime.Now.AddHours(1),
        ScheduledEnd = DateTime.Now.AddHours(2),
        Location = "Office",
        RequiredAttendees = new ActivityParty[] { party },
        Organizer = new ActivityParty[] { party }
    };



    // Use the Book request message.
    BookRequest book = new BookRequest
    {
        Target = appointment
    };
    BookResponse booked = (BookResponse)_serviceProxy.Execute(book);
    Guid _appointmentId = booked.ValidationResult.ActivityId;

}

Wednesday, 10 October 2012

Code to retrieve the schedule of Multiple users in CRM 2011

In this article , I am going to explain how to  retrieve the schedule of Multiple users

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;

Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
                                                    serverConfig.HomeRealmUri,
                                                    serverConfig.Credentials,
                                                    serverConfig.DeviceCredentials))
{
    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

    // Get the current user's information.
    WhoAmIRequest userRequest = new WhoAmIRequest();
    WhoAmIResponse userResponse = (WhoAmIResponse)_serviceProxy.Execute(userRequest);
    Guid _currentUserId = userResponse.UserId;

    // Create another user
    Guid _otherUserId = new Guid("0a4252a0-7e70-11d0-a5d6-28db04c10000");
         

    // Retrieve the schedule of the current and the other user.                                            
    QueryMultipleSchedulesRequest scheduleRequest = new QueryMultipleSchedulesRequest();
    scheduleRequest.ResourceIds = new Guid[2];
    scheduleRequest.ResourceIds[0] = _currentUserId;
    scheduleRequest.ResourceIds[1] = _otherUserId;
    scheduleRequest.Start = DateTime.Now;
    scheduleRequest.End = DateTime.Today.AddDays(7);
    scheduleRequest.TimeCodes = new TimeCode[] { TimeCode.Available };

    QueryMultipleSchedulesResponse scheduleResponse = (QueryMultipleSchedulesResponse)_serviceProxy.Execute(scheduleRequest);
                   
}


Code to retrieve the schedule of a system user in CRM 2011

In this article , I am going to explain how to  retrieve the schedule of a system user

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;

Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
                                                    serverConfig.HomeRealmUri,
                                                    serverConfig.Credentials,
                                                    serverConfig.DeviceCredentials))
{
    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                   
    // Get the current user's information.
    WhoAmIRequest userRequest = new WhoAmIRequest();
    WhoAmIResponse userResponse = (WhoAmIResponse)_serviceProxy.Execute(userRequest);

    // Retrieve the schedule of the current user.                                            
    QueryScheduleRequest scheduleRequest = new QueryScheduleRequest
    {
        ResourceId = userResponse.UserId,
        Start = DateTime.Now,
        End = DateTime.Today.AddDays(7),
        TimeCodes = new TimeCode[] { TimeCode.Available }
    };
    QueryScheduleResponse scheduleResponse = (QueryScheduleResponse)_serviceProxy.Execute(scheduleRequest);
}