Dynamics CRM 2013 批處理

Destiny、Yang發表於2024-04-22

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;
        }
    }
});

相關文章