Retrieving site information in Sitecore

Sitecore provides functions to get the root path and start item for the current context. So when working with multisite solutions you should always get a path relative to the Sitecore.Context.Site settings:

Response.Write("Current site: " + Sitecore.Context.Site.Name + "<br/>");
Response.Write("Database: " + Sitecore.Context.Site.Database.Name + "<br/>");
Response.Write("Content start path: " + Sitecore.Context.Site.ContentStartPath + "<br/>");
Response.Write("Home node: " + Sitecore.Context.Site.StartPath + "<br/>");
Response.Write("Language: " + Sitecore.Context.Language.GetDisplayName() + "<br/>");

The code above would give me the following output for the site at

Current site: website_1
Database: web
Content start path: /sitecore/content/website1
Home node: /sitecore/content/website1/home
Language: English : English

Finding a Sitecore item by ID using a Solr query

By default the _group field contains the Sitecore ID for a specific item. To search for an item with an ID of {799539AB-3495-4D40-9B2F-A394A87960D6} use the following Solr query:


Please note the ID is not stored in the group field as a standard GUID. For the search to work hyphens need removing and converting to lower case.

However this will only work for an item with a single version and language. As Sitecore uses the same item for each piece of content, regardless of the number of versions or languages, the above query may return multiple results.

In this case we need to use the _uniqueid field to return the exact item representing a specific version and language. In this example we change the Solr query to:


Unpublish a Sitecore item

Any regular Sitecore users knows there are many ways to publish an item. However it’s not always obvious how to “unpublish” an item. Its actually quite straight forward.

1. Navigate to the item you want to unpublish using the Content Editor. From the Publish ribbon select the Change button:

In change dialog, make the item unpublishable by deselecting the Publishable checkbox:

The content editor will display are warning informing you what’s about to happen.

The final step to actually publish the item and it will disappear from the front-end but remain in the master database. This may sound strange, but as the item is already published we need to publish again to remove it from the web database.

Sitecore Habitat Installation Issues

I’ve recently attempted to setup a Sitecore Habitat solution by following instructions from the GitHub site.

This was not as straight forward as I’d hoped. The following are some issues I had to resolve along the way.

Web Forms For Marketers

Ensure this is installed correctly and working. Although I had installed the module and it appear to be successful, when I checked System/Modules/Web Forms for Marketers items were missing. The problem didn’t become apparent until Unicorn tried to sync items relating to WFFM.

I also discovered although it’s not mentioned in the Habitat documentation MongoDB is required for WFFM to work correctly with Habitat.


After installing NodeJS I ran npm install from the root of the Habitat repository as instructed. However this kept failing while trying to install node-sass with a 404 error. Running npm install node-sass to manually install the package before running npm install resolved the issue for me.

Editing repository files

Make sure any files you’ve been editing are not open by another application

Unicorn Sync

Although the sync process should work fine through Visual Studio Task Runner. I found it more successful to run it through a command prompt using gulp.\


Even after a success npm install I received a gulp command not found error when running gulp from a command prompt. Running npm install –global gulp-cli in the repository root resolved the problem.


Sitecore items not visible on website

Sometimes you may find an item appears in both the master and web database’s. But when you navigate to that item on the live website site either a 404 returned, or in the case of a component it’s not visible.

This could be because the item has publishing restrictions in place. There’s a few places to check:

  1. Log into Sitecore.
  2. Open the Content Editor.
  3. Select the View tab and ensure Standard Values is checked.
  4. Navigate to the offending item using the content tree and select it.
  5. Using the right panel scroll down to the Publishing section.
  6. Check the dates are either empty or valid for displaying the item as of today.
  7. At the bottom of the Publishing section there is a Never Publish checkbox, make sure this is unchecked.

If workflow is in place it might be worth checking the Workflow selection for this item. The state field will highlight where in the workflow this item is. However if the item is’t approved it shouldn’t show in the web database.

If the above doesn’t help its worth republishing the item or its parent..

Sitecore generating Overlapping onDeckSearchers=x warnings in Solr logs

I’ve recently had an issue where every 10 minuets or so a entry would appear in the Solr logs. This was only an issue for the analytics core. After reading various articles and try different Solr configurations I failed to resolve the problem.

