Sitecore Item access based on external data

Sometimes you might want to disable read access on items in your website based on something that doesn’t reside in Sitecore, like for example external data that comes from your own database. We have run into a situation where we had purposely chosen not to use the Sitecore role provider for several reasons and found a way to still apply the necessary security checks to make sure some items were not visible for all users on our portal. Our real Sitecore user was extended with some roles in our own database and we wrote an ItemProvider that would deal with this custom scenario. The methods that override the base implementation have been decompiled and slightly adjusted to serve our needs:

And the following configuration patch:

You can see that we wrote an extension on the ‘Item’ to find out if a user has access to this item (with custom logic). You can implement your own logic that will decide if the item should be returned when using the Sitecore API to fetch items. This provider will make sure that the functionality hooks in to the regular calls you make to retrieve items.

Improving performance with custom cache keys in Sitecore

Out of the box Sitecore offers you a considerable amount of usable cache keys to improve the speed of pages served from your platform. The standard provided cache keys are quite powerful in optimising the user experience. The image below shows how we can setup a rendering to be cached based on the page it’s served at:

We selected the checkbox ‘Cacheable’ so the rendering knows it can be served from cache. With no other options selected this rendering will be rendered once from the Controller logic and View and from that point on will be served from the cache as it was rendered the first time. This is cool for renderings that only have one appearance like for example a logo on a single website platform. When you want to start varying the renderings based on other parameters like ‘data’ (the page the data is displayed at), you will notice that Sitecore generates different cache keys to serve this data.

For example:

  • We have a ‘ImageSlider’ (rendering) with the options ‘cacheable’ and ‘vary by data’ activated.
  • We place this rendering on 2 pages in Sitecore.
  • We select the same datasource for both renderings, called ‘Slider1’ with the appropriate data we want to display

Now on those two pages the rendering is placed on, Sitecore will generate a cache key looking something like this:
#ImageSlider_#data:/sitecore/content/websitename/SharedContent/ImageSliders/Slider1

Sitecore now renders this ‘ImageSlider’ once and for the other page it generates the same cachekey, resulting in serving the same html from cache.

For one of our customers we run a multi-site setup with some customisations regarding datasources. You can read more about our customisation here and specifically check ‘Queryable Datasource’. Because we made a modification to Sitecore, in our case the ‘vary by data’ option was not working as Sitecore doesn’t recognize our datasource as a correct datasource. This resulted in renderings that all got cached on the same key, even though we gave them our own custom datasource (which was valid for us).

Example:

With the following datasource:

Which enables us to setup standard templates for websites on our multi-site platform with queryable datasources, which can be very powerful.
Now with this customization we also needed our own specific variable datasource option to enable caching to the footerblocks:

Here you can see I created our own cache key called ‘VaryByDataSource’ which will retrieve the correct datasource and add it to the cache key and in turn enables us to cache this rendering. This is just an example of a custom cache key, and we also created some other cache keys like ‘VaryByWebsite’, ‘VaryByProduct’ and ‘VaryByCategory’. The vary by website cache key is very powerful when you want to make sure a rendering is always uniquely rendered on a website. Maybe your own platform requires some of these unique custom caching keys? It’s very easy to create your own actually!

Step 1:

Expand the inherited ‘Cache’ template with ID ‘{E8D2DD19-1347-4562-AE3F-310DC0B21A6C}’. You can create some new checkbox fields there, that will make sure you can select the option on all of your Controller Renderings.

Step 2:

Create a new class called ‘GenerateCacheKey’ somewhere in your solution. Make sure it’s built and available in the assembly folder of your application. Below you find an implementation (dev) that we created. The VaryByWebsite could be useful in your own scanario.

Step 3:

Override the ‘GenerateCacheKey’ class that Sitecore MVC is using to generate a cache key for a rendering:

You will notice, that for custom logic, it will be a very good idea to really think ahead about which factors have to modify the cache keys (for a rendering) to make sure you can benefit from caching without sacrificing functionality.

 

Don’t forget this when upgrading to SC9

For one of our clients we have been upgrading the Sitecore environment from 8.1 (rev. 160519) to 9.0.1 (rev. 171219). The upgrade process was doable after reading the upgrade documentation thoroughly, and just like every upgrade there are some things to look out for when upgrading your solution.

