This is my whole code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
namespace Exercise1
{
public partial class Form1 : Form
{
//Setting Organization Service name
private OrganizationServiceProxy _serviceProxy;
private IOrganizationService _service;
public Guid contactid = new Guid("59E61C6D-3015-E711-80DC-00155D004F8B");
public Guid accountId = new Guid("59E61C6D-3015-E711-80DC-00155D004F8B");
public Form1()
{
InitializeComponent();
}
//----------- Connect to the organization and create the crm service----------------
private IOrganizationService GetService()
{
// URI Of the project
int retries = 3;
OrganizationServiceProxy orgProxy = null;
int reconnectretries = 0;
do
{
try
{
reconnectretries++;
var organizationUri = new Uri(uri);
ClientCredentials credentials = new ClientCredentials();
// Early Bound
// credentials.Windows.ClientCredential = new NetworkCredential("Login", "Password", "Domain");
orgProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null);
}
catch (Exception ex)
{
}
} while (reconnectretries < retries && (orgProxy == null));
return orgProxy;
}
//------------ Start of function: Create New Contact----------------------------------------------------------------------------
private void button9_Click(object sender, EventArgs e)
{
//create new contact
Entity contact = new Entity("contact");
//attributes names and values
contact.Attributes["firstname"] = "Joy";
contact.Attributes["lastname"] = "";
contact.Attributes["fullname"] = "Joy ";
OptionSetValue picklist = new OptionSetValue(3); // the index no of the option you want to set on OptionSet
contact.Attributes.Add("preferredcontactmethodcode", picklist);
OptionSetValue pickcode= new OptionSetValue(2);
contact.Attributes.Add("familystatuscode", pickcode);
contact.Attributes["anniversary"] = new DateTime(2014, 06, 04);
// GetService() function
_service = GetService();
g = _service.Create(contact);
}
//------------ Start of function: Create New account--------------------------------------------------------------------
private void button13_Click(object sender, EventArgs e)
{
//creates a new instance of EntityReference for a specific contact id; lookup primary contact
EntityReference contactId = new EntityReference("contact", new Guid("59E61C6D-3015-E711-80DC-00155D004F8B"));
//create new account
Entity account = new Entity("account");
account.Attributes["name"] = "Test";
account.Attributes["telephone1"] = "";
account.Attributes["fax"] = "";
account.Attributes["numberofemployees"] = 100;
OptionSetValue picklist2 = new OptionSetValue(6); // the index no of the option you want to set on OptionSet
account.Attributes.Add("industrycode", picklist2);
account.Attributes["creditlimit"] = new Money(6000);
account.Attributes["creditonhold"]=true;
//Primary Contact is a lookup field on account;
//entity parent refer contact with the mentioned Guid is the primary contact.
var entity_parentcontact = new EntityReference("account", contactId.Id);
account.Attributes["primarycontactid"] = entity_parentcontact;
// GetService() function
_service = GetService();
// Create
g = _service.Create(account);
}
//------------ Start of function: Deactivate the created account------------------------------------------------------
public static void getDeactivatedbyaccount(string entityName, Guid accountId, IOrganizationService _service)
{
// Attributes I want to Deactivate: statecode, statuscode.
var cols = new ColumnSet(new [] {"statecode", "statuscode"});
//Check if it is Active or not active
Entity account = _service.Retrieve("account", accountId, cols);
try
{
if (account != null && account.GetAttributeValue<OptionSetValue>("statecode").Value == 0)
{
//StateCode = 1 and StatusCode = 2 for deactivating Account or Contact
SetStateRequest setStateRequest = new SetStateRequest()
{
EntityMoniker = new EntityReference
{
Id = accountId,
LogicalName = "account",
},
State = new OptionSetValue(1),
Status = new OptionSetValue(2)
};
_service.Execute(setStateRequest);
}
}
catch (TimeoutException ex)
{
//Exception Code
}
}
//------------ Start of function: Deactivate the created contact-------------------------------------------------------
public static void getDeactivatedbycontact(string entityName, Guid contactid, IOrganizationService _service)
{
// Attributes I want to Deactivate: statecode, statuscode.
var cols = new ColumnSet(new[] { "statecode", "statuscode", "contactid" });
//Check if it is Active or not active
var contact = _service.Retrieve("contact", contactid, cols);
if (contact != null && contact.GetAttributeValue<OptionSetValue>("statecode").Value == 0)
{
//StateCode = 1 and StatusCode = 2 for deactivating Account or Contact
SetStateRequest setStateRequest = new SetStateRequest()
{
EntityMoniker = new EntityReference
{
Id = contactid,
LogicalName = "contact",
},
State = new OptionSetValue(1),
Status = new OptionSetValue(2)
};
_service.Execute(setStateRequest);
}
}
private void button10_Click(object sender, EventArgs e)
{
// Calling getDeactivatedbyaccount function to deactivate the record
getDeactivatedbyaccount("account",accountId , _service);
}
private void button11_Click(object sender, EventArgs e)
{
// Calling getDeactivatedbycontact function to deactivate the record
getDeactivatedbycontact("contact", contactid, _service);
}
}
}