However after a chat with Sitecore Support the issue was resolved by a quick change to our Sitecore environment. Our production environment consists of four separate Sitecore instances, two delivery, one authoring and one processing server. By default Sitecore uses a Timed Index Refresh Strategy to update Solr’s analytics core. By having four environments pointing to the same Solr instance, the analytics core was being updated four times simultaneously.

When a processing server is being used only that should update the analytics core. In an environment without a processing server it would the possibility of authoring server.

To resolve the issue the processing server was left with the default Timed Index Refresh Strategy. Automatic updating of the analytics was disabled on the remaining three environments by changing the strategy to manual.

The strategies are set in the Sitecore.ContentSearch.Solr.Index.Analytics.config file. The default is:

<strategies hint="list:AddStrategy">
   <timed type="Sitecore.ContentSearch.Maintenance.Strategies.TimedIndexRefreshStrategy, Sitecore.ContentSearch">
      <param desc="interval">00:01:00</param>

To use a manual strategy remove the above from the config file and add:

<strategies hint="list:AddStrategy">
   <strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/manual" />

Rather than updating the config file a better solution would be creating a patch file. This way if the config file is replaced during an upgrade the config change will still be applied.

OctoPack NuGet versioning order

NuGet packages have version numbers. When you use OctoPack, the NuGet package version number will come from (in order of priority):

  1. The command line, if you pass /p:OctoPackPackageVersion= as an MSBuild parameter when building your project.
  2. If the [assembly: FileVersion] is the same as the assembly: AssemblyInformationalVersion, then we’ll use the [assembly: AssemblyVersion] attribute in your AssemblyInfo.cs file
  3. Otherwise we take the [assembly: AssemblyInformationalVersion].

Querying the Sitecore Reporting Database (xDB) Directly

While Sitecore provides comprehensive analytics from the main dashboard, there may be a requirement to interrogate the analytics data directly in your code. To do this we need to make use of Sitecore’s ReportDataProviderBase and ReportDataQuery classes. These provide access to the reporting database using the parameters set in the Sitecore.Analytics.Reporting.config.

Sitecore stores all it’s queries in items based in the ReportQuery template. As of Sitecore 8.1 these can be found at the following location Master/Web > sitecore > system > Settings > Report Queries.

First create a new ReportQuery item by duplicating an existing one. Open your new item and update the Query field with your own SQL query. Once complete save and publish taking note of the item ID.

Next create a method to run the query, below is a basic example:

        public void GetReportingData()
            if (!String.IsNullOrEmpty(searchTerm))
                using (var context = Index.CreateSearchContext())
                    // By using the ReportDataProviderBase and ReportDataQuery Sitecore is instructed to use the Reporting (Analytics database)
                    // defined in the connectionStrings.config

                    // Create ReportDataProviderBase factory using connection info from Sitecore.Analytics.Reporting.config
                    var provider = (ReportDataProviderBase)Factory.CreateObject("reporting/dataProvider", false);

                    // Load SQL from Sitecore ReportQuery item
                    Item dataSourceItem = Context.Database.Items[ID.Parse("{id of ReportQuery item created above}")];
                    var dataSQLQuery = dataSourceItem.Fields["Query"].Value;
                    var query = new ReportDataQuery(dataSQLQuery);

                    // Run query using the FiltersFactory defined in the reporting node from the dataProvider node in the Sitecore.Analytics.Reporting.config
                    // in this case /sitecore/system/Settings/Analytics/Vendor Specific Filters/MSSQL
                    var response = provider.GetData("reporting", query, CachingPolicy.WithCacheDisabled);

                    // Populate list
            return list;

Sitecore MVC Ajax POST to Controller

Using AJax to post to a controller using Sitecore MVC is a bit different to standard MVC. The following is example on how to do this. The main difference’s are the URL and data properties. With standard MVC the URL would be the controller name and Action i.e /MyController/MyAction and data would contain any variables which need passing into the action method. With Sitecore MVC we set the URL property to the current URL and pass the controller name and action in the data property.

First the controller action method, the controller is called MyController

public JsonResult MyAction(string myData)
   return Json(...)

Next the Javascript

$(function () {

    $(".my-action-invoker").click(function () {
    var my-data = $(this).find(".my-data").html();

        url: window.location.href,
        type: "POST",
        context: this,
        data: { scController: "MyController", scAction: "MyAction", myData: my-data },
        success: function (data) {
            // do stuff
        error: function (data) {
            console.log("error", data);