Dynamic Placeholders

Dynamic placeholders are now integrated with Sitecore and we don’t need to install extra modules anymore to make use of them. You can easily remove the reference to the old dynamic placeholder module (remove assemblies and config) and start using the Sitecore ones by just using the Sitecore Html helper and rendering a placeholder just like that. One thing to keep in mind is that the functionality is extended, and it’s now possible to render multiple placeholders by adding an integer value to the placeholder key itself: row_{FDDF4C6C-C1B8-5C6D-2B41-DB83D241634C}-2

This example will render a row placeholder with x instances of that row which can be used to nest other renderings in. Pretty handy when you need to setup a page template with 5 rows, as you don’t have to add 5 row to the page by hand. When upgrading your instance this however can be an issue if you are already using dynamic placeholders. The new format as described above is a little different then the older format. More details about this can be found here. The powershell script that is provided can be used to transform everything to the new format, but make sure that any customizations done to either the rendering of a dynamic placeholder or overall rendering of renderings to the page might need some little tweaks to the powershell script. In our case we had created a custom way of rendering a navigation element, so running this powershell would get us into trouble. Therefor we created our own script, which was suitable for this particular case.

Configs files rearranged

Sitecore has done something great again, the config files have been rearranged and are structured in a much more logical way then before. This has multiple reasons. It’s very much easy to find the relation between the functionality Sitecore offers and the folder structure in the app_config folder. For example ‘Launchpad’ now has it’s own folder, so all processors and pipelines related to the launchpad are now residing together in one folder. Offcourse only changing the way configuration files are stored doesn’t automatically mean that all configuration that can be done to an element are now done on one place as this also depends on having everything patched and sorted out in the right way, but it’s clear that Sitecore is going for a much more module / functionality based approach. The more you have already chosen for a config patching approach when customizing Sitecore, the easier it is to adept to this new way of storing the configuration by Sitecore. When merging and setting up while upgrading you will notice that some of the somewhat bigger configuration files might have been split up in 3 or more new different configuration files. Just make sure to use the right merge tools and understand the new structure before starting the merge.

MongoDB

In the past Sitecore has been forcing us to make use of MongoDB for the saving of our xPerience data (interactions, contacts, devices etc.) but with the feedback they got from the community a new approach has been  taken. In Sitecore 9 and with the first update there is no provider implemented for saving xPerience through MongoDB. This is a rather fundamental change and might cause some issues for the ones that have embraced MongoDB (like us). The reason being that Sitecore doesn’t support MongoDB for xPerience data yet, but seems to already ship the newer MongoDB drivers with a clean Sitecore 9 install. Therefor if you are using MongoDB in you application to for example store some of your application’s data you will have to upgrade to the newer MongoDB drivers, to already prevent any mismatches in the comming release of Sitecore 9 that will support MongoDB again.

Queryable datasource

