好吧,我認爲我得到了這個工作...我基本上基於我的解決方案在這個網站上:http://www.konstrui.nl/en/about-us/blog/daniel-plomp/2013/11/13/add-a-dynamic-content-selector-to-a-user-profile和http://blog.falafel.com/blogs/josh-morales/2013/07/09/selecting-dynamic-content-in-native-sitefinity-modules-with-custom-fields以編程方式添加該字段。
但由於某些原因,它沒有爲視圖(編輯和插入)添加字段,所以我不得不手工添加。下面我會展示我做了更多細節。
首先,我產生了Sitefinity動態項目字段控制選擇通過Sitefinity迅雷,請參閱如何做到這一點的更多信息第2鏈接。
在Global.asax.cs中添加此代碼:
private void Bootstrapper_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)
{
// Register the dynamic field selector
if (e.CommandName == "Bootstrapped")
{
RegisterFieldForProduct<AssetsSelectorElement>("Telerik.Sitefinity.DynamicTypes.Model.sf_ec_prdct_skillsoftpack", "Assets");
}
}
public static void RegisterFieldForProduct<T>(string productType, string fieldName)
where T : FieldControlDefinitionElement
{
// Check if the field is not already present for this content type
var catalogManager = CatalogManager.GetManager();
var itemClrType = TypeResolutionService.ResolveType(productType);
// Specify the persistent filed CLR type (e.g. String, Guid[], ContentLink).
// Please ensure your custom field has been properly implemented to work with that CLR type
var persistentFieldType = typeof(Guid[]);
var itemType = itemClrType.FullName;
// Check to see if the field exists
var fieldExists = GetMetaFieldsForType(itemType).SingleOrDefault(f => f.FieldName == fieldName) != null;
if (fieldExists) return;
// Add the metafield that will hold the data
App.WorkWith()
.DynamicData()
.Type(itemClrType)
.Field()
.TryCreateNew(fieldName, persistentFieldType)
.SaveChanges(true);
// Get correct module configuration depending on item type
var manager = ConfigManager.GetManager();
// Suppress the security
var suppressSecurityChecks = manager.Provider.SuppressSecurityChecks;
manager.Provider.SuppressSecurityChecks = true;
// Get Backend views(e.g. Edit, Create) configuration from ProductsBackendDefinitionName
var section = Config.Get<ContentViewConfig>();
const string definitionName = "ProductsBackendDefinitionName";
var backendSection = section.ContentViewControls[definitionName];
var views = backendSection.ViewsConfig.Values.Where(v => v.ViewType == typeof(DetailFormView));
foreach (DetailFormViewElement view in views)
{
if (view.ViewName.Contains("sf_ec_prdct_skillsoftpack") == true)
{
// If there are no custom fields added before, the new field will be placed in the CustomFieldsSection
var sectionToInsert = CustomFieldsContext.GetSection(view, CustomFieldsContext.customFieldsSectionName, itemType);
var fieldConfigElementType = TypeResolutionService.ResolveType(typeof(T).FullName);
// Create a new instance of our field configuration in the current view configuration
var newElement = Activator.CreateInstance(fieldConfigElementType, new object[] { sectionToInsert.Fields }) as T;
// Populate custom field values
if (newElement == null) continue;
newElement.DataFieldName = fieldName;
newElement.FieldName = fieldName;
newElement.Title = fieldName;
newElement.DisplayMode = FieldDisplayMode.Write;
sectionToInsert.Fields.Add(newElement);
manager.SaveSection(section);
}
}
// Save and restart the application
catalogManager.SaveChanges();
manager.Provider.SuppressSecurityChecks = suppressSecurityChecks;
SystemManager.RestartApplication(true);
}
在Sitefinity後端我不得不手工添加字段視圖,管理 - >設置 - >高級 - >目錄 - >控制 - > ProductsBackendDefinitonName->查看 - > {insert_view} - > Sections->主要本條>字段
在這裏,我選擇創建新的,選擇AssetsDefinitionName(我的內容類型定義名稱),然後我只加信息這個字段:
- DataFieldName - >資產(我的字段名)
- 直寫>寫
- 字段名稱 - >資產(我的字段名)
- 字段類型 - > SitefinityWebApp.Fields.Assets.AssetsSelector,SitefinityWebApp(我的動態項目現場控制選擇器)
然後我重複相同的另一個視圖(edit_view)。
在此之後我會內容類型的列表,以特定的產品相關聯。 我認爲這是非常多的,希望我沒有忘記任何一步。
忘了提及我使用Sitefinity 6.3。 – Pedro