コードを使用したカスタム API の作成

注意

これは、これらのトピックをすでに読んで、理解していることを前提とした高度なトピックです:

Web API または組織サービスのいずれかを使用して、Microsoft Dataverse レコードを作成する方法を理解する必要もあります。 詳細については、以下を参照してください。

カスタム API のデータはテーブルに保存されるため、Web API または組織サービスを使用してプログラムにより新しい API を作成することができます。

カスタム API テーブル のテーブルで、コードを使用して設定できるすべてのプロパティを記述します。

Web API を使用してカスタム API を作成する

この例では、1 つの要求パラメーターと 1 つの応答プロパティを持つカスタム API アクションをひとつの操作で作成しています。 詳細情報: 1 回の操作で関連するテーブル行を作成する

このカスタム APIは、CustomAPIExample という一意の名称のソリューションの一部として作成され、id = 00000000-0000-0000-0000-000000000001 というプラグイン タイプに関連付けられています。

要求

POST [Organization URI]/api/data/v9.1/customapis HTTP/1.1
MSCRM.SolutionUniqueName: CustomAPIExample
Content-Type: application/json

{
    "allowedcustomprocessingsteptype": 0,
    "boundentitylogicalname": null,
    "uniquename": "sample_CustomAPIExample",
    "displayname": "Custom API Example",
    "bindingtype": 0,
    "executeprivilegename": null,
    "isfunction": false,
    "isprivate": false,
    "name": "sample_CustomAPIExample",
    "description": "A simple example of a Custom API",
    "iscustomizable": {
                "Value": false
            },
    "CustomAPIRequestParameters": [
        {
            "type": 10,
            "isoptional": false,
            "displayname": "Custom API Example String Parameter",
            "name": "sample_CustomAPIExample.StringParameter",
            "uniquename": "StringParameter",
            "logicalentityname": null,
            "description": "The StringParameter request parameter for Custom API Example",
            "iscustomizable": {
                "Value": false
                }
        }
    ],
    "CustomAPIResponseProperties": [
        {
            "type": 10,
            "name": "sample_CustomAPIExample.StringProperty",
            "logicalentityname": null,
            "displayname": "Custom API Example String Property",
            "uniquename": "StringProperty",
            "description": "The StringProperty response property for Custom API Example",
            "iscustomizable": {
                "Value": false
                }
        }
    ],
    "PluginTypeId@odata.bind": "plugintypes(00000000-0000-0000-0000-000000000001)"
}

回答

HTTP/1.1 204 No Content
OData-Version: 4.0
OData-EntityId: [Organization URI]/api/data/v9.1/customapis(b532b299-4684-eb11-a812-0022481d298f)

組織サービスを使用してカスタム API を作成する

このコードは、アーリーバウンドのプログラミング スタイルで CrmServiceClient を使用しています。詳細情報 :

この例では、ひとつのリクエスト パラメータとひとつのレスポンス プロパティを持つカスタム API アクションをひとつの操作で作成しています。 詳細: 1 回の操作で関連するエンティティを作成する

このカスタム APIは、CustomAPIExample という uniquename のソリューションの一部として作成され、id = 00000000-0000-0000-0000-000000000001 というプラグインの種類に関連付けられています。

string conn = $@"
    Url = {url};
    AuthType = OAuth;
    UserName = {userName};
    Password = {password};
    AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d;
    RedirectUri = app://58145B91-0C36-4500-8554-080854F2AC97;
    LoginPrompt=Auto;
    RequireNewInstance = True";

var service = new CrmServiceClient(conn);

//The plug-in type
var pluginType = new EntityReference("plugintype", new Guid("00000000-0000-0000-0000-000000000001"));
var solutionUniqueName = "CustomAPIExample";

//The custom API
var customAPI = new CustomAPI
{
    AllowedCustomProcessingStepType = new OptionSetValue(0),//None
    BindingType = new OptionSetValue(0), //Global
    Description = "A simple example of a Custom API",
    DisplayName = "Custom API Example",
    ExecutePrivilegeName = null,
    IsFunction = false,
    IsPrivate = false,
    Name = "sample_CustomAPIExample",
    PluginTypeId = pluginType,
    UniqueName = "sample_CustomAPIExample",
    IsCustomizable = new BooleanManagedProperty(false),
    customapi_customapirequestparameter = new List<CustomAPIRequestParameter>()
    {
        new CustomAPIRequestParameter {
            Description = "The StringParameter request parameter for Custom API Example",
            DisplayName = "Custom API Example String Parameter",
            LogicalEntityName = null,
            IsOptional = false,
            Name = "sample_CustomAPIExample.StringParameter",
            Type = new OptionSetValue(10), //String
            UniqueName = "StringParameter",
            IsCustomizable = new BooleanManagedProperty(false)
        }
    },
    customapi_customapiresponseproperty = new List<CustomAPIResponseProperty>()
    {
        new CustomAPIResponseProperty {
            Description = "The StringProperty response property for Custom API Example",
            DisplayName = "Custom API Example String Property",
            Name = "sample_CustomAPIExample.StringProperty",
            Type = new OptionSetValue(10), //String
            UniqueName = "StringProperty",
            IsCustomizable = new BooleanManagedProperty(false)
        }
    }
};

var createReq = new CreateRequest
{
    Target = customAPI
};
createReq["SolutionUniqueName"] = solutionUniqueName;

Guid customAPIId = ((CreateResponse)service.Execute(createReq)).id;

関連情報

カスタム API の作成と使用
CustomAPI テーブル
プラグイン登録ツールを使用してカスタム API を作成する
Power Apps でカスタム API を作成する
ソリューション ファイルを使用したカスタム API の作成

注意

ドキュメントの言語設定についてお聞かせください。 簡単な調査を行います。 (この調査は英語です)

この調査には約 7 分かかります。 個人データは収集されません (プライバシー ステートメント)。