Saturday, September 23, 2017

Sitecore Commerce 8.2.1 -How to rebuild all catalogs

Here are simple steps to rebuild the commerce catalog.

Please check that your local catalogue service is up and running.


Sample function to run the rebuild.

public TaskResults Run()
        {
            var context = CatalogContext.Create(new CatalogServiceAgent("https://tests.csservices.dev.local/Test_CatalogWebService/CatalogWebService.asmx"));

            var rebuildProgress = context.RebuildAllCatalogs(true);

            // 30 second refresh interval.
            const int refreshInterval = 30000;
            // Monitor the progress of the import operation.
            while (rebuildProgress.Status == CatalogOperationsStatus.InProgress)
            {
                System.Threading.Thread.Sleep(refreshInterval);
                rebuildProgress.Refresh();
                Log.Info(string.Format("Rebuilding {0} in progress...", rebuildProgress.CatalogName));
            }

            if (rebuildProgress.Status == CatalogOperationsStatus.Failed || rebuildProgress.Status == CatalogOperationsStatus.CompletedWithErrors)
            {
                foreach (var error in rebuildProgress.Errors)
                {
                    Log.Error(string.Format("Error message: {0}", error.Message));
                }
            }

            if (rebuildProgress.Status == CatalogOperationsStatus.Failed)
            {
                return TaskResults.Fail;
            }


            return TaskResults.Succeed;
        }

No comments:

Post a Comment