Get items from Bucket in Sitecore

I need to get items from a big bucket.
For best performance you should get the items through Sitecore ContentSearch.

Here I have some example code how to get a selection of items from a bucket.
Let’s say I want to get items that has a specific Tag on a field, which in this case is a string type field. (You can of course change the queryable to whatever you like)

List<Item> ResultsItems = new List<Item>();
IIndexable index = new SitecoreIndexableItem(bucketItem);

using (var context = ContentSearchManager.GetIndex(index).CreateSearchContext())
{
   var results = 
     context.GetQueryable<T>().Where(x => x.TagFieldName.Equals("TagName"))).GetResults();
   
   foreach (var result in results)
   {
     Item item = result.Document.GetItem();
     if(item !=null)
     {
       ResultsItems.Add(item);
     }
   }
}

Now ResultsItems are filled with the item context of all the children of the bucket.

Note: I’m showing the code for demonstration purpose. Beware of potentially calling a GetItem() on potential thousands of items.
I would also recommend selecting the fields you are after with .Select(i => new { i.FieldA, i.FieldB}) (read more here) so that the query gets smaller.

Sitecore installation setup UI

Just had to make a quick post about this because apparently I’ve been living under a rock.
I’ve used SIF and setting up configs on my own every time I’ve setup a new sitecore instance.
Which now in hindsight has been a giving lesson to learn the basics.

I mean if you’re just after a quick install for an empty Sitecore instance this is golden.
So this is for all others that have missed this too.


You can download this at the normal Sitecore download section: https://dev.sitecore.net/Downloads/Sitecore_Experience_Platform/102/Sitecore_Experience_Platform_102


Sitecore shutting down – initialization error

The problem: All I could see in the log file was that error message “sitecore shutting down – initialization error“. This didn’t tell me much of where the issue was coming from except when Sitecore is spinning up.

So I took a look in the Event viewer to check out any logs there.
Open “Event Viewer” by clicking the “Start” button. Click “Control Panel” > “System and Security” > “Administrative Tools”, and then double-click “Event Viewer”.

Go to Windows Logs -> Application and you’ll see the latest log inputs at the top.

Click it and read the details. There I saw my problem.

It was a config file that was hooking into the initialize pipeline and had a faulty dependency. Whops. Fix that and bada boom, Sitecore starts to spin up again.

Add custom buttons to Dashboard/Launchpad

A neat way to execute or show some custom functionalities is to create a custom button for the Sitecore Dashboard/Launchpad (whatever you call it, I say Dashboard).
This is very simple and you can easily make the button either for example

a) open an aspx
b) show custom views
c) make an API request to one of your controller actions

Sitecore dashboard
Here i’ve added “My button” to the Control Panel section

Step 1

Switch to Core database by going to the Desktop view and in the lower right corner switch database. (Or enter the querystring &sc_content=core)
Then enter the Content Editor

Step 2

Locate /sitecore/client/Applications/Launchpad/PageSettings/Buttons and you will find the sections corresponding to the Dashboard/Launchpad

Step 3

Choose what section you want to put your button in and right-click the item (Tools = Control Panel) and add a new Launchpad-button

Step 4

Fill in the button as you want.
Text: The text for the button on the Dashboard/Launchpad
Icon: Icon for the button
Link: You can link directly to a Sitecore Item or a controller action within the sitecore api as i’ve done. (or your own api)
OpenNewTab: Clicking the button opens it in a new tab.
OpenIframe: Clicking the buttons opens an iframe.
PathResolver: Namespace and dll name

That’s it. It should now show up on the Dashboard/Launchpad

Replace non-english characters

I needed to replace swedish characters from string without removing the character for itemname.

With Sitecore you can use ItemUtil.ProposeValidItemName() but it removes the invalid characters.
I have a text like “Flöde” and I want it to be “Flode” and not “Flde” like Sitecores ItemUtil does.

Then I can use the following code:

byte[] b = Encoding.GetEncoding(1251).GetBytes("Flöde/");
string output = Encoding.ASCII.GetString(b);

string validItemName = ItemUtil.ProposeValidItemName(output);

________________________
output: "Flode/"
validItemName: "Flode"

The ItemUtil.ProposeValidItemName will give you a valid item name for Sitecore.
The string output would still contain possible invalid special characters that you don’t allow in the Sitecore config setting ItemNameValidation regex.

Custom caching with Sitecore Cache

Performance is key. We have all know this. Customers has very high expectations on fast solutions. And this is where caching is king.

There are many caching soultions out there today, like Reddis cache. You can also build your own layer if you’d like. Or you can use Sitecores.

