+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to associate a Content Types to a Catalog Product?

How to associate a Content Types to a Catalog Product?

4 posts, 1 answered
  1. Squall
    Squall avatar
    3 posts
    Registered:
    03 Apr 2014
    03 Apr 2014
    Link to this post

    I'm trying to associate a List/Array of Content Types to a Product, i.e, i have several content types and when creating a Product, on Sitefinity Ecommerce, i need to associate this Product with several content types. So far i created a Dynamic Field Selector to select several conten types on the Product, this works fine until the moment i try to save it and a serialization error is thrown since i can't set the Type of this field as a Guid[], like on Content Types fields, so i selected as long text. But since it's trying to convert a Guid[] to string it throws a error.

    Anyone as any ideia on how to do this or a hint so i can follow up?

  2. Grisha 'Greg' Karanikolov
    Grisha 'Greg' Karanikolov avatar
    156 posts
    Registered:
    30 Nov 2016
    04 Apr 2014
    Link to this post
    Hi Squall,

    The best thing you could do is use Sitefinity 7.0 once it comes out (which is going to happen in the following week or two the most), where the feature to associate any content type with any content type (interface included) comes out-of-the-box.



    Regards,
    Grisha 'Greg' Karanikolov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. Squall
    Squall avatar
    3 posts
    Registered:
    03 Apr 2014
    04 Apr 2014 in reply to Grisha 'Greg' Karanikolov
    Link to this post

    Hi Greg,

     Thanks fro the reply, isn't it possible somehow to do it in Sitefinity 6.3? Programmatically or not?

  4. Squall
    Squall avatar
    3 posts
    Registered:
    03 Apr 2014
    05 Apr 2014
    Link to this post

    Ok, i got this working...below are the steps i took.

     I basically used this site: http://www.konstrui.nl/en/about-us/blog/daniel-plomp/2013/11/13/add-a-dynamic-content-selector-to-a-user-profile and http://blog.falafel.com/blogs/josh-morales/2013/07/09/selecting-dynamic-content-in-native-sitefinity-modules-with-custom-fields.

    To programmatically add the field, but for some reason it didn't add the field for the views (Edit and Insert) so i had to add them by hand. Below i will show what i did with more detail.First i generated a Sitefinity Dynamic Items Field Control Selector through Sitefinity Thunder, see 2nd link for more information on how to do this.

    On Global.asax.cs i added this code:

    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);
    }

    On Sitefinity BackEnd i had to add the field view by hand, Administration->Settings->Advanced->Catalog->ProductsBackendDefinitonName->Views->{insert_view}->Sections->Main Section->Fields

    Here i selected create new, choose AssetsDefinitionName (my Content Type Definition Name), then i only added information to this fields:

    DataFieldName -> Assets (My field name)

    Write-> WriteField Name -> Assets (My field name)

    Field Type -> SitefinityWebApp.Fields.Assets.AssetsSelector, SitefinityWebApp (My Dynamic Items Field Control Selector)

    Then i repeated the same to the other view (edit_view).

    After this i could associate a list of content type to a specific product. I think it was pretty much this, hope i didn't forgot any step.

    Answered
4 posts, 1 answered