Sunday, February 28, 2016

Solr Index rebuild for single Item (Document) in Sitecore

There are many live scenarios in sitecore application where we need to update the index to reflect the changes on website (UI). As sitecore recommended we use solr/lucence search index to play with huge data or search.
Some time we need to update some items or particular one item to update the index file.
So here is the way to update the single item in index file:
1.       Go to sitecore
2.       Go to developer tab: by default its hidden to show a developer tab, right-click a tab, and then select the tab to show > developer.
3.       Or go to ribbons >  my toolbar >Customize>Add Re-index Tree under indexing tools

4.       Now select the item which you want to rebuild the index
5.       Click on Re-index tree button > it will re build the index of selected item and its descendants.

Appreciate  to some comments/suggestion 

Happy Site-core..


Wednesday, February 24, 2016

Publish items only if version available in target database

Publish items only, if available in the target database.

One of my client requirement was to publish only those items which have version available in the target database, if we found any item which doesn’t exist in target database or don’t have a version in current language then don’t publish that particular item.
So here is the solution:

Override the publish:itemProcessing

Add below config file in App_config>Include>ItemProcessedEvent.config 

<?xml version="1.0"?>
    <events timingLevel="custom">
      <event name="publish:itemProcessing">
        <handler type="Namespace.className, " method="OnItemProcessing" />

Create the class with desired name and add below method: 

  public void OnItemProcessing(object sender, EventArgs args)
                var argsProcessing = (ItemProcessingEventArgs)args;
                var context = argsProcessing.Context;
                Assert.IsNotNull(context, "ItemProcessedIndexingEvent.OnItemProcessing: Cannot get PublishItem context");

                var sourceItem = context.PublishHelper.GetSourceItem(context.ItemId);
                var targetItem = context.PublishHelper.GetTargetItem(context.ItemId);

               if (targetItem == null || (sourceItem.Database.GetItem(sourceItem.ID, Sitecore.Context.Language).Versions.Count > 0 && targetItem.Database.GetItem(sourceItem.ID, Sitecore.Context.Language).Versions.Count <= 0))
                        argsProcessing.Cancel = true;

            catch (Exception ex)

Solution: if you publish site, folder with sub items or single item, the publishing will check if item and language version is available in target database, then only it will publish it otherwise that particular item will skip from publishing.
Please feel free to provide your valuable input/feedback

Happy sitecoreJ