ทำงานกับโฟลว์ระบบคลาวด์โดยใช้โค้ด

โฟลว์ทั้งหมดจะถูกเก็บไว้ใน Dataverse และคุณสามารถใช้ Dataverse SDK สำหรับ .NET หรือ API เว็บเพื่อจัดการ

บทความนี้ครอบคลุมถึงการจัดการโฟลว์ที่รวมอยู่ในแท็บ โซลูชัน ใน Power Automate ในปัจจุบัน การจัดการโฟลว์ภายใต้ โฟลว์ของฉัน ไม่ได้รับการสนับสนุนโดยโค้ด

ทำงานกับ Dataverse API

Dataverse มีความสามารถที่เทียบเท่าการใช้ Dataverse SDK สำหรับ .NET หรือ API เว็บ

วิธีการใดที่ฉันควรใช้

วิธีที่ดีที่สุดจะขึ้นอยู่กับเทคโนโลยีของโครงการและทักษะที่คุณมี

หากโครงการของคุณใช้ .NET เราขอแนะนำให้ใช้ SDK SDK ทำให้ประสบการณ์การพัฒนาของคุณง่ายขึ้นโดยการมีโมเดลออบเจ็กต์ที่ระบุชนิดและวิธีการเพื่อรับรองความถูกต้อง

ข้อมูลเพิ่มเติม: ใช้บริการสำหรับองค์กร

เชื่อมต่ออย่างไร

วิธีการเชื่อมต่อขึ้นอยู่กับว่าคุณใช้ Dataverse SDK สำหรับ .NET หรือ API เว็บ

ด้วย SDK คุณจะต้องเชื่อมต่อกับแอปพลิเคชันไคลเอ็นต์เพื่อเข้าถึงอินสแตนซ์ IOrganizationService IOrganizationService เป็นอินเทอร์เฟซที่มีวิธีการที่คุณสามารถใช้เพื่อทำงานกับ Dataverse

ข้อมูลเพิ่มเติม:

ตารางเวิร์กโฟลว์

โฟลว์ระบบคลาวด์จัดเก็บไว้ใน ตารางกระบวนการ (เวิร์กโฟลว์) ที่แสดงใน API เว็บเป็น เวิร์กโฟลว์ EntityType

ตารางต่อไปนี้อธิบายคอลัมน์ที่สำคัญในตารางเวิร์กโฟลว์:

ชื่อตรรกะ ชนิด คำอธิบาย
category ชุดตัวเลือก ประเภทของโฟลว์ หมวดหมู่ต่างๆ มีดังต่อไปนี้
0 - เวิร์กโฟลว์ Dataverse แบบคลาสสิก
1 - กล่องโต้ตอบ Dataverse แบบคลาสสิก
2 - กฎธุรกิจ
3 - การดำเนินการของ Dataverse แบบคลาสสิก
4 - โฟลว์กระบวนการธุรกิจ
5 - โฟลว์สมัยใหม่ (โฟลลว์แบบอัตโนมัติ, แบบทันที หรือ ที่จัดกำหนดการ)
6 - โฟลว์เดสก์ท็อป
clientdata สตริง JSON ที่เข้ารหัสสตริงของข้อกำหนดโฟลว์และการอ้างอิงการเชื่อมต่อของโฟลว์
createdby การค้นหา ผู้ใช้ที่สร้างโฟลว์
createdon DateTime วันที่ที่โฟลว์ถูกสร้างขึ้น
description สตริง อธิบายผู้ใช้ที่ระบุเกี่ยวกับโฟลว์
ismanaged Bool ระบุว่าโฟลว์ถูกติดตั้งผ่านทางโซลูชันที่มีการจัดการ
modifiedby การค้นหา ผู้ใช้ล่าสุดที่อัปเดตโฟลว์
modifiedon DateTime ครั้งล่าสุดที่มีการอัปเดตโฟลว์
name สตริง ชื่อที่แสดงที่คุณกำหนดไว้สำหรับโฟลว์
ownerid การค้นหา ผู้ใช้หรือทีมที่เป็นเจ้าของโฟลว์
statecode ชุดตัวเลือก สถานะของโฟลว์ สถานะสามารถเป็น:
0 - แบบร่าง (ปิด)
1 - เปิดใช้งานแล้ว (เปิด)
2 - หยุดชั่วคราว
type ชุดตัวเลือก ระบุว่าโฟลว์เป็นโฟลว์ที่ใช้งานอยู่ หรือโครงแบบที่สามารถใช้เพื่อสร้างโฟลว์เพิ่มเติม
1 - คำนิยาม
2 - การเปิดใช้งาน
3 - เทมเพลต
workflowid Guid รหัสเฉพาะสำหรับโฟลว์ระบบคลาวด์ในการนำเข้าทั้งหมด
workflowidunique Guid ตัวระบุเฉพาะสำหรับการติดตั้งของโฟลว์นี้

