Saturday, October 15, 2016

#Sitecore Cache Clear Event for Multisite Solutions


Hi All,
In one of our project we have requirement for apply cache in our site. In our #Sitecore solution we have multiple Site-Nodes. Like –
Sitecore
                -content
                                -site-node1
                                                -home
                                -site-node2
                                                -home

In our sitedefinition.config I have 2 sites with the name –
      1)      Site1
      2)      Site2

I have applied cache on item’s Templates presentation. Now I need to clear cache.

To clear cache I have created a patch config. I used following event –

<sitecore>
    <events>
      <event name="publish:end">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
            <sites hint="list">
            <site>Site1</site>
            <site>Site2</site>
          </sites>
        </handler>
      </event>
      <event name="publish:end:remote">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
          <sites hint="list">
            <patch:delete />
          </sites>
          <sites hint="list">
            <site>Site1</site>
            <site>Site2</site>
          </sites>
        </handler>
      </event>
    </events>
  </sitecore>

This event clears the cache. But after some testing I found that after publishing only cache is clearing for Site2 not for Site1. To verify this, I opened the url http://hostname/sitecore/admin/showconfig.aspx and search for “publish:end” term, found the entry of Site2 only in cache clear event. Now this is the problem. Need to find a solution for this.

We need to open url http://hostname/sitecore/admin/cache.aspx every time to clear the cache, but that is not good practice. So after some digging on the google to find the solution. But finally I did it.
We need to add one more sites section in the same patch config file.
<sites hint="list">
    <patch:delete />
 </sites>

Following is the solution-

<sitecore>
    <events>
      <event name="publish:end">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
          <sites hint="list">
            <patch:delete />
          </sites>
          <sites hint="list">
            <site>Site1</site>
            <site>Site2</site>
          </sites>

        </handler>
      </event>
      <event name="publish:end:remote">
        <handler type="Sitecore.Publishing.HtmlCacheClearer, Sitecore.Kernel" method="ClearCache">
          <sites hint="list">
            <patch:delete />
          </sites>
          <sites hint="list">
            <site>Site1</site>
            <site>Site2</site>
          </sites>
        </handler>
      </event>
    </events>
  </sitecore>

After adding this sites section, I again opened the url http://hostname/sitecore/admin/showconfig.aspx and search for publish:end term and look what I saw, I could see both sites entries (Site1 and Site2). But I needed to verify this by publishing an item as well and it worked finally for both the sites.

Happy Coding :)




Sunday, June 19, 2016

System.Reflection.TargetInvocationException error when Rebuilding Sitecore Solr Indexes


Hi All,
We have a multilingual Sitecore site. When I was rebuilding the Sitecore indexes, I was getting a weird error that is
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.


Generally we click on cross button, and rebuild the index again, sometime it works. :p
But in my case it’s not working. So I dig into the issue. 

First of all I just opened the local solr url in browser like this http://localhost:8983/solr/
And then click on logging button on the left side and saw there were so many errors as shown in the below image.


By looking into the image, we can see that there are some language specific error. Like “zh” for Chinese, “ko” for Korean.

So now what to do?

Then after some searching I found that we have to do something with physical solr files. Now go to solr folder where you have configured solr for your project. Let’s assume you have configured solr with the name “project_master” and “project_web”. So follow the steps –    

      1) Go to “solr/project_master/conf” folder.
      2) Then you will find an xml file named “schema.xml” there.
3) Open this schema.xml file in notepad or so and search for “dynamicField” that is under fields section.

If you don’t find schema.xml file in conf folder then you need to follow following steps – 

       1) Go to sitename/Sitecore
       2)  Log into Sitecore with username and password.
 3) Then click on Sitecore button that is in left bottom corner.
       4) Then navigate to Control Panel, a screen will popup like following image.


      5) In this there are 2 links, then you need to click on “Generate the Solr Schema.xml file”.


      6) You will see there are 2 fields “Source File” and “Target File”.
      7) In the Source File you need to provide a default schema.xml file. You can download default schema.xml here.
      8) In the Target File provide the target folder, where you want it to be.
      9) Now you are ready to customize “schema.xml” file.

When you will open this schema.xml, you will find that lots of dynamic fields are there something like this –
<dynamicField name="*_t" type="text_general" indexed="true" stored="true" />

Note – Before making changes in schema.xml you need to take care of apache server. In your PC click on “Start” button and search for “Monitor Tomcat”, click on it. You need to click on “Stop service”, after that you can make changes in schema.xml file.

In these dynamic field search for “zh, ko”, and you will find that there are no entries with these names, so now we need to make entry for these language types like below image. Make sure you have entry for each language that is causing error.


What “*” means, it will include all the entries ending with e.g. “t_zh”.
There are several “dynamicField type” like text_general, string, boolean, int, date, location, tint, tlong, tfloat, tdouble, tdate, pint, currency, ignored, random etc. I am using “text_general” here.

It’s done, no. There is still something we need to do.

Start you Tomcat (Apache) service again by opening Monitor Tomcat.

Now you again need to go to Sitecore and rebuild your index again.

Finally it’s done now.

If you got error again, then you need to follow this process again then check the logging and make sure all language specific entries are there.

Happy Coding :)