// Step 1: Create Connection to CRM
string discoveryURL = "Discovery Service url";
string OrganisationURL = "Organisation service url";
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = "your uid";
credentials.UserName.Password = "Your pwd";
DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(new Uri(discoveryURL), null, credentials, null);
dsp.Authenticate();
OrganizationServiceProxy org1 = new OrganizationServiceProxy(new Uri(OrganisationURL), null, credentials, null);
// Step 2: Find the GUID of Opportunity
Guid oportunityid = new Guid();
var oportunityByPage = from oportunity in orgContext.CreateQuery("opportunity")
where ((string)oportunity["name"]) == "MyOpprtunity"
select oportunity;
oportunityid = (oportunityByPage as System.Collections.Generic.IEnumerable<Microsoft.Xrm.Sdk.Entity>).SingleOrDefault().Id;
// Step 3: Create the Task
Entity entityTask = new Entity();
entityTask.LogicalName = "task";
entityTask["subject"] = "Test Task " + DateTime.Now.ToString();
entityTask["regardingobjectid"] = new EntityReference("opportunity", oportunityid);
Guid guid = org1.Create(entityTask);
string discoveryURL = "Discovery Service url";
string OrganisationURL = "Organisation service url";
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = "your uid";
credentials.UserName.Password = "Your pwd";
DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(new Uri(discoveryURL), null, credentials, null);
dsp.Authenticate();
OrganizationServiceProxy org1 = new OrganizationServiceProxy(new Uri(OrganisationURL), null, credentials, null);
// Step 2: Find the GUID of Opportunity
Guid oportunityid = new Guid();
var oportunityByPage = from oportunity in orgContext.CreateQuery("opportunity")
where ((string)oportunity["name"]) == "MyOpprtunity"
select oportunity;
oportunityid = (oportunityByPage as System.Collections.Generic.IEnumerable<Microsoft.Xrm.Sdk.Entity>).SingleOrDefault().Id;
// Step 3: Create the Task
Entity entityTask = new Entity();
entityTask.LogicalName = "task";
entityTask["subject"] = "Test Task " + DateTime.Now.ToString();
entityTask["regardingobjectid"] = new EntityReference("opportunity", oportunityid);
Guid guid = org1.Create(entityTask);