หมายเหตุ

ด้วย API เว็บ ค่าการค้นหาเป็น คุณสมบัติการนำทางแบบค่าเดียว ที่สามารถขยายเพื่อดูรายละเอียดจากเรกคอร์ดที่เกี่ยวข้อง

คอลัมน์การค้นหายังมี คุณสมบัติการค้นหา GUID ที่เกี่ยวข้องที่สามารถใช้ในการสอบถาม คุณสมบัติการค้นหามีหลักการตั้งชื่อดังนี้: _<logical name>_value สำหรับเวิร์กโฟลว์ entitytype ใน API เว็บ คุณสามารถอ้างอิงคุณสมบัติการค้นหาเหล่านี้: _createdby_value, _modifiedby_value และ _ownerid_value

รายชื่อโฟลว์

หากต้องการดึงรายการโฟลว์ระบบคลาวด์ คุณสามารถสอบถามตารางเวิร์กโฟลว์ได้ การสอบถามต่อไปนี้ส่งคืนเฉพาะโฟลว์แบบอัตโนมัติ แบบทันที หรือโฟลว์ที่จัดกำหนดการเป็น 'เปิด':

วิธีการ OutputFirstActiveFlow แบบคงที่นี้ต้องการไคลเอ็นต์ที่ผ่านการรับรองความถูกต้องซึ่งใช้ IOrganizationService ซึ่งใช้วิธีการ IOrganizationService.RetrieveMultiple

/// <summary>
/// Outputs the first active flow
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
public static void OutputFirstActiveFlow(IOrganizationService service)
{
   var query = new QueryExpression("workflow")
   {
         ColumnSet = new ColumnSet("category",
                                    "createdby",
                                    "createdon",
                                    "description",
                                    "ismanaged",
                                    "modifiedby",
                                    "modifiedon",
                                    "name",
                                    "ownerid",
                                    "statecode",
                                    "type",
                                    "workflowid",
                                    "workflowidunique"),
         Criteria = new FilterExpression(LogicalOperator.And)
         {
            Conditions = {
            {  new ConditionExpression(
               "category",
                     ConditionOperator.Equal,
                     5) }, // Cloud Flow
            {  new ConditionExpression(
                     "statecode",
                     ConditionOperator.Equal,
                     1) } // Active
         }
         },
         TopCount = 1 // Limit to one record
   };

   EntityCollection workflows = service.RetrieveMultiple(query);

   Entity workflow = workflows.Entities.FirstOrDefault();

   Console.WriteLine($"category: {workflow.FormattedValues["category"]}");
   Console.WriteLine($"createdby: {workflow.FormattedValues["createdby"]}");
   Console.WriteLine($"createdon: {workflow.FormattedValues["createdon"]}");
   // Description may be null
   Console.WriteLine($"description: {workflow.GetAttributeValue<string>("description")}");
   Console.WriteLine($"ismanaged: {workflow.FormattedValues["ismanaged"]}");
   Console.WriteLine($"modifiedby: {workflow.FormattedValues["modifiedby"]}");
   Console.WriteLine($"modifiedon: {workflow.FormattedValues["modifiedon"]}");
   Console.WriteLine($"name: {workflow["name"]}");
   Console.WriteLine($"ownerid: {workflow.FormattedValues["ownerid"]}");
   Console.WriteLine($"statecode: {workflow.FormattedValues["statecode"]}");
   Console.WriteLine($"type: {workflow.FormattedValues["type"]}");
   Console.WriteLine($"workflowid: {workflow["workflowid"]}");
   Console.WriteLine($"workflowidunique: {workflow["workflowidunique"]}");
}

