Removing Unneeded Tags from RTE fields with Sitecore PowerShell Extensions

bacon-575334_1280

This week I had an issue where some items imported from another system into Sitecore had invalid HTML in some of their RTE fields. These tags were breaking the SXA page content component that was being used.

I needed to a quick way to remove these tags without having to re-import the content. As usual SPE came to the rescue again and I was able to correct 100s of items in seconds.

The script has a function which takes an item and field name as a parameter:

Strip-InvalidTags -item $item -fieldName "Details"

I’m getting a number of profile items and calling this function in a loop. The function uses HTMLAgilityPack to look for ‘Script’ tag nodes (but can easily be modified to pick up different tags or multiple tags). If the script finds the tags is strips them and then saves the modified html back to the field.

Here is the script that I created:

Hopefully it’s useful for others who might need to do this.

Thanks again to the community for some useful ideas: https://sitecore.stackexchange.com/questions/20795/using-powershell-extensions-to-remove-empty-p-tags-from-all-rich-text-fields/20802

Sitecore Sloth Module 1.1 Released

santa-helperA few weeks ago I wrote about how I updated Sloth to support Items as Resource files.  I’ve now got round to releasing this package on Nuget and creating Sitecore Packages for this too in case you want to install in that way instead. I’ve updated the Github readme with all the info too.

Sloth 1.1 supports being able to enable and disable the module under the settings here: /sitecore/system/Modules/Sitecore Sloth. Please ensure you enable the module and save the item after installing version 1.1.

sloth-enabled

 

 

 

 

 

 

Installing Sloth 1.1 for Sitecore 10.1+ Via Nuget

You can install version 1.1 of Sloth via Nuget like so if using Sitecore 10.1 or above:

Install-Package FluxDigital.Foundation.Sloth -Version 1.1.0

I’ve tested installing the Nuget package for Sitecore 1.1 and if installed correctly you should see the following files:

sloth-1.1-nuget-install

 

sloth-1.1-nuget-install-2

 

 

 

 

I haven’t created a Sloth 1.1 Nuget package for Sitecore 8-10 yet but I will do so when I get a minute.

Hope this helps and your enjoying using Sloth.

Please let me know of any issues you have and I’ll try and resolve them ASAP.

Have a great Christmas everyone.

Creating Custom Item as Resources Files with Sitecore CLI 4.0

gandalf-spellBack when Sitecore 10.1 was released in February Sitecore include a significant change to how the out of the box items were shipped with the release. They were no longer stored in the databases and instead are shipped as ‘items.x.dat’ files in the /App_Data/items folder. These files are in a new Protocol Buffers (ProtoBuf) format, referred to as ‘Items as Resources’ and make upgrades simpler. Jeremy has a really good over-view of this if you want to learn more about it.

However until recently it was not possible to create your own custom ProtoBuf files to deploy your items as it wasn’t supported in Sitecore 10.1 or using Sitecore CLI 3.0.

Sitecore CLI 4.0 was released at the end of September though and now includes a ‘Sitecore ResourcePackage plugin’ for doing this.
Note: it is only possible to use Items as Resources with 10.1 and 10.2.

TLDR

Use Sitecore CLI 4.0 to Serialize your items to disk. Then run the sitecore itemres create command to create a .dat file and deploy it to another environment or Solution.

Ok but why would we want to do this?

I recently released a Module called Sitecore Sloth and for an update to it (coming soon) I wanted to add a settings item in Sitecore for it. I figured this would be a good opportunity to see if I can use the new Items as Resources functionality in my Module package. I haven’t seen anyone blog about doing this in the supported way yet so here goes.
It could be really useful for Deploying developer owned resources to other Sitecore instances and Containers and keeping the database clean. No deployment scripts are needed for the .dat file install as they are auto-installed on Sitecore start-up.

Sounds good how do I do it?

So the first thing you need to do is install the new Sitecore CLI 4.0, if you have any trouble with Sitecore’s guide then this is an really easy blog post to follow and includes useful info on setting up the serialization. If you have CLI 3.0 installed already you can upgrade it.

Prerequisites – Serializing your Items to disc

Assuming you now have the CLI setup and authenticated you need to do the following to Serialize your items. If you’ve done this already then skip this section:

1. Ensure you have an Sitecore.json file at the root of your solution with the new Sitecore.DevEx.Extensibility.ResourcePackage included as well as the Serialization one:

