Steps to Achieve:
- Create a console Application in Visual Studio and add (Microsoft.Crm.Sdk.Proxy.dll, Microsoft.Xrm.Sdk.dll) in references section.
- Download this C# Class files to get crm service ,If you have this files just skip this step https://www.dropbox.com/s/pyj6zym6u9ea7wp/deviceidmanager.cs?dl=0 https://www.dropbox.com/s/q82xp39jn0nqi0j/OnlineService.cs?dl=0
- Open OnlineService.cs file and enter your crm credentials as shown below
credentials.UserName.UserName = "Username@Domain.onmicrosoft.com";//Username
credentials.UserName.Password = "yourpasswordhere";//Password
var _servproxy = new OrganizationServiceProxy(new Uri("https://Domain.api.crm5.dynamics.com/XRMServices/2011/Organization.svc"), null, credentials, GetDeviceCredentials());//ServerUrl
Code Snippet:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
namespace SampleCrudOperations
{
class CrudOperations
{
public void CreateRecord()
{
IOrganizationService service = OnlineService.servproxy;//To get CRM Service
if (service != null)
{
Entity myentity = new Entity("new_employee");
myentity.Attributes["new_name"] = "Rahul";
myentity.Attributes["new_ename"] = "Veldandi";
myentity.Attributes["new_employeesalary"] = new Money(23000);//Currency
myentity.Attributes["new_employeelocation"] = new OptionSetValue(100000003);//optionset
myentity.Attributes["new_employeehobbies"] = "TV \n Playing Cricket";//MultiLineText
myentity.Attributes["new_employeegender"] = false;//Two Options myentity.Attributes["new_employeeaccount"] = new EntityReference("account", new Guid("A478321B-E44C-E511-8102-C4346BADA644"));//Lookup
myentity.Attributes["new_department"] = new EntityReference("new_department", new Guid("E07CB8D2-6C50-E511-810E-C4346BAD5740"));//Lookup
myentity.Attributes["createdon"] = new DateTime(2015, 09, 11);//Date Time
service.Create(myentity);
Console.WriteLine("Record Created Successfully");
}
}
public void RetrieveRecord()
{
Entity et = OnlineService.servproxy.Retrieve("new_employee", new Guid("BF18416B-2E58-E511-810E-C4346BAD3768"), new ColumnSet(true));
string name = et["new_name"].ToString();
string hobbies = et["new_employeehobbies"].ToString();
string account = et.GetAttributeValue<EntityReference>("new_employeeaccount").Name;
string gender = et.FormattedValues["new_employeegender"].ToString();
string location = et.FormattedValues["new_employeelocation"].ToString();
string dept = et.GetAttributeValue<EntityReference>("new_department").Name;
decimal sal = et.GetAttributeValue<Money>("new_employeesalary").Value;
Console.WriteLine("employee Name:" + name);
Console.WriteLine("employee hobbies:" + hobbies);
Console.WriteLine("employee account" + account);
Console.WriteLine("employee gender" + gender);
Console.WriteLine("employee location" + location);
Console.WriteLine("employee department" + dept);
Console.WriteLine("employee salary" + sal);
}
public void UpdateRecord()
{
Entity myet = OnlineService.servproxy.Retrieve("new_employee", new Guid("BF18416B-2E58-E511-810E-C4346BAD3768"), new ColumnSet("new_employeesalary", "new_employeelocation", "new_employeeaccount", "new_employeehobbies"));
Console.WriteLine("Enter Employee salary");
decimal sal = Convert.ToDecimal(Console.ReadLine());
myet["new_employeesalary"] = new Money(sal); ;
myet["new_employeelocation"] = new OptionSetValue(100000002);
myet["new_employeeaccount"] = new EntityReference("account", new Guid("9C78321B-E44C-E511-8102-C4346BADA644"));
myet["new_employeehobbies"] = "Watching TV";
OnlineService.servproxy.Update(myet);
Console.WriteLine("Record Updated Successfully");
}
public void delete()
{
OnlineService.servproxy.Delete("new_employee", new Guid("24E6B2EF-7B57-E511-810A-C4346BADB280"));
Console.WriteLine("Record Deleted Sucessfully");
}
public void multiretrive()
{
QueryExpression query = new QueryExpression();
query.EntityName = "new_employee";
query.ColumnSet = new ColumnSet(true);
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;
RetrieveMultipleResponse responce = (RetrieveMultipleResponse)OnlineService.servproxy.Execute(request);
foreach (Entity emp in responce.EntityCollection.Entities)
{
string name = emp["new_name"].ToString();
string hobbies = emp["new_employeehobbies"].ToString();
string account = emp.GetAttributeValue<EntityReference>("new_employeeaccount").Name;
string gender = emp.FormattedValues["new_employeegender"].ToString();
string location = emp.FormattedValues["new_employeelocation"].ToString();
string dept = string.Empty;
if (emp.Attributes.Contains("new_department"))
{
dept = emp.GetAttributeValue<EntityReference>("new_department").Name;
}
decimal sal = emp.GetAttributeValue<Money>("new_employeesalary").Value;
Console.WriteLine("employee Name : " + name);
Console.WriteLine("employee hobbies : " + hobbies);
Console.WriteLine("employee account : " + account);
Console.WriteLine("employee gender : " + gender);
Console.WriteLine("employee location : " + location);
Console.WriteLine("employee department : " + dept);
Console.WriteLine("employee salary : " + sal);
}
}
}
}
Finally Call all the methods in Program.cs file..Like below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
namespace SampleCrudOperations
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Your Choice: C for Create , U for update , D for delete");
char ch =char.Parse(Console.ReadLine());
switch (ch)
{
case 'c':
CrudOperations c = new CrudOperations();
c.create();
break;
case 'u':
CrudOperations u = new CrudOperations();
u.update();
break;
case 'd':
CrudOperations d = new CrudOperations();
d.delete();
break;
default:
Console.WriteLine("Please Choose any one from given choices");
break;
}
Console.ReadLine();
}
}
}
Thank you.......