Nelissa Viera (Customer) asked a question.

How to Invalidate/Refresh Count after custom association removal in Sitefinity?

We have a custom process where product-to-catalog/category-node associations are removed either in single or bulk mode. The association is removed successfully (record is deleted), and the Product module reflects this correctly.

However, the relationship count shown in the Catalogs module still includes the removed associations. The old count remains until a full refresh/reload scenario triggers recalculation (where the count is requested and refresh the value correctly).

Expected behavior is after removing associations, the Catalogs module count should be recalculated/invalidated and display the updated value.

Question:

Is there a specific Sitefinity API/event/service we should call to invalidate or recalculate the category node association count (the same way Sitefinity does when deletion is performed internally in its standard flow)?

 


  • Steve McNiven-Scott (https://www.sitefinitysteve.com)

    Are you recalculating on modify with the API?

     

    The counts are cached. Sitefinity stores it in sf_taxonomy_statistic (the TaxonomyStatistic entity), with one row per taxon, item type, provider and lifecycle status, so Master and Live have separate counts. Sitefinity keeps that counter up to date when a change goes through the content manager that owns the item. If you remove a taxon from a product's taxonomy field and call manager.SaveChanges(), the platform compares the field's old and new values and adjusts the count by the difference. If your process deletes the association another way the record goes away and the counter never hears about it.

     

    1. TaxonomyManager.RecalculateStatistics(typeof(Product)); // all providers
    2. TaxonomyManager.RecalculateStatistics(typeof(Product), new[] { "OpenAccessProvider" }); // specific providers

    Is that what you mean?

     

    ...if you do it all from the API though it SHOULD BE calculating on it's own.

  • Nelissa Viera (Customer)

    Our process does not use the manager.SaveChanges() method it uses the manager.DeleteDataItem() and manager.RecycleBin.MoveToRecycleBin. In this case should I need to call TaxonomyManager.RecalculateStatistics(typeof(Product)); after the relation is deleted to recalculate the count? Because It is a child dynamic type.

    • Steve McNiven-Scott (https://www.sitefinitysteve.com)

      Recycling and then deleting with DeleteDataItem should lower the count by itself, but only once the change is committed through the dynamic module provider. How do you commit it, if not with SaveChanges()?

       

      Can you share the code?

      • Nelissa Viera (Customer)

        Our bulk delete extension uses DynamicModuleManager.GetManager(providerName, transactionName) and commits via TransactionManager.CommitTransaction(transactionName) after DeleteDataItem / recycle bin operations.

        So the transaction is committed through TransactionManager, not through direct manager.SaveChanges().

      • Steve McNiven-Scott (https://www.sitefinitysteve.com)

        I checked this against the 15.4 assemblies. Committing with TransactionManager.CommitTransaction is fine, because DynamicModuleManager updates the category counts during that commit.

         

        There's one gap.

         

        If DeleteDataItem runs on an item that wasn't moved to the recycle bin first, Sitefinity leaves the count as it was. Moving the item to the recycle bin first, in the same transaction, fixes that.

         

        If you also want a recount, pass the type that has the category field (your child type, not Product), and only run it after a permanent delete. The recount includes items still in the recycle bin.