【发布时间】:2019-10-17 12:48:45
【问题描述】:
我在 VIsual Studio 2017,C# 4.61 上运行 Kentico 12 MVC 站点(从 Kentico 11 MVC 转换而来)。
我正在构建一个一次性 API,将数百个 Azure 搜索索引转换为新的本地智能搜索索引。还有更多内容,因为我还必须更新相关的页面类型。这涉及创建一个新字段、更新另一个默认值以及更新本地索引的所有这些搜索复选框。我几乎可以让它按预期工作,但是当我动态添加我的新页面类型字段并成功更新所有内容时,我似乎仍然无法在 ClassSearchSettings XML 中找到我的新字段。
我发现了另一个关于在页面类型中创建新字段的问题,我在这里找到了:kentico add new form field via API。这帮助我完成了字段的动态创建。
这是我正在做的事情的低调,希望它会对你有所帮助,但我真正的问题是更进一步。 newIndex 是我自己构建的类的实例。
1) 在此处检索索引:
var indexes = SearchIndexInfoProvider.GetSearchIndexes()
.WhereEquals("IndexSearchServiceName", searchServiceName)
//.WhereEquals("IndexName", indexName)
.ToList();
2) 检索页面类型项目和相关字段。我使用了这个代码块,并且在遍历集合时,我的参数 newIndex.PageTypeClassName 是从前一个块中检索到的。在单个索引上时,使用以下代码获取页面类型类名称:
2a:
newIndex.PageTypeClassName = index.IndexSettings.Items.FirstOrDefault().Value.ClassNames;
然后获取您的页面类型“东西”(DataClassInfo、FormInfo、FormFieldInfo 列表
2b:
var dci = DataClassInfoProvider.GetDataClassInfo(newIndex.PageTypeClassName);
var fi = new CMS.FormEngine.FormInfo(dci.ClassFormDefinition);
var pageTypeFields = fi.GetFields(true, true, false);
3) 创建并添加新的页面类型字段:
var newField = new FormFieldInfo
{
Name = DataAutomationHelper.FilterAttributeTitle,
DefaultValue = string.Empty,
Size = 200,
Enabled = true,
AllowEmpty = true,
System = false,
FieldType = FormFieldControlTypeEnum.TextBoxControl,
DataType = FieldDataType.Text,
Visible = false, // this is not a visible field, only used for filtering and creation of the LEFT menu items
};
newIndex.FormInfo.AddFormItem(newField, 2);
var tm = new TableManager(System.Configuration.ConfigurationManager.ConnectionStrings["CMSConnectionString"].ToString());
//tm.DropTableColumn(newIndex.CorrectedPageTypeClassName, DataAutomationHelper.FilterAttributeTitle);
tm.AddTableColumn(newIndex.CorrectedPageTypeClassName, newField.Name, "nvarchar(200)", true, string.Empty);
// Add new column to database
newIndex.PageType.ClassXmlSchema = tm.GetXmlSchema(newIndex.CorrectedPageTypeClassName);
newIndex.PageType.ClassFormDefinition = newIndex.FormInfo.GetXmlDefinition();
// Update DataClassInfo object
DataClassInfoProvider.SetDataClassInfo(newIndex.PageType);
// Update inherited classes with new field
FormHelper.UpdateInheritedClasses(newIndex.PageType);
耶!我的页面类型中有一个新字段!我认为我必须再次获取我的页面类型才能看到更新的字段,但 ClassSearchSettings 仍然与添加新字段之前相同。 奇怪的是:当我打开另一个浏览器,查看该页面类型的详细信息时,我可以看到我的新字段,并且还可以在搜索复选框列表中看到它。为什么会这样?有什么需要刷新的吗?请让我知道您的想法,我们将不胜感激!我也可以根据需要澄清。谢谢!
【问题讨论】:
标签: xml api indexing lucene kentico