{
 "$schema": "./.sitecore/schemas/RootConfigurationFile.schema.json",
 "modules": [
 "src/*/*/*/*.module.json"
 ],
 "plugins": [
 "Sitecore.DevEx.Extensibility.Serialization@4.0.0",
 "Sitecore.DevEx.Extensibility.Publishing@4.0.0",
 "Sitecore.DevEx.Extensibility.ResourcePackage@4.0.0"
 ],
 "serialization": {
 "defaultMaxRelativeItemPathLength": 100,
 "defaultModuleRelativeSerializationPath": "items",
 "removeOrphansForRoles": true,
 "excludedFields": []
 }
}

Mine is here:

json

 

 

 

 

 

 

2. Add a module JSON file for your Feature or Module like so:

add-sloth-module

 

 

 

 

 

 

 

3. Configure the JSON as required. I’ve included my Sloth data template and root item in mine:

{
 "namespace": "FluxDigital.Foundation.Sloth",
 "items": {
 "includes": [
 {
 "name": "templates",
 "path": "/sitecore/templates/Foundation/Sloth",
 "allowedPushOperations": "createUpdateAndDelete",
 "scope": "itemAndDescendants"
 },
 {
 "name": "sitecore sloth",
 "path": "/sitecore/system/Modules/Sitecore Sloth",
 "allowedPushOperations": "createUpdateAndDelete",
 "scope": "SingleItem"
 }
 ]
 }
}

 

4. CD into your module or feature directory and Run the ser pull command to serialize your items to disc:

dotnet sitecore ser pull

ser-pull2

 

 

 

 

You should see the .yml files on disc in an items folder within your module / feature:

items

 

 

 

 

Creating the Item as Resource file

Ok so now were ready to create our Item as Resource (.dat) file.

1. Ensure your command prompt is still within the Solution route and run the itemres create command like so:

dotnet sitecore itemres create -i FluxDigital.Foundation.Sloth  -o src\Foundation\Sloth\code\App_Data\items\master\sloth –overwrite

Note: the -o param to choose the output folder for your .dat file.

itemres2

 

 

 

 

 

 

 

You should then see you have a file saved to your output folder like so:

datfile

 

 

 

 

 

 

 

 Deploying your items as resource file

So the final step is to deploy the .dat file to another environment (e.g UAT or Staging). In the real world this could be via NuGet or inside a Sitecore package or a deployment pipeline.

I’m going to include this file in my the Sloth 1.1 release via NuGet and Sitecore packages for Sitecore 10.1 and 10.2. You can see the contents of it here (my items.master.sloth.dat file is inside the /App_Data/items/master/ folder):

nuget-pack

 

 

 

 

To test this I installed this NuGet package to a new Sitecore 10.2 instance I setup locally and auto-magically the items appeared:

sloth-install

 

 

 

 

 

pretty cool huh?!

magic-meme

 

 

 

 

 

Hopefully this is useful for others interested in using this new feature.

Sloth 1.1 will be released later this week hopefully using this new feature to ship the items for Sitecore 10.1 and 10.2.

Further Reading

As well as the articles I’ve already linked I read a bunch of Community posts on the items as resource files topic and they are definitely worth a read if you’d like to know more about them, the video below from Justin Vogt was really useful too:

https://blog.martinmiles.net/post/everything-you-wanted-to-ask-about-items-as-resources-coming-with-new-sitecore-10-1

https://www.maartenwillebrands.nl/2021/05/22/sitecore-generating-item-resource-files/

https://www.youtube.com/watch?v=0kWjJRMT1uc

 

 

Manchester Sitecore User Group – September 2021

sug-image

Back in September I organised the first In-Person Sitecore user group in Manchester in over a year with help from Dom Hurst and Jeremy Davis.

It was kindly hosted by Dept with the Talks on the Big Screen and was great to get everyone together again.

We had 2 great talks and a debate session in the middle which I lead. It was a challenge to organise and promote given concerns around In-Person events. However we got around 25 attendees on the day, so all in all I think it was a success.

I recorded the event but it’s taken me a while to pull the videos off my SLR as needed to find my adapter for my Mac and edit the videos together.

I have finally done this though and you can now view the video of the SUG on YouTube.

Apologies for the sound and video as they are not great quality, we also lost a bit of the end of Jim and My Talks unfortunately. So I will go back to using my iPhone to record future events I think. It’s still worth sharing though I feel.

The Talks

Below is a summary of the Talks as the SUG:

Talk 1 – Shared components with JSS
James Simm, Head Of Engineering @ Dept

IMG_5439
James presented on how Dept used JSS to create a number of shared components to use for the London Marathon website.

Debate Session – Sitecore Acquisitions
Adam Seabridge, Freelance Sitecore Developer & Consultant @Flux Digital

