November Happy Hour will be moved to Thursday December 5th.

Programmatically adding category to a page results in CategoryType equal to 0

Vote:
 

Hi,

I need to programmatically create some pagetypes that have a CategoryList property in them. 

I add some categories to this list, but when checking the database table 'tblContentCategory' the fields 'CategoryType' and 'ScopeName' are equal to 0.

The 'fkCategoryID' is filled in correctly however. 

Those zeroes are probably the reason that the CategoryList field on the admin-side is empty although there are categories associated with the page since there are rows in 'tblContentCategory'.

When I add another instance of that new page-type programmatically and check that table again, I see one new row for each category (same categories I used when creating them programmatically) in this categorylist where the fields 'CategoryType' and 'ScopeName' are not equal to 0 but filled in correctly.

What could be the cause for this?

Here's a code snippet of how I create my page

           var categoryRepository = _serviceLocator.GetInstance();
                    var faqQuestionPage = _contentRepository.GetDefault(faqSettingsPage.ContentLink);

                    var shipping = categoryRepository.Get(categories[i]);
                    faqQuestionPage.Category.Add(shipping.ID);

                    _contentRepository.Save(faqQuestionPage, EPiServer.DataAccess.SaveAction.Publish, AccessLevel.NoAccess);

Here's a code snippet of how I create my categories.

            var categoryRepository = _serviceLocator.GetInstance();
            var parentCategory = categoryRepository.Get("Faq");

            var shippingCategory = new Category(parentCategory, "Shipping")
            {
                Selectable = true,
                Description = "Shipping"
            };
            categoryRepository.Save(shippingCategory);
#192460
Edited, May 17, 2018 9:51
Vote:
 

I tried to reproduce this on an Alloy site running CMS Core 11.5.4 with following code:

 var categoryRepository = ServiceLocator.Current.GetInstance<CategoryRepository>();
            var shippingCategory = categoryRepository.Get("Shipping");
            if (shippingCategory == null)
            {
                var parentCategory = categoryRepository.Get("Faq");

                shippingCategory = new Category(parentCategory, "Shipping")
                {
                    Selectable = true,
                    Description = "Shipping"
                };

                categoryRepository.Save(shippingCategory);
            }

            var faqQuestionPage = ServiceLocator.Current.GetInstance<IContentRepository>().GetDefault<StandardPage>(currentPage.ContentLink);
            faqQuestionPage.Name = "faq";
            faqQuestionPage.Category.Add(shippingCategory.ID);

            ServiceLocator.Current.GetInstance<IContentRepository>().Save(faqQuestionPage, EPiServer.DataAccess.SaveAction.Publish, AccessLevel.NoAccess);

In my case the created faqQuestionPage will get the Category and it shows up as expected in edit mode. 

#192462
May 17, 2018 10:22
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.