2014-04-03 89 views
0

我想內容類型的列表/陣列的產品,即關聯,我有幾個內容類型和創建產品時,在Sitefinity電子商務,我需要此產品有多種內容類型相關聯。 到目前爲止,我創建了一個動態字段選擇器來選擇產品上的幾種類型conten,這工作得很好,直到此刻我嘗試將其保存和序列化引發錯誤,因爲我無法設置該字段的類型爲GUID [ ],就像內容類型字段一樣,所以我選擇長文本。但是因爲它試圖將Guid []轉換爲字符串,所以會引發錯誤。如何將內容類型與Sitefinity上的產品關聯?

任何關於如何做到這一點還是一個提示,所以我可以跟進任何IDEIA?對建在Sitefinity內容模塊使用選擇

+0

忘了提及我使用Sitefinity 6.3。 – Pedro

回答

0

好吧,我認爲我得到了這個工作...我基本上基於我的解決方案在這個網站上:http://www.konstrui.nl/en/about-us/blog/daniel-plomp/2013/11/13/add-a-dynamic-content-selector-to-a-user-profilehttp://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)。

在此之後我會內容類型的列表,以特定的產品相關聯。 我認爲這是非常多的,希望我沒有忘記任何一步。

0

可惜你不能指定自定義GUID []字段,但是在沙拉三明治的人有一個博客貼子,展示瞭如何通過代碼添加的選擇:http://blog.falafel.com/blogs/josh-morales/2013/07/09/selecting-dynamic-content-in-native-sitefinity-modules-with-custom-fields

+0

感謝您的回覆本,我已經發現這個博客和另一個類似的:http://www.konstrui.nl/en/about-us/blog/daniel-plomp/2013/11/13/add-a -dynamic-內容選擇到一個用戶簡檔。 Telerik的人回答我說Sitefinity 7應該可以提供這種功能。我們將會看到......現在我仍然試圖弄清楚這個...... – Pedro

+0

我剛剛從Sitefinity得到了這個消息:「Sitefinity 7.0的發佈預計在下週的下半年,在4月9日和11日之間」和發佈說明列表「管理任何內容關係」,這裏是一個視頻顯示它:https://www.youtube.com/watch?v = Hef5tJEFNvE 不確定你的時間框架是什麼,但可能值得等待升級。 – Ben

相關問題