หากต้องการเรียกเรกคอร์ดเพิ่มเติม ให้เอาขีดจำกัด TopCount ออก

เอาท์พุท

category: Modern Flow
createdby: SYSTEM
createdon: 5/20/2020 9:37 PM
description:
ismanaged: Unmanaged
modifiedby: Kiana Anderson
modifiedon: 5/6/2023 3:37 AM
name: When an account is updated -> Create a new record
ownerid: Monica Thomson
statecode: Activated
type: Definition
workflowid: d9e875bf-1c9b-ea11-a811-000d3a122b89
workflowidunique: c17af45c-10a1-43ca-b816-d9cc352718cf

ข้อมูลเพิ่มเติม:

สร้างโฟลว์ระบบคลาวด์

คุณสมบัติจำเป็นสำหรับโฟลว์แบบอัตโนมัติ แบบโต้ตอบแบบทันที และที่จัดกำหนดการไว้ ประกอบด้วย: category, name, type, primaryentity และ clientdata ใช้ none สำหรับ primaryentity สำหรับโฟลว์ประเภทเหล่านี้

วิธีการแบบคงที่นี้ต้องการไคลเอ็นต์ที่ผ่านการรับรองความถูกต้องซึ่งใช้ IOrganizationService ซึ่งใช้วิธีการ IOrganizationService.Create

/// <summary>
/// Creates a cloud flow
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <returns>The workflowid</returns>
public static Guid CreateCloudFlow(IOrganizationService service)
{
   var workflow = new Entity("workflow")
   {
         Attributes = {
            {"category", new OptionSetValue(5) }, // Cloud flow
            {"name", "Sample flow name"},
            {"type", new OptionSetValue(1) }, //Definition
            {"description", "This flow reads some data from Dataverse." },
            {"primaryentity", "none" },
            {"clientdata", "{\"properties\":{\"connectionReferences\":{\"shared_commondataserviceforapps\":{\"impersonation\":{},\"runtimeSource\":\"embedded\",\"connection\":{\"name\":\"shared-commondataser-114efb88-a991-40c7-b75f-2693-b1ca6a0c\",\"connectionReferenceLogicalName\":\"crdcb_sharedcommondataserviceforapps_109ea\"},\"api\":{\"name\":\"shared_commondataserviceforapps\"}}},\"definition\":{\"$schema\":\"https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#\",\"contentVersion\":\"1.0.0.0\",\"parameters\":{\"$connections\":{\"defaultValue\":{},\"type\":\"Object\"},\"$authentication\":{\"defaultValue\":{},\"type\":\"SecureObject\"}},\"triggers\":{\"manual\":{\"metadata\":{\"operationMetadataId\":\"76f87a86-89b3-48b4-92a2-1b74539894a6\"},\"type\":\"Request\",\"kind\":\"Button\",\"inputs\":{\"schema\":{\"type\":\"object\",\"properties\":{},\"required\":[]}}}},\"actions\":{\"List_rows\":{\"runAfter\":{},\"metadata\":{\"operationMetadataId\":\"9725b30f-4a8e-4695-b6fd-9a4985808809\"},\"type\":\"OpenApiConnection\",\"inputs\":{\"host\":{\"apiId\":\"/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps\",\"connectionName\":\"shared_commondataserviceforapps\",\"operationId\":\"ListRecords\"},\"parameters\":{\"entityName\":\"accounts\",\"$select\":\"name\",\"$top\":1},\"authentication\":\"@parameters('$authentication')\"}}}}},\"schemaVersion\":\"1.0.0.0\"}" }
         }
   };

   return service.Create(workflow);
}

ข้อมูลเพิ่มเติม: สร้างแถวของตารางโดยใช้บริการสำหรับองค์กร

statecode ของโฟลว์ทั้งหมดที่สร้างขึ้นด้วยวิธีนี้ตั้งค่าเป็น 0 (แบบร่างหรือ 'ปิด') ต้องเปิดใช้งานโฟลว์ก่อนจึงจะใช้งานได้

คุณสมบัติที่สำคัญที่สุดคือ clientdata ซึ่งมี connectionReferences ที่โฟลว์ใช้ และ คำนิยาม ของโฟลว์ connectionReferences เป็นการแมปไปยังแต่ละการเชื่อมต่อที่โฟลว์ใช้