IMG_5428
Adam talked briefly about the recent Sitecore acquisitions (Moosend, Boxever, Four51 etc) and what they are. We then debated how Sitecore might utilise them within their product offering in the near future.

Talk 2 – Exposing assets to any channel with Sitecore Content Hub Experience Edge
Daniel Ionita, Sales Engineer @ Sitecore UK

IMG_5444
Daniel gave an overview of Sitecore Content Hub Experience Edge and a detailed demonstration of combining Content Hub DAM with Microsoft Teams which allowed users to insert DAM pre-approved images into Teams messages.

 

I’m looking forward to organising the next one of these and for the upcoming SUG in London in December.

Feel free to get in touch on Twitter of Slack if you would like to speak at a future SUG.

Sitecore Sloth Module

sitecore-sloth-logo-small

A while back Kamruz Jaman wrote about how to stop experience editor reloading the page when you make changes to components.

Over the years I’ve added this improvement into a few Sites I’ve worked on in an upgrade-proof way and tweaked it a bit. It prevents the page scrolling back to the top when reloading when adding or editing components, changing datasources, rendering params or workflow.

This still seems to be a frustration even in the latest version of Sitecore and as adding this improvement in involves a bit of code (and understanding of how to inject scripts into Sitecore Experience Editor) I decided it would be nice to package this up into a proper Sitecore module.

Example of Experience Editor Issue

You can see an example of this below with a simple Bootstrap 5 Site. When a Card components properties are edited in experience editor it scrolls the editor half way up the page when saving.

sloth-experience-editor-issue

Sloth Demo

When performing the same tasks in as in the previous video (editing component properties & adding datasources) you can see that Experience Editor takes you back to where you were after saving.

sloth-demo

So here it is ‘Sitecore Sloth’. Named so, as Sloths are pretty stationary animals and that’s what this module does, stops Experience Editor moving around too much :-).

You can download the Sitecore package and install it from Github and I’m going to push it to NuGet soon too. The install instructions are in the readme here.

You can also see the code on Github here. It’s been created as an Foundation Module following Helix conventions.

Sitecore Sloth has been tested with most versions of Sitecore from 9.1 up to 9.3. Sitecore 10 update 2 seems to have resolved this issue, I’m not sure about other 10.x versions.

Please note it will only work on MVC based Sitecore sites and not Webforms ones as it hooks into the MVC rendering pipeline.

Thanks to Kamruz for sharing the original code and idea and hope it’s useful for others too.

Installing SiteCron through NuGet and Configuring

watch-header2Last week I needed to run a Job to perform a daily task within Sitecore so I decided that SiteCron would be the perfect fit for this. SiteCron allows you to run Jobs on a schedule within Sitecore using Cron expressions (under the hood it uses Quartz.NET). I’ve used SiteCron on 3 or 4 projects in the past and really like how reliable and configurable it is for these kind of requirements.

Each time I install it though I forget the steps (especially if doing it via NuGet) so I thought I’d give an overview of this below, including the Unicorn config I used.

Wait, where is the SiteCron version for Sitecore 9+?

Unfortunately the latest version at the time was 3.4 which was compiled against .Net 4.5.2 for Sitecore 7 – 8.3.
The client I was working with use Sitecore 9.3 which requires .Net 4.7.1.
So I compiled a new version (3.5) from the Source on Github for Sitecore 9.3 and created a Nuget package. I noticed that João Neto had also created a branch with an upgraded version that should work with Sitecore 10.1 so created a NuGet package for that too (3.6). Akshay (who created SiteCron) kindly uploaded them to NuGet for me.

sitecron-tweet

What version of SiteCron should I use and How do I install it?

You can download Sitecron from the Marketplace however only 3.2 is released there currently. So I’d advise you install it from Nuget instead. Also generally this is a better way to manage the installation and deployment of Modules in Projects.

Here are the versions of SiteCron you should install for each Sitecore version:

  • Sitecore 7-8.2 –> use SiteCron 3.4
  • Sitecore 9.1-9.3 –> use SiteCron 3.5
  • Sitecore 10.1 –> use SiteCron 3.6

    What about Sitecore 9.0 and 10.0?
    – well 3.4 might work with Sitecore 9 and 3.6 may work with Sitecore 10 but I’m not sure, I’ve not tested them.
    If you try them and they don’t work please pull the source, update the .Net version and compile one or let me know and I’ll try and help.

