LibrarySites.Banner

LINQPad and Sitecore 7

This post explains how to use LINQPad to test and debug LINQ statements with Sitecore 7.

If you use LINQ, you are probably already aware of LINQPad. It is a helpful tool quickly writing and testing LINQ statements. It's especially helpful if you're learning LINQ, or if it's something you don't use too often and need a refresher.

Sitecore 7 includes the LINQScratchPad, which is a great tool in its own right. But LINQPad gives you auto-complete (in the $40 pro version) and debugging, so it has some advantages.

There are different ways you can get the Sitecore 7 search provider to work in LINQPad. This post describes how I configured my system.

Configuring LINQPad

  1. Create the app.config file for LINQPad. The file should be located at C:\Program Files (x86)\LINQPad4\LINQPad.config.
  2. Add the following code to the config file:
    <?xml version="1.0"?>
    <configuration>
    </configuration>
  3. Add the configSections node from your Sitecore web.conf to the configuration node.
  4. Add the appSettings node from your Sitecore web.conf to the configuration node.
  5. Add the log4net node from your Sitecore web.config to the configuration node.
  6. Add the contents of the ConnectionsStrings.config file to the configuration node.
  7. Add the sitecore configuration to the configuration node. The configuration can be read using http://[host]/sitecore/admin/ShowConfig.aspx.
  8. Save the config file. 

Your config file should look something like the following (almost every setting has been removed for simplicity):

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="sitecore" type="Sitecore.Configuration.ConfigReader, Sitecore.Kernel" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, Sitecore.Logging" />
  </configSections>
  <connectionStrings>
    ...
  </connectionStrings>
  <appSettings>
    ...
  </appSettings>
  <log4net>
    ...
  </log4net>
  <sitecore xmlns:patch="https://www.sitecore.com/xmlconfig/" database="SqlServer">
    ...
  </sitecore>
</configuration>

Using LINQPad

  1. Navigate to Query > Query Properties (or press F4).
  2. Add references to all the assemblies used on your Sitecore server.
  3. In the Language dropdown, select C# Statements(s).
  4. Enter your code. For example: 
    var index = ContentSearchManager.GetIndex("sitecore_master_index");
    using (var context = index.CreateSearchContext())
    {
      Console.WriteLine(
        context.GetQueryable<SearchResultItem>()
          .Where(item => item.Name == "Home")
            .Where(item => item.DatabaseName == "master")
              .Where(item => item.Language == "en"));
    }
  5. Execute the query!

LINQPad

Troubleshooting

When you execute your query, you may get one or more of the following exceptions. These exceptions indicate the LINQPad.config file is not correct.

  • Timeout expired - The Sitecore server cannot be reached. One cause is that the database server cannot be reached. Make sure the hostname in the connectionStrings section of LINQPad.config does not include "localhost" unless Sitecore is running on the same machine as LINQPad.
  • ConfigurationErrorsException - LINQPad will display the offending line number. Often times the problem is an invalid character in the config file. Things such as ” and “ and ’.
  • The directory name C:\App_Config\Security is invalid - The path to the a file cannot be resolved. For example, I needed to change the path /App_Config/Security/Domains.config to Z:\Sitecore\Website/App_Config/Security/Domains.config.
  • The Role Manager feature has not been enabled - Remove the ApplyOutboundIndexFilter processor from the indexing.filterIndex.outbound pipeline. When running inside Sitecore, this processor removes search results the current visitor does not have authorization to view. From LINQPad, this sort of processing isn't needed.
  • No results - No exceptions are thrown, but you always get an empty GenericQueryable<T> object. Make sure the IndexFolder setting in the config file is pointing to the folder where the index files are stored. For example, I needed to change the path C:\Sitecore\Data\indexes to Z:\Sitecore\Data\indexes. 

Whenever you change LINQPad.config, you must restart LINQPad in order for the changes to take effect.

Related Links

This post depends on tips from the following blogs:

  • Any thoughts on how to get LinqPad to work when using Solr for search?

  • Hey Adam,  Took a stab at getting this working with a Solr implementation.  Got things working using these steps: www.paragon-inc.com/.../using-linqpad-with-sitecore-7-and-solr

  • I tried to use the LINQPAD and configured all the setting as mentioned but I am getting the following error after running the code in result section.  Could not read Sitecore configuration.  Message Could not read Sitecore configuration.  Data (0 items)    InnerException null     StackTrace    at Sitecore.Configuration.Factory.GetConfiguration()    at Sitecore.Configuration.Factory.GetConfigNode(String xpath, Boolean assert)    at Sitecore.Configuration.Factory.CreateObject(String configPath, String[] parameters, Boolean assert)    at Sitecore.Configuration.Factory.CreateObject(String configPath, Boolean assert)    at Sitecore.ContentSearch.ContentSearchManager.get_SearchConfiguration()    at Sitecore.ContentSearch.ContentSearchManager.GetIndex(String name)   Source Sitecore.Kernel  HResult -2146233079  

  • My config doesn't contain ApplyOutboundIndexFilter processor but  i got 'The Role Manager feature has not been enabled' error message, any idea how to avoid this error?

  • Hi Andras,  I would double-check your config file. The only time I've seen that error message is when that processor is included in the pipeline.  Another option is to use the LINQPad driver for Sitecore that I wrote about: www.sitecore.net/.../LINQPad-Driver-for-Sitecore.aspx

  • hi I am getting error The type initializer for 'Sitecore.Diagnostics.LoggerFactory' threw an exception.     Message Object reference not set to an instance of an object.   StackTrace    at Sitecore.Diagnostics.Log.Error(String message, Exception exception, Type ownerType)    at Sitecore.Diagnostics.Log.Error(String message, Type ownerType)    at Sitecore.Configuration.Factory.LoadAutoIncludeFiles(ConfigPatcher patcher, String folder)    at Sitecore.Configuration.Factory.LoadAutoIncludeFiles(XmlNode element)    at Sitecore.Configuration.Factory.GetConfiguration()    at Sitecore.Diagnostics.LoggerFactory..cctor()