Skip to main content

Posts

Know if Sitecore item ID is a page or data source

Basically, everything in Sitecore is an item. An item is a page only if it has presentation details added i.e. a valid layout and valid set of renderings.  So, when we have an item ID and if we need to find if the ID belongs to page item or a datasource, we need to get the item using the ID and then get the presentation details for the item.  This can be done easily as -  Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId)); item.Visualization.GetLayout(Sitecore.Context.Device); Or pass the item to below func - private bool HasPresentationDetails(Item item) {  return item.Fields[Sitecore.FieldIDs.LayoutField] != null       && !String.IsNullOrEmpty(item.Fields[Sitecore.FieldIDs.LayoutField].Value); } If it returns true, the item is a page item. Else it is a datasource item.  

Implementing .Net core based Dependency Injection in Sitecore

Since version 8, Sitecore has started focusing on cleaner and extensible architecture with more and more plug and play features. One key improvement in version 8 was introducing dependency injection within Sitecore based on  .net core libraries for DI. So if you are creating an application in Sitecore and planning to use DI, you can always make use of inbuilt DI used by Sitecore. Obvious advantage is you wont be introducing any new paths in the solution and no new libraries will be introduced keeping the solution lightweight.  So, the obvious question - how do we do it? I am assuming here that you have basic understanding of a Sitecore application and dependency injection fundamentals. Hence, I will focus on steps needed to enable DI in Sitecore. Include the following DLLs to references in your project (in Foundation layer if you are following Helix principles) - Microsoft.Extensitons.DependencyInjection,  Microsoft.Extensions.DependencyInjection.Abstraction Create the configurator cla

Sitecore Powershell script to update standard value of field without impacting existing item values

So, sometimes, the content authors and marketers feel that the default value of one of the fields mentioned in template standard values should be updated. The problem here is that the standard value for the field is already being used in hundreds of items derived from this template in the site. Updating the value for field in standard values will update the field value in all these items (if they have not been updated by an author ever). But we want that all the existing items should retain their current value, even if it is derived from standard value item.  The easiest way to execute such updates is via Powershell :)  The below script basically looks for ContainsStandardValue flag in the fields which tells Sitecore that the field value for item is derived from standard values. So our script shall reassign the value to the field programmatically whenever  the flag is set to true. Reassigning will set the flag to false. The script generates a report comparing the values before the scri

Sitecore Powerhshell Script to find keyword in content tree

In our Sitecore application, we had requirement to replace/remove style classes or tokens in entire Sitecore content tree. The site was live and had lots of content. So we needed to go through each and every field in Sitecore content tree and look for those keywords.  Sitecore power shell is the best way to do such bulk search/replacements. Below script can be run in a Sitecore instance to lookup for specific keywords in Sitecore fields. The script generates a report at the end which shows the list of all occurrences. $startPath = "master:/sitecore/content/<site path>" Write-Host "Search started $(Get-Date -format 'u')" $reportFields = "ID|Language|TemplateName|FieldName|FieldType|Path|FieldValue" $reportFieldsArray = ($reportFields).Split("|"); $searchText = "<search text>" $list = [System.Collections.ArrayList]@() $itemsToProcess = Get-ChildItem $startPath -Language * -Recurse if($itemsToProcess -ne $null) {     $i

Auto Install Sitecore Modules in Azure PaaS using ARM templates

We can install the Sitecore modules e.g. JSS, SXA, SPE or any other module using Azure ARM templates while installing a new Sitecore environment in Azure PaaS. Wondering why we need to do this? To setup a Sitecore application server by single click using CICD, we cannot depend on manual installation of Sitecore modules as a post-installation step after Sitecore setup. How we do this? We need to- Download the wdp file (not zip package) for JSS module and store it in Azure storage (wdp available at -  https://dev.sitecore.net/Downloads/Sitecore_JavaScript_Services/120/Sitecore_JavaScript_Services_1200.aspx ) Add a snippet with JSS module details in azure.parameters.json (described in the links below) https://github.com/Sitecore/Sitecore-Azure-Quickstart-Templates/blob/master/MODULES.md https://github.com/Sitecore/Sitecore-Azure-Quickstart-Templates/tree/master/JSS%2012.0.0/xp Prerequisite   - Bootloader module snippet need to be included in azure.parameters.json as well for this to work.