{
  "properties": {
    "connectionReferences": {
      "shared_commondataserviceforapps": {
        "runtimeSource": "embedded",
        "connection": {},
        "api": { 
         "name": "shared_commondataserviceforapps" 
         }
      }
    },
    "definition": {
      "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "$connections": { "defaultValue": {}, "type": "Object" },
        "$authentication": { "defaultValue": {}, "type": "SecureObject" }
      },
      "triggers": {
        "manual": {
          "metadata": {},
          "type": "Request",
          "kind": "Button",
          "inputs": {
            "schema": { "type": "object", "properties": {}, "required": [] }
          }
        }
      },
      "actions": {
        "List_rows": {
          "runAfter": {},
          "metadata": {},
          "type": "OpenApiConnection",
          "inputs": {
            "host": {
              "apiId": "/providers/Microsoft.PowerApps/apis/shared_commondataserviceforapps",
              "connectionName": "shared_commondataserviceforapps",
              "operationId": "ListRecords"
            },
            "parameters": {
              "entityName": "accounts",
              "$select": "name",
              "$top": 1
            },
            "authentication": "@parameters('$authentication')"
          }
        }
      }
    }
  },
  "schemaVersion": "1.0.0.0"
}

ปรับปรุงโฟลว์ระบบคลาวด์

หากต้องการอัปเดตโฟลว์ ให้ตั้งค่าเฉพาะคุณสมบัติที่คุณต้องการเปลี่ยนแปลง

วิธีการแบบคงที่นี้ต้องการไคลเอ็นต์ที่ผ่านการรับรองความถูกต้องซึ่งใช้ IOrganizationService โดยใช้วิธีการ IOrganizationService.Update เพื่ออัปเดตคำอธิบายของโฟลว์และตั้งค่าเจ้าของ

/// <summary>
/// Updates a cloud flow
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="workflowid">The ID of the flow to update.</param>
/// <param name="systemuserid">The id of the user to assign the flow to.</param>
public static void UpdateCloudFlow(IOrganizationService service, Guid workflowid, Guid systemuserid) {

   var workflow = new Entity("workflow",workflowid)
   {
         Attributes = {

            {"description", "This flow will ensure consistency across systems." },
            {"ownerid", new EntityReference("systemuser",systemuserid)},
            {"statecode", new OptionSetValue(1) } //Turn on the flow.
         }
   };

   service.Update(workflow);
}

ข้อมูลเพิ่มเติม: อัปเดตและลบแถวของตารางโดยใช้บริการสำหรับองค์กร > การอัปเดตเบื้องต้น

ลบโฟลว์ระบบคลาวด์

ตัวอย่างต่อไปนี้แสดงวิธีการลบเรกคอร์ดของเวิร์กโฟลว์ที่แสดงถึงโฟลว์ระบบคลาวด์

วิธีการ DeleteCloudFlow แบบคงที่จะลบเรกคอร์ดของเวิร์กโฟลว์

/// <summary>
/// Deletes a workflow
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="workflowId">The id of the cloud flow to delete.</param>
public static void DeleteCloudFlow(IOrganizationService service, Guid workflowId) { 

service.Delete(entityName:"workflow",id: workflowId);

}

ข้อมูลเพิ่มเติม: ลบเรกคอร์ดโดยใช้ SDK

รับผู้ใช้ทั้งหมดที่แชร์โฟลว์ระบบคลาวด์

ใช้ข้อความ RetrieveSharedPrincipalsAndAccess เพื่อดูรายชื่อผู้ใช้ทั้งหมดที่แชร์โฟลว์ระบบคลาวด์ด้วย

ด้วย SDK ให้ใช้ คลาส RetrieveSharedPrincipalsAndAccessRequest และด้วย API เว็บ ให้ใช้ ฟังก์ชัน RetrieveSharedPrincipalsAndAccess

ข้อมูลเพิ่มเติม: ดูบัญชีผู้ใช้หลักที่มีสิทธิ์เข้าถึงเรกคอร์ด

แชร์หรือยกเลิกแชร์โฟลว์ระบบคลาวด์

