使用代码创建自定义 API

Note

使用代码创建 API 是一个高级主题,假定你阅读并理解以下文章:

还应了解如何使用用于.NET的 Web API 或 SDK 创建Microsoft Dataverse记录。 有关详细信息,请参见:

由于自定义 API 数据保存在表中,因此可以使用 Web API 或 sdk for .NET 以编程方式创建新 API。

自定义 API 表描述了可以使用代码设置的所有属性。

此代码使用 CrmServiceClient 进行早期绑定的编程样式。 还可以使用 ServiceClient。 详细信息:

此示例演示如何在单个操作中使用一个请求参数和一个响应属性创建自定义 API 操作。 详细信息: 在一个操作中创建相关实体

此自定义 API 作为具有唯一名称 CustomAPIExample 的解决方案的一部分创建,并与 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 ServiceClient(conn);
var service = new CrmServiceClient(conn);
// var service = new ServiceClient(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
自定义 API 表
使用插件注册工具创建自定义 API
在 Power Apps 中创建自定义 API
使用解决方案文件创建自定义 API