Set Organizer for appointment or working with ActivityParty in CRM 2011

Hi,
Just posting a sample code for creating appointment record in CRM 2011.

Uri organizationUri = new Uri(<a href="http://CRM2011/orgName/XRMServices/2011/Organization.svc">http://CRM2011/orgName/XRMServices/2011/Organization.svc</a>);
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential("administrator", "password", "contoso");

OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
// Get the IOrganizationService
IOrganizationService orgService = (IOrganizationService)orgProxy;

// create entity record
Entity appointment = new Entity();
appointment.LogicalName = "appointment";
appointment.Attributes["subject"] = "App at " + DateTime.Now.ToString();
appointment.Attributes["scheduledstart"] = new DateTime();
appointment.Attributes["scheduledstart"] = DateTime.Now.AddDays(1);
appointment.Attributes["scheduledend"] = new DateTime();
appointment.Attributes["scheduledend"] = DateTime.Now.AddDays(1).AddHours(2);

// create activity party record
Entity activityParty = new Entity();
activityParty.LogicalName = "activityparty";
activityParty.Attributes["partyid"]=new EntityReference("systemuser",new Guid("userGuid"));
EntityCollection colAP = new EntityCollection();
colAP.Entities.Add(activityParty);

appointment.Attributes["organizer"] = new EntityCollection();
appointment.Attributes["organizer"] = colAP;

// If we use the bookrequest class it will fail
// and gives ResourceBusy error in case of scheduling conflict
// so simply create the appointment without using book request
// to make it appear in the calendar

//   BookRequest bookRequest = new BookRequest();
//   bookRequest.Target = appointment;

try
{
//  BookResponse booked = (BookResponse)orgService.Execute(bookRequest);
string appGuid = orgService.Create(appointment).ToString();
}
catch (Exception ex)
{
string exMessage = ex.Message;
}

Hope it helps !

One thought on “Set Organizer for appointment or working with ActivityParty in CRM 2011

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s