แชร์โฟลว์ระบบคลาวด์เหมือนกับเรกคอร์ด Dataverse อื่นๆ โดยใช้ข้อความ GrantAccess ด้วย SDK ให้ใช้ คลาส GrantAccessRequest และด้วย API เว็บ ให้ใช้ การดำเนินการ GrantAccess ข้อมูลเพิ่มเติม: ตัวอย่าง GrantAccess

หากคุณต้องการเปลี่ยนสิทธิ์การเข้าถึงที่คุณให้เมื่อคุณแชร์เรกคอร์ด ให้ใช้ข้อความ ModifyAccess ด้วย SDK ให้ใช้ คลาส ModifyAccessRequest และด้วย API เว็บ ให้ใช้ การดำเนินการ ModifyAccess ข้อมูลเพิ่มเติม: ตัวอย่าง ModifyAccess

หากต้องการยกเลิกการแชร์เรกคอร์ด ให้ใช้ข้อความ RevokeAccess ด้วย SDK ให้ใช้ คลาส RevokeAccessRequest และด้วย API เว็บ ให้ใช้ การดำเนินการ RevokeAccess ข้อมูลเพิ่มเติม: การเพิกถอนการเข้าถึง

ส่งออกโฟลว์

เมื่อโฟลว์เป็นส่วนหนึ่งของโซลูชัน คุณสามารถส่งออกได้โดยส่งออกโซลูชันที่มีโฟลว์โดยใช้ข้อความ ExportSolution

ตัวอย่างวิธีการ ExportSolution แบบคงที่ด้านล่างใช้ ExportSolutionRequest เพื่อดึงข้อมูล byte[] ที่มีไฟล์ ZIP ของโซลูชันที่ไม่มีการจัดการด้วย UniqueName ที่ระบุ

/// <summary>
/// Exports an unmanaged solution
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="solutionUniqueName">The uniquename of the solution.</param>
/// <returns></returns>
public static byte[] ExportSolution(
   IOrganizationService service, 
   string solutionUniqueName) 
{
   ExportSolutionRequest request = new() { 
         SolutionName = solutionUniqueName,
         Managed = false
   };

   var response = (ExportSolutionResponse)service.Execute(request);

   return response.ExportSolutionFile;
}

นำเข้าโฟลว์

เมื่อคุณมีไฟล์ ZIP ของโซลูชัน คุณสามารถนำเข้าได้โดยใช้ข้อความ ImportSolution

เมื่อคุณนำเข้าโฟลว์ คุณควรตั้งค่าพารามิเตอร์ต่อไปนี้:

ชื่อคุณสมบัติ คำอธิบาย
OverwriteUnmanagedCustomizations ถ้ามีอินสแตนซ์ที่มีอยู่ของโฟลว์เหล่านี้ใน Dataverse ค่าสถานะนี้จำเป็นต้องถูกตั้งค่าเป็น true เพื่อนำเข้า มิฉะนั้นจะไม่ถูกเขียนทับ
PublishWorkflows ระบุว่าเวิร์กโฟลว์ Dataverse แบบคลาสสิกจะถูกเปิดใช้งานเมื่อนำเข้าหรือไม่ การตั้งค่านี้ไม่นำไปใช้กับโฟลว์ประเภทอื่นๆ
CustomizationFile ไฟล์ zip ที่เข้ารหัส 64 ฐานที่มีโซลูชัน

วิธีการตัวอย่าง ImportSolution แบบคงที่แสดงวิธีนำเข้าไฟล์โซลูชันโดยใช้ คลาส ImportSolutionRequest

/// <summary>
/// Imports a solution.
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="solutionFile">The byte[] data representing a solution file. </param>
public static void ImportSolution(
   IOrganizationService service, 
   byte[] solutionFile) {

   ImportSolutionRequest request = new() { 
         OverwriteUnmanagedCustomizations = true,
         CustomizationFile = solutionFile
   };

   service.Execute(request);
}

ดูเพิ่มเติม

การดำเนินการของคลาสเอนทิตีโดยใช้บริการสำหรับองค์กร
ดําเนินงานโดยใช้ API เว็บ
การแชร์และการกำหนด
การตรวจสอบการเข้าถึงในโค้ด
ทำงานกับโซลูชันโดยใช้ Dataverse SDK