Install Process via NuGet and Unicorn

  1. Within the project you want to install Sitecon in, either use the NuGet package manger in Visual Studio or run the Install-Package command with the version you need:
    Install-Package SiteCron -Version 3.5.0
  2. Once you’ve installed the package you will see the following have been installed in your project:
    +---App_Config
    | \---Include
    | \---Z.SiteCron
    | SiteCron.config
    | SiteCronJobs.config
    | 
    +---sitecore
    | \---admin
    | SiteCron.aspx
    | 
    \---SitecorePackage
     Konabos.Minions.RunMinion.dll
     Konabos.Minions.RunMinion.Help.txt
     SiteCron FULL Sitecore Package.zip
     SiteCron Items Only Sitecore Package.zip

    You should see there are two packages installed from NuGet along with some config, dlls and an admin page.

  3. Next I’d recommend installing the ‘SiteCron Items Only Sitecore Package.zip’ on your local Sitecore instance. This will install just Sitecore items that are required to run the module but no other files.install-sitecron-items-only-package
    Once you’ve installed the package you should see something like the following:
    sitecron-after-install
  4. Next, if your using Unicorn you should configure settings similar to below to ensure the items you just installed get Serialized to disk and deployed to other environments. Were using Helix Base but if you are not the config will be quite similar to the below.

    After completing step 7 below you should serialize this config from the Unicorn control panel once it’s deployed locally.
  5. Review the settings in /Z.SiteCron/SiteCron.config and adjust as required.

  6. Tidy up – you can now remove the Sitecore zip packages from the project and if you are not using Commerce you can remove the Minion dlls too.
  7. Build and publish the Project.
  8. Finally enable one of the sample Jobs that are installed in the OOTB folder and check that it runs correctly. If it does not run then Check the logs.

Creating Custom Jobs using SiteCron

Akshay has written some info on creating Jobs and using it like this one here. I’ve written a few posts before on this too so check them out.

Contributing to SiteCron

If you’d like to contribute to the module then I’m sure Akshay would appreciate it. You can pull the source from here: https://github.com/akshaysura/Sitecron and branch it and then create a pull request.

Note that the active branch is master and that the current code-base has been updated for 10.1. Therefore if you are looking to update the code for an older version of Sitecore then you may need to branch from an older commit to master instead. It’s probably best to reach out to Akshay to be sure.

Once you’ve created an updated version of the Module make sure you also update the AsemblyInfo.cs file with the new version number. You can then run the following command to create a NuGet package and share it with Akshay too:

nuget pack Sitecron.csproj -version 3.7.0

 

Hopefully this is useful for anyone working with SiteCron in future and I’ll aim to update this for future releases if required.

Creating a Table Component for Sitecore

So not too long ago I had a requirement to create an Table component for a client.why
Before you shoot me down, I know this is generally a bad idea as the RTE in Sitecore has in-built support for Tables and there are Rich Snippets too. However there were a number of reasons that wouldn’t work well in this scenario, read on to find out why.

But, but RTE!?

table-expeditor

 

 

 

 

 

 

 

There were some specific requirements which couldn’t be met by the RTE or Rich Snippets fully, we needed to:

  • Add some specific mark-up, classes and wrapping divs around the table (some of this was for responsive requirements).
  • Enforce Table header, body and footer rows
  • Reduce the options for cells and rows to control better what content editors can do (for consistency).
  • Be able to update the table mark-up easily in the future (so Rich Snippets wouldn’t work well here either)
  • Limit the RTE options within a cell also to control better what content editors do.

Ok so how does the Component work?

So I did a bit research and thinking about various options and I liked Ben Golden’s answer on SSE. This provided a simple solution using Sitecore’s standard item and child item architecture to structure the tables.

So this is what I went for, it looks like so in Sitecore:

table-cells

As you can see the structure is Table > Row > Cell(s) and the Table has 3 fields. More could be added easily if required in future.

There are no specific fields on the Rows but the Table has two checkboxes:
table

 

The cell template looks like so:
cell-template

You can see the rowspan and cellspan options use a list of items as their datasource. We also use a custom RTE profile for the cell text to only allow a very small amount of RTE options.

Show me the Code

The following elements were required to build the component.

Rendering

The view is pretty simple. It just loops over the rows and outputs the cells. It also works out if a header and/or footer is required and if so renders out the correct mark-up for the first/last row. We are using Glass here so this may differ a bit for some sites. Note I’ve removed the code for row and cell span to make it easier to read:

The rendering itself is just a simple Sitecore View Rendering:   table-rendering

Models

The following are the 3 models which are used by the component, again pretty simple. The models are mapped with standard Glass mapping for the fields so I’ve not included that here:

What does it look like?

All being well you should see something like this output in the page:

