# What is the best way to link newly added product to a category?

**URL:** <https://forum.litium.com/t/what-is-the-best-way-to-link-newly-added-product-to-a-category/1567>\
**Category:** Questions\
**Tags:** litium-7\
**Created:** [July 20, 2020, 4:20am UTC](https://forum.litium.com/t/what-is-the-best-way-to-link-newly-added-product-to-a-category/1567 "2020-07-20T04:20:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinesh](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@jinesh](https://forum.litium.com/u/jinesh)\
**Post date:** [July 20, 2020, 4:20am UTC](https://forum.litium.com/t/what-is-the-best-way-to-link-newly-added-product-to-a-category/1567/1 "2020-07-20T04:20:25Z")

</div>

After 7.4.2, Product category link have been moved to Category. So whats the best way to link a newly imported product to a category.

It seems, adding to product links of category and updating category results a rebuild index of all products in that category. Is there any other better way?

Litium version: _7.4.3_

---

<div class="post-metadata">

**Author:** ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)\
**Post date:** [July 20, 2020, 9:27am UTC](https://forum.litium.com/t/what-is-the-best-way-to-link-newly-added-product-to-a-category/1567/2 "2020-07-20T09:27:45Z")

</div>

Your available option is to do as you’re describing, adding the product to the `.ProductLinks` collection of the category.

However, when testing I’m not seeing indexing logged for all products in the category, only the one just updated.

---

<div class="post-metadata">

**Author:** ![jinesh](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@jinesh](https://forum.litium.com/u/jinesh)\
**Post date:** [July 20, 2020, 12:14pm UTC](https://forum.litium.com/t/what-is-the-best-way-to-link-newly-added-product-to-a-category/1567/3 "2020-07-20T12:14:10Z")

</div>

Could you share the code for AddProductToCategory? May be, when I’m doing the sort index might be changing(just a thought)

---

<div class="post-metadata">

**Author:** ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)\
**Post date:** [July 20, 2020, 12:42pm UTC](https://forum.litium.com/t/what-is-the-best-way-to-link-newly-added-product-to-a-category/1567/4 "2020-07-20T12:42:18Z")

</div>

Assuming that the link can’t already exist and that you have `baseProduct` and `variant`.

```auto
var category = _categoryService.Get(categorySystemId).MakeWritableClone();
var link = new CategoryToProductLink(baseProduct.SystemId);
link.ActiveVariantSystemIds(variant.SystemId);
category.ProductLinks.Add(link);

using (_securityContextService.ActAsSystem())
{
	_categoryService.Update(category);
}

```

You can also add all variants at once, something like

```auto
category.ProductLinks.Add(new CategoryToProductLink(baseProduct.SystemId)
{
	ActiveVariantSystemIds = new HashSet<Guid>(_variantService.GetByBaseProduct(baseProduct.SystemId).Select(x => x.SystemId))
});

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [August 17, 2020, 12:42pm UTC](https://forum.litium.com/t/what-is-the-best-way-to-link-newly-added-product-to-a-category/1567/5 "2020-08-17T12:42:19Z")

</div>

Closed on August 17, 2020, 12:42pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