Queryable datasources can be used to make sure that datasources that can be chosen for renderings can be queried from a certain context. For example:
I am placing a rendering on a website and I want the content to be chosen from a shared content folder under that particular website, so I create the following queryable datasource: ancestor-or-self::*[@@templatename=’Website’]/Shared Content/Content/*

This query will make sure that content can be picked from the ‘Content’ folder that resides under the Shared content folder of the website we are creating a new rendering on. This is cool, cause this allows us to have 1 rendering defenition that will be able to be used on more then just 1 website, and we don’t have to bother our content editors with the choice of which shared content folder the content should be picked from (as the queryable datasource will make that choice).

We ourselves found this way of working really cool and actually extended Sitecore to also be able to work with Queryable datasources in the actual datasource of a rendering itself. This means that we have the option to give our content editors the freedom of choosing content, but we can also create standard versions of pages that will have predefined renderings on the page that have queryable datasources prefilled.

For example: I have 5 footer blocks that I want to setup on standard values of the ‘Content’ page template. Every time a page gets created those blocks should be on the page. I have a multisite setup, so I cannot set the content of this footer blocks to one particular datasource as this would cause the links in that footerblock to always point to one website and I want the foolterblocks to be specific to the website itself. Instead of just creating the blocks with a set datasource i create a query like this:

2018-02-23_1141

Now when this rendering is added to the page it will look for the FooterBlock1 item under the shared content folder of the current website. We created our own MVC Controller base class that will deal with this and has a method implemented that will retrieve the datasource item:

Cool things, but in Sitecore 9 the way a datasource gets validated might cause the rendering from being displayed! In the  Sitecore.Mvc.ExperienceEditor.Pipelines.Response.RenderRendering.AddWrapper class a datasource validator is validating all the datasources. If a datasource was not validated correctly this AddWrapper class will abort the pipeline and give the following warning ‘”‘{0}’ is not valid datasource for {1} or user does not have permissions to access.” We solved this by using the AddWrapper as a base class in a new processed that we patch instead of the AddWrapper class and introduced our own Datasource validator based on the interface ‘IDatasourceValidator’.

Config:

AddWrapper method:

Datasource validator:

This made sure that renderings would be rendered even if the datasource was invalid according to Sitecore, but was valid according to our own customization (using the query: prefix on a datasource in a rendering)

Core database

We had some issues with the core database after upgrading to Sitecore 9. This may have several causes, for example a not well performed SQL script that should be run in one of the upgrade steps. For now we have just solved this the most easy way by just placing a new Sitecore 9 Core database and migrating any customizations we have done by creating Sitecore packages between the old (Sitecore 8.1) core and the new Sitecore 9 database. Offcourse this is not enough, as also users / roles and some other key things should be taken into account when migrating data from the core database, but for now this was the simple way to get ourselves ready to start fixxing the really important issues like code breaking changes and missing renderings.

Well, this concludes it all for now, I hope there are some things that will eventually help out other people that come along any of the issues described above, and I wish you all happy Sitecore-ing!

 

 

 

 

 

 

 

Extend Sitecore to allow Multisite Aliases

A while ago I released our first version of a module that is available on the Sitecore Marketplace to enable Sitecore to work with aliases in a multi-site environment.

The idea is that multiple websites can use the same alias names.

For example:
http://site1/alias1 -> points to item ‘/sitecore/content/site1/item/itemlvl2/item’
http://site2/alias1 -> points to item ‘/sitecore/content/site2/item/itemlvl2/item’
http://site3/alias1 -> points to item ‘/sitecore/content/site3/item/itemlvl2/item’

Sitecore out of the box supports only having a single aliasname across your complete environment. I have recently implemented this solution for one of our customers and extended the functionality to completely disable the standard Sitecore aliasresolver (so it doesn’t even call the base.Process method anymore). I also wrote a simple layout that you can add in your solution to convert any existing alias items so that they will work in the new multi-site strategy.

What the script does:

  • Creates alias folders under all of the website’s (from the sitedefinitions)
  • Checks all existing alias items and copies them to the newly created alias folders (based on the Target Item of the alias item). This required some logic to determine the root site for an item, but this has been built in aswell.

From then on the custom AliasResolver – that is patched from a config file which is placed in the /app_config/include folder – will resolve the items from the alias folder directly under the website instead of the System/Alias folder Sitecore normally uses.

Why is my time not displaying correct after upgrading to 8.1?

I upgraded one of our Sitecore solutions from Sitecore 6.2 (rev. 100507) to Sitecore 8.1 (rev. 151207). After the upgrade we noticed that the display of time on the website was slightly off. Instead of showing the GMT + 1 time it showed the UTC time (which is 2 hours off in our case). This caused that all newly created items (wheter it be reactions on news or news items itself) showed the wrong time. An item created on 11-04-2016 – 11:04 would be shown as 11-04-2016 – 09:04. As you might guess this wasn’t really what our customer was looking for and I haven’t stumpled upon any tips regarding this issue in the upgrade guide while upgrading the solution.

Now I have decompiled the Sitecore.Kernel and read some other posts regarding this issue I would like to share my solution to this problem. As per Sitecore documentation provided in the Sitecore.config file I looked for the Timezone I would like to use on msdn:

MSDN Article – Timezones

In my case I chose to use the “W. Europe Standard Time” so my settting in the sitecore.config file looked like this:

Now this seems all nice and ready to roll, but you aren’t done yet. If you are using for example “Sitecore.Statistics.Created” as your DateTime object and just format this date and .toString() it in any format with a time specification you will not yet see the correct time displayed. I therefor used the Sitecore.DateUtil.ToServerTime() method, which uses the ServerTimeZone setting we just specified to convert between the UTC and server time.

This results in the right date / time being shown on your website.

Hope you will find this article useful!

 

Queryable datasource on sublayouts (presentation details)

Offcourse we all know that there is a way to set queryable datasources to sublayout definitions in the Sitecore database to help us select appropriate data for our renderings. This can be done using a module that you can find on the marketplace:

http://sitecoreblog.blogspot.nl/2012/06/datasources-queryable.html

This module – when you specify a sitecore query – changes the rendering datasource to a new location. This can be very handy when you work with the page editor and want content of specific items to be placed underneath the content item itself. You can then for example place a “/content” folder underneath your items to hold the specific content that will be displayed on your item.

What I’ve noticed during development of a new Sitecore website that uses the page editor is that it can be very handy to have a complete page setup for our content editors so they see how a page should look. Giving the editors the ability to change existing layout is generally more appreciated than giving them a blank page that they have to fill from scratch everytime they add a new page / content item.

To achieve this goal we create a new “branch” for this set of items that will help our editors to quickly fill a new content item. This branch could for example have the following structure:

2014-08-04_1224

What you see here is a branch on which we have filled specific content items and other data template based items that will be used in our branch. When the content editor creates a new item from this branch, we wanted the presentation details of this branch to have those items preselected without having them  central /in a shared folder somewhere in Sitecore. Every branch would then create this structure and the item created would have it’s own items underneath that would be used in the presentation. This offcourse could be done with the same concept as we’ve seen before in the Queryable Datasource. We went to see how Sitecore actually determines what item to present in a sublayout and found out we could play a little with the “ProcessContextQuerySource” pipeline.

We did the following: (after decompiling the Sitecore.Kernel.dll)

  • Create a new processor definition in the config that replaces the ‘Sitecore.Pipelines.InsertRenderings.Processors.AddRenderings, Sitecore.Kernel’ pipeline.

  • Create a class that inherits “Sitecore.Pipelines.InsertRenderings.Processors.AddRenderings” and implement it using the following code:

Now when sitecore is adding renderings to the page and finds a rendering that has a query: in the datasource  it will be picked up as a special rendering. This class will extract all renderings with “query:” in it’s datasource and sets the appropriate datasource for those renderings. Pretty neat huh?

You can now add a sublayout on your branch item with the following query to set default text:

2014-08-04_1225

When the item is created from the branch it will initially point to the item underneath itself, but the content editor is free to change to another datasource if needed. When you click the ‘set associated content’ button the usual stuff will happen, making sure that you can set different content as datasource.

I do realise that this concept is taken from the queryable datasource, tho I think that this might be useful to setup a branch with item specific data underneath the content item itself (without having to create shared content items somewhere to point at from the datasource).

That’s it for now!

Lucene MaxClauseCount error after upgrading to Sitecore 7.2 (rev. 140228)

After upgrading your solution from an early release of Sitecore 7 (ie. 7.0 / 7.1) you might get the following error:

Message: maxClauseCount is set to 1024
Source: Lucene.Net
bij Lucene.Net.Search.BooleanQuery.Add(BooleanClause clause)
bijLucene.Net.Search.MultiTermQuery.ScoringBooleanQueryRewrite.Rewrite(IndexReaderreader, MultiTermQuery query)
bijSitecore.ContentSearch.Linq.Lucene.Queries.SpanWildcardQuery.Rewrite(IndexReaderreader)
bij Lucene.Net.Search.Spans.SpanFirstQuery.Rewrite(IndexReader reader)
bij Lucene.Net.Search.BooleanQuery.Rewrite(IndexReader reader)
bij Lucene.Net.Search.BooleanQuery.Rewrite(IndexReader reader)
bij Lucene.Net.Search.IndexSearcher.Rewrite(Query original)

You can’t solve this by adjusting the setting:

in your Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config file, but by adding the following lines of code to your SaerchProvider:

Somehow adjusting the setting in your config file won’t have any effect.