如何使用 ASP.NET MVC 调用 Kentico 的 API?
how to do API calls of Kentico using ASP.NET MVC?
我正在使用 ASP.NET MVC 对 Kentico 表单进行 API 调用,因此我可以使用 AngularJS 来显示 return 数据(JSON格式)。
具体来说,我的客户在他们的服务器上使用 Kentico 使用 "Forms" 在 Kentico 上创建数据,我想使用 ASP.NET 通过 API 调用获取存储在这些表单中的记录MVC。我在想的是,在 "Forms" 的一般部分,我看到 "Form code name" 显示 "Code name is a string identifier of the object that can be used by developers in API calls or URLs"。但似乎在互联网上没有很好的例子。继续尝试搜索它但没有运气。我还尝试直接在 kentico 存储数据的 SQL 服务器中访问数据。但是Kentico在SQL服务器中用来存储数据的table的名称与Kentico在"Forms"或"Custom tables"中的不同。
希望有人能告诉我怎么做,我真的很感激。提前致谢。
官方有一个很好的例子documentation of Kentico。
请注意,Forms 过去曾多次重命名(它们被称为 BizForms 和 On-Line forms),这就是下面代码引用 CMS.OnlineForms
并使用 BizFormInfoProvider
的原因。这也很可能是您找不到任何好的例子的原因:)
下面的示例显示了如何检索表单的定义(元数据)、获取所有数据并遍历它。
using CMS.OnlineForms;
using CMS.DataEngine;
using CMS.SiteProvider;
using CMS.Helpers;
...
// Gets the form info object for the 'ContactUs' form
BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("ContactUs", SiteContext.CurrentSiteID);
// Gets the class name of the 'ContactUs' form
DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
string className = formClass.ClassName;
// Loads the form's data
ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(className);
// Checks whether the form contains any records
if (!DataHelper.DataSourceIsEmpty(data))
{
// Loops through the form's data records
foreach (BizFormItem item in data)
{
string firstNameFieldValue = item.GetStringValue("FirstName", "");
string lastNameFieldValue = item.GetStringValue("LastName", "");
// Perform any required logic with the form field values
// Variable representing a custom value that you want to save into the form data
object customFieldValue;
// Programatically assigns and saves a value for the form record's 'CustomField' field
item.SetValue("CustomField", customFieldValue);
item.SubmitChanges(false);
}
}
更新:
上面的示例假定您正在使用 运行 Kentico 实例中的 API。如果您想从外部应用程序使用 Kentico API (DLL),请按照我在另一个 answer.
中描述的步骤操作
您还询问了站点标识符(BizFormInfoProvider.GetBizFormInfo()
方法的 siteId 或 siteName 参数)。它们指的是 Kentico 中的 SiteInfo
对象 (DB table CMS_Site
)。如果您导航到 Site->Edit site->General->Site code name
.
,您可以找到站点名称
如果您不想使用 Kentico DLL,还有另一种选择 - 使用 Kentico REST endpoint。
我正在使用 ASP.NET MVC 对 Kentico 表单进行 API 调用,因此我可以使用 AngularJS 来显示 return 数据(JSON格式)。
具体来说,我的客户在他们的服务器上使用 Kentico 使用 "Forms" 在 Kentico 上创建数据,我想使用 ASP.NET 通过 API 调用获取存储在这些表单中的记录MVC。我在想的是,在 "Forms" 的一般部分,我看到 "Form code name" 显示 "Code name is a string identifier of the object that can be used by developers in API calls or URLs"。但似乎在互联网上没有很好的例子。继续尝试搜索它但没有运气。我还尝试直接在 kentico 存储数据的 SQL 服务器中访问数据。但是Kentico在SQL服务器中用来存储数据的table的名称与Kentico在"Forms"或"Custom tables"中的不同。
希望有人能告诉我怎么做,我真的很感激。提前致谢。
官方有一个很好的例子documentation of Kentico。
请注意,Forms 过去曾多次重命名(它们被称为 BizForms 和 On-Line forms),这就是下面代码引用 CMS.OnlineForms
并使用 BizFormInfoProvider
的原因。这也很可能是您找不到任何好的例子的原因:)
下面的示例显示了如何检索表单的定义(元数据)、获取所有数据并遍历它。
using CMS.OnlineForms;
using CMS.DataEngine;
using CMS.SiteProvider;
using CMS.Helpers;
...
// Gets the form info object for the 'ContactUs' form
BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("ContactUs", SiteContext.CurrentSiteID);
// Gets the class name of the 'ContactUs' form
DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
string className = formClass.ClassName;
// Loads the form's data
ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(className);
// Checks whether the form contains any records
if (!DataHelper.DataSourceIsEmpty(data))
{
// Loops through the form's data records
foreach (BizFormItem item in data)
{
string firstNameFieldValue = item.GetStringValue("FirstName", "");
string lastNameFieldValue = item.GetStringValue("LastName", "");
// Perform any required logic with the form field values
// Variable representing a custom value that you want to save into the form data
object customFieldValue;
// Programatically assigns and saves a value for the form record's 'CustomField' field
item.SetValue("CustomField", customFieldValue);
item.SubmitChanges(false);
}
}
更新: 上面的示例假定您正在使用 运行 Kentico 实例中的 API。如果您想从外部应用程序使用 Kentico API (DLL),请按照我在另一个 answer.
中描述的步骤操作您还询问了站点标识符(BizFormInfoProvider.GetBizFormInfo()
方法的 siteId 或 siteName 参数)。它们指的是 Kentico 中的 SiteInfo
对象 (DB table CMS_Site
)。如果您导航到 Site->Edit site->General->Site code name
.
如果您不想使用 Kentico DLL,还有另一种选择 - 使用 Kentico REST endpoint。