CRUD Dynamic CRM 2011 |
Config:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="CRMUrlOrg" value="http://192.168.21.28:5555/yourorganization/XRMServices/2011/Organization.svc" /> <add key="CRMOrganization" value="yourorganization" /> <add key="CRMDomain" value="yourdomain" /> <add key="CRMUsername" value="yourusername" /> <add key="CRMPassword" value="yourpassword" /> </appSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
1. Create Operation
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query; using System.Configuration; using System.ServiceModel.Description; namespace DemoCRM { class Program { static void Main(string[] args) { //create service crm Uri urlcrm = new Uri(ConfigurationManager.AppSettings["CRMUrlOrg"]); string username = ConfigurationManager.AppSettings["CRMUsername"]; string password = ConfigurationManager.AppSettings["CRMPassword"]; ClientCredentials credentials = new ClientCredentials(); credentials.Windows.ClientCredential.UserName = username; credentials.Windows.ClientCredential.Password = password; OrganizationServiceProxy serviceproxy = new OrganizationServiceProxy(urlcrm, null, credentials, null); IOrganizationService service; service = (IOrganizationService)serviceproxy; //First tutorial CRUD Crm 2011 // 1. Create // insert into entity contact Entity _contact = new Entity("contact"); _contact.Attributes["firstname"] = "Jhon"; _contact.Attributes["lastname"] = "Corner"; _contact.Attributes["emailaddress1"] = "jhon@mial.com"; Guid contactID = service.Create(_contact); Console.WriteLine("INSERT CONTACT SUCCESS WITH THE GUID : " + contactID); Console.ReadKey(); //finish //lets run the program } } }
2. Read Operation
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query; using System.Configuration; using System.ServiceModel.Description; namespace DemoCRM { class Program { static void Main(string[] args) { //create service crm Uri urlcrm = new Uri(ConfigurationManager.AppSettings["CRMUrlOrg"]); string username = ConfigurationManager.AppSettings["CRMUsername"]; string password = ConfigurationManager.AppSettings["CRMPassword"]; ClientCredentials credentials = new ClientCredentials(); credentials.Windows.ClientCredential.UserName = username; credentials.Windows.ClientCredential.Password = password; OrganizationServiceProxy serviceproxy = new OrganizationServiceProxy(urlcrm, null, credentials, null); IOrganizationService service; service = (IOrganizationService)serviceproxy; // read from entity contact QueryByAttribute q_readcontact = new QueryByAttribute("contact"); q_readcontact.ColumnSet = new ColumnSet(new string[] { "firstname", "lastname", "emailaddress1" }); q_readcontact.Attributes.Add("firstname"); q_readcontact.Values.Add("jhon"); //similar to //select firstname, lastname, emailaddress1 from contact where emailaddress1='jhon@mial.com' EntityCollection read_contacts = service.RetrieveMultiple(q_readcontact); // it return collection of entity // so, we use looping entity if (read_contacts.Entities.Count > 0) { foreach (Entity _contact in read_contacts.Entities) { string fname = _contact.Attributes["firstname"].ToString(); string lname = _contact.Attributes["lastname"].ToString(); string email = _contact.Attributes["emailaddress1"].ToString(); Console.WriteLine("read from entity contact :"); Console.WriteLine("first name : " + fname); Console.WriteLine("last name : " + lname); Console.WriteLine("email : " + email); Console.WriteLine("-------------------------------------------"); Console.WriteLine("press enter to continue"); Console.ReadKey(); //lets run the program } } } } }
3.Update Operation
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query; using System.Configuration; using System.ServiceModel.Description; namespace DemoCRM { class Program { static void Main(string[] args) { //create service crm Uri urlcrm = new Uri(ConfigurationManager.AppSettings["CRMUrlOrg"]); string username = ConfigurationManager.AppSettings["CRMUsername"]; string password = ConfigurationManager.AppSettings["CRMPassword"]; ClientCredentials credentials = new ClientCredentials(); credentials.Windows.ClientCredential.UserName = username; credentials.Windows.ClientCredential.Password = password; OrganizationServiceProxy serviceproxy = new OrganizationServiceProxy(urlcrm, null, credentials, null); IOrganizationService service; service = (IOrganizationService)serviceproxy; //update entity crm //first, define which entity to update //update contact with email address='jhon@mial.com'; QueryByAttribute q_updatecontact = new QueryByAttribute("contact"); q_updatecontact.ColumnSet = new ColumnSet(new string[] { "firstname", "lastname", "emailaddress1" }); q_updatecontact.Attributes.Add("emailaddress1"); q_updatecontact.Values.Add("jhon@mial.com"); EntityCollection _updatecontacts = service.RetrieveMultiple(q_updatecontact); if (_updatecontacts.Entities.Count > 0) { foreach (Entity _updatecontact in _updatecontacts.Entities) { _updatecontact.Attributes["firstname"] = "Indiana"; _updatecontact.Attributes["lastname"] = "Jhones"; service.Update(_updatecontact); Console.WriteLine("Update contact success!"); Console.ReadKey(); //it similar to UPDATE contact SET firstname='Indiana', lastname='Jhones' WHERE emailaddress1='jhon@mial.com' //lets run the program } } } } }
4. Delete Operation
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Query; using System.Configuration; using System.ServiceModel.Description; namespace DemoCRM { class Program { static void Main(string[] args) { //create service crm Uri urlcrm = new Uri(ConfigurationManager.AppSettings["CRMUrlOrg"]); string username = ConfigurationManager.AppSettings["CRMUsername"]; string password = ConfigurationManager.AppSettings["CRMPassword"]; ClientCredentials credentials = new ClientCredentials(); credentials.Windows.ClientCredential.UserName = username; credentials.Windows.ClientCredential.Password = password; OrganizationServiceProxy serviceproxy = new OrganizationServiceProxy(urlcrm, null, credentials, null); IOrganizationService service; service = (IOrganizationService)serviceproxy; //DELETE from entity crm QueryByAttribute q_deletecontact = new QueryByAttribute("contact"); q_deletecontact.ColumnSet = new ColumnSet(new string[] { "firstname", "lastname", "emailaddress1" }); q_deletecontact.Attributes.Add("emailaddress1"); q_deletecontact.Values.Add("jhon@mial.com"); EntityCollection deletecontacts = service.RetrieveMultiple(q_deletecontact); if (deletecontacts.Entities.Count > 0) { foreach (Entity _deletecontact in deletecontacts.Entities) { service.Delete(_deletecontact.LogicalName, _deletecontact.Id); Console.WriteLine("delete contact success!"); Console.ReadKey(); //finish //lets run the program } } //thanks for wathing //see you in the next tutorial //happy coding :) } } }