Sitecore has their own caching framework that is used for Sitecore to get data faster. This can also be used for custom caches. So you can cache something simple as a string or even anonymous objects.

There are two types of cache you can use for customcaching.

Sitecore.Caching.CustomCache<string> and Sitecore.Caching.Generics.Cache<T>. One takes a string and one takes an object type.

Sample code

   var mycache = new Sitecore.Caching.Cache("test cache", 1024);
   mycache.Add("key", "data");

   var myType = new Type {Id = 1, Name = "Test"};
   mycache.Add("type", myType);

   //Get the value based on key
   var value = mycache.GetValue("type");

Read more:

http://sitecore-community.github.io/docs/documentation/Sitecore%20Fundamentals/Caching/

Sitecore Solr Sort Alphabetically

Just a short and quick blogpost. I got an issue with my alphabetically sort.
This was because my field was tokenized and of type text in Solr. So I had to make the field Untokenized and returnType string. Fixed!

With Sitecore you can pretty easy sort by Ascending or Descending with LINQ to Sitecore
Just write an OrderBy or OrderByDescending with your field like so:

switch (direction)
{
  case SearchOrderDirection.Ascending:
    return queryable.OrderBy(i => i.Title);
  case SearchOrderDirection.Descending:
  default:
    return queryable.OrderByDescending(i => i.Title);
}

REMINDER: The field should be UNTOKENIZED and returnType string.

Sitecore 10.2 Feature summary

Sitecore released the new version, 10.2 recently and I wanted to make a quick summary of some of the features that stood out to me.
For full Release Notes please check Sitecores doc here: 10.2 Release Notes

Decrepcated

  • Update Center is going away.
  • Support for Azure Search has been removed. They are going back to Solr after moving over to Azure Search in Version 10.0. Solr has been there through all 10.x but now their fully relying their internal indexes on Solr.

New features

  • EXM DDS role now supports containers. For fast deployment of a Dedicated Dispatch Server.
  • Solr version 8.8.2 is now the recommended version.
  • MVC components are now supported in Experience Edge. This is so projects can gradually go towards Headless and not have to throw away their existing projects.
  • Sitecore Identity Server 6.0.0 now runs on .Net Core 3.1. A big security update.
  • Use xConnect CLI to purge interactions you don’t need. Clear up space.
  • There is a new “Content Explorer” in Horizon which is a search and filter based way of finding items in Sitecore. This looks promising for future Headless endeavours.
  • Horizon now fully integrated with SXA.
    SXA now has Bootstrap 5 support, upgrades to Scriban templates and lots more.

Sort Sitecore Analytics tables

In Sitecore Experience Analytics you can see graphs and tables with data of visitor information such as pages they most navigate to, goal conversions etc.

By default these tables have no sorting set on a specific column. This in turn leads to intense scrolling through the result.

A sorted table. Learn how below

If you want to sort the tables by for example Visits in descending order, i’ll show you how. Same idea if you want to switch Visits for Page Views, Conversion Rate etc.

  1. Log into Sitecore and go to Desktop from the Dashboard.
  2. Switch to Core database
  3. Open Content Editor and navigate to /sitecore/client/Applications/ExperienceAnalytics/Dashboard.
  4. From here you need to find the tables you want to fix the sorting.
    As you can see in the image below, the tree in Content Editor matches the order in Experience Analytics.

5. Now open the coresponding nodes PageSettings and click the ListControl Parameters.
Find the fields KeysSortByMetric (the column) and KeySortDirection.

Save and refresh your Experience Analytics window and the table should now be sorted.

Link to Sitecore documentation for Dimensions and Metrics and what the they mean: Click

IndexWorker service shut down – rebuild

Has your IndexWorker service stopped?
To fix this we need to start the service and rebuild the _xdb index in Solr.

This is pretty straight forward and explained well in Sitecores documentation here: https://doc.sitecore.com/developers/91/sitecore-experience-platform/en/rebuild-the-xdb-index-in-solr.html

  1. You need two cores in Solr (read the sitecore doc I linked for this if you need to create the _rebuild core).
    – <name>_xdb
    – <name>_xdb_rebuild
  2. Next, in powershell, find your IndexWorker instance folder. i.e. on Sitecore 9.1.1 XP0 it’s:<path>\site.xconnect\App_Data\jobs\continuous\IndexWorker.
  3. Run .\XConnectSearchIndexer -requestrebuild

This will rebuild the index and swap the Cores with eachother. After it’s done you should see that your data pops up again in Xdb.
Good luck!