GetService
using System;
using System.Configuration;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
ConnectionStringSettings settings = new ConnectionStringSettings("ConnString", connectionString);
CrmConnection crmConn = new CrmConnection(settings);
IOrganizationService service = new OrganizationService(crmConn);
Entity entity = service.Retrieve("account", new Guid(""), new ColumnSet(true));
Console.ReadKey();
}
}
}
CreateRequest
Entity account = new Entity("account")
{
Attributes = {
//String primary name
{"name","张三" },
//Yes/No column
{"creditonhold", false },
// DateTime
{"lastusedincampaign", new DateTime(2023, 1, 1) },
// Double
{"address1_latitude",47.642311},
{"address1_longitude",-122.136841},
// Integer
{"numberofemployees", 500 },
// Money
{"revenue",new Money(new decimal(5000000.00))},
// Choice column: Preferred Customer
{"accountcategorycode",new OptionSetValue(1)}
}
};
//Create the account
CreateRequest request = new CreateRequest()
{
Target = account
};
var response = (CreateResponse)service.Execute(request);
Console.WriteLine($"Created record ID: {response.id}");
RetrieveRequest
string tableLogicalName = "account";
string id = "271027A5-56FD-EE11-B4ED-005056A99CFC";
Guid accountid = new Guid(id);
RetrieveRequest request = new RetrieveRequest()
{
ColumnSet = new ColumnSet(
"accountcategorycode",
"address1_latitude",
"address1_longitude",
"creditonhold",
"numberofemployees",
"primarycontactid",
"revenue",
"name"
),
Target = new EntityReference(tableLogicalName, accountid)
};
var response = (RetrieveResponse)service.Execute(request);
UpdateRequest
Entity account = new Entity("account");
account.Id = new Guid("271027A5-56FD-EE11-B4ED-005056A99CFC");
account["name"] = "李四";
//Update the account
UpdateRequest update = new UpdateRequest()
{
Target = account
};
(UpdateResponse)service.Execute(update);
DeleteRequest
EntityReference erAccount = new EntityReference("account", new Guid("271027A5-56FD-EE11-B4ED-005056A99CFC"));
//Delete the account
DeleteRequest delete = new DeleteRequest()
{
Target = erAccount
};
(DeleteResponse)service.Execute(delete);
ExecuteMultipleRequest
以下示例仅为展示,并不可直接运行
//集合总数建议每次不超过1000条
OrganizationRequestCollection requests = new OrganizationRequestCollection();
requests.Add(new CreateRequest() { Target = new Entity() });
requests.Add(new UpdateRequest() { Target = new Entity() });
requests.Add(new DeleteRequest() { Target = new EntityReference() });
ExecuteMultipleRequest request = new ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings
{
ContinueOnError = true,//该值指示如果正在处理的当前请求返回错误,是否应继续执行 Requests 集合中的消息请求。
ReturnResponses = true//该值指示是否应返回对已处理的每个消息请求的响应。
},
Requests = requests
};
var response = (ExecuteMultipleResponse)service.Execute(request);
response.Responses.ToList().ForEach(x =>
{
if (x.Fault != null)
{
Console.WriteLine($"Request {x.RequestIndex} failed with error: {x.Fault.Message}");
}
else
{
switch (x.Response.ResponseName)
{
case "Create":
var createResponse = (CreateResponse)x.Response;
Console.WriteLine($"Created record with ID:{createResponse.id}");
break;
case "Update":
Console.WriteLine("Record updated.");
break;
case "Delete":
Console.WriteLine("Record deleted.");
break;
}
}
});
标签:account,Console,Target,service,Entity,new,Dynamics,2013,CRM
From: https://www.cnblogs.com/YuYangBlogs/p/18146124