How To Create An Case Or Incident In Microsoft Dynamics CRM Online 2011 Programatically


Have you ever wanted to create an Incident or Case in Micosoft Dynamics CRM Online programatically?


using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using System.Runtime.Serialization;

using System.Configuration;
namespace My.CRM
    public class MicrosoftCRMOnline
        public void CreateIncident()
                //BEGIN CONNECTION STUFF
                var connection = CrmConnection.Parse("Authentication Type=Passport; Server=; Username=user; Password=xxx; DeviceID=xxx-ws00001; DevicePassword=xxxx");
                var service = new OrganizationService(connection);
                var context = new CrmOrganizationServiceContext(connection);
                IOrganizationService _service = (IOrganizationService)service;

                Entity incident = new Entity();
                incident.LogicalName = "incident";
                incident["title"] = "Test Case Creation";
                incident["description"] = "This is a test incident";

                //Set customerid with an existing contact guid 
                Guid customerid = new Guid("9BA22E13-1149-E211-8BE3-78E3B5107E67");     //the actual contact GUID.

                //Set customerid as contact to field customerid 
                EntityReference CustomerId = new EntityReference("contact", customerid);
                incident["customerid"] = CustomerId;

                //create the incident


                //create a contact and assign the id to the Id above
                Contact newContact = new Contact();
                newContact.Id = customerid;

                Incident newIncident = new Incident();
                newIncident.Title = "Test Created With Proxy";                    //set the title
                newIncident.Description = "This is a test incident";               //set the description
                newIncident.CustomerId = newContact.ToEntityReference(); //set the Customer Id to the Entity Reference of the Contact

                //create the incident
            catch (Exception e)
                throw e;



In order to create the Proxy Class (Early Bound Objects), you need to execute the following command, and add the .cs file to your project:

CrmSvcUtil.exe /url: /out:MicrosoftCRM2011OnlineProxy.cs /username:"username" /password:"xxxxxx"


**The CrmSvcUtil.exe is located in the bin folder of the SDK.

**Note that you use the same user and pass as in the code above, which is the windows live id you created to log into CRM Online.