table-output

 

 

 

 

 

 

In experience editor you can edit the cells like so:

cell-editing

Adding Rows and Cells quickly

This all works quite nicely but it can be time consuming to add rows and columns, especially if creating large tables. So there was a requirement to automate this. So I created a custom action which works in both experience editor and content editor. It’s fairly intelligent as does things like checking to see how many cells exist for current rows when adding new ones, orders the cells and rows automatically and names them for the user too.

add-table-row

 

 

 

 

 

 

add-table-row-5

 

 

 

 

 

 

add-table-row-8

 

 

 

 

 

 

Here is the code, note you will need to update the template ids for the rows and cells if re-using this code:

This is patched in for Content Editor like so:

<sitecore>
<commands>
<command name=”webedit:ext:addtablerows” type=”MyWebsite.Commands.AddTableRows, MyWebsite”/>
</commands>
</sitecore>

It can be added to experience editor by creating an custom experience button to call the command and assigning it to the rendering.

Is this a good idea?

Inevitably there will be requests to add more and more functionality to the table component over time to support things like alignment of cell content and so forth which adds further development time and complexity to the component. However for simple tables this does work fairly well and is an option where there are complicated design requirements or content editor restrictions required for Tables.

Hopefully this is useful for those considering building a Table component and weighing up the pros and cons of doing so.

Logging to the SiteCron Last Run Log

time

I’ve worked with SiteCron quite a bit, but up until recently I hadn’t realised until recently that there was a field that should be used to track what happened when the Cron Job last ran, this field is the ‘Last Run Log’.

I think it’s been around since version 2.18 but I didn’t notice it and couldn’t find any examples online which showed me how to use it:

cron-job-log

 

 

 

 

 

 

 

 

I wasn’t sure how to update it but as It is a Multiline text field I figured I could just be update it like any other Sitecore field like so:

The script is pretty simple, it gets the Job Item and then finds the field and updates it. Thankfully SiteCron has some constants that can be used to help here.

Notice how I’m appending to the field to build up an historic log of each run. This is useful if you have a job that runs multiple times a day/week and need to look back at the last x runs to see what happened.
I’m logging both successful and unsuccessful runs. Obviously you can change the log message to say what you want.

Anyway I hope this is useful to others using SiteCron who want to log what the Job does when it runs.

Enable and Disable Publishing Targets with SPE

pexels-pixabay-416760 (3)

It’s been a while since my last post as back February & March I was preparing for presenting at the Swiss Sitecore user group and I’ve been pretty busy since then, So I thought it’s time I shared some things that other might find useful from the past few months.

The client I’m currently working with needs to Toggle On and Off publishing Targets quite often in their UAT environment during performance testing.  I posted a question a while ago on SPE about how I could do this but the answers about setting permissions for specific users were not really feasible for me.

So I wrote an SPE script to delete and restore the Publishing Targets, however  our version of SPE (5.1) was too old for it to work (as I’m using the Restore-ArchiveItem function). We recently upgraded to SPE 6.2 and I was able to test it and it works great.

The Function

We have a Web and Pub publishing Targets so you will need to update these and the ids in the function below:

 

How to use It

Import the above function and then pass either ‘on’ or ‘off’ to it.

Import-Function Toggle-Publishing-Targets

Toggle-Publishing-Targets -state “on”

Thats it. You’ll get some feedback in the console to inform you when it’s removing them or restoring them.

It takes about 20 seconds or so to run in our UAT environment for some reason but it’s a nice quick way of doing this when needed. Hopefully this is useful for others too.

Scripting Sitecore Packages with SPE

gandalf

This week I needed to automate the creation of a package to include a number of items that we want to backup before syncing content down from other environments. I needed to conditionally only backup certain test pages and components based on name.

I figured I’d be able to do this with Sitecore PowerShell Extensions and it turns out you can.

Here is a script based on the one included in SPE (/sitecore/system/Modules/PowerShell/Script Library/SPE/Core/Platform/Development/PowerShell Extensions Maintenance/Prepare Console Distribution).

The script creates a package with two sources:

  • Test Pages – pages within a specific folder
  • Test Components – matches components in sub-folders that have a specific name (sub-folders that start with ‘Test’)

It then provides the package for you to download like so:
spe-package

 

 

 

 

 

 

To use this script update the meta data section at the top and the paths to include your own pages and components. Check the InstallMode param is correct for what you want to do and also the filters for component name matches what you need, update it if not. You could include additional sources too by duplicating the $source = ‘x’ and $package.Sources.Add($source); lines.

Hopefully this is useful for others that need to do this in future.