Skip to main content

Posts

Showing posts with the label Powerhshell

Sitecore PowerShell script to update Allowed Controls field in placeholder settings

The Problem We created a new module (or component or rendering) in Sitecore and we needed to add this rendering to Allowed Controls field in the placeholder settings in Sitecore. It is also required to allow this rendering on all the placeholders in Sitecore so that content authors have the ability to add this rendering on pages in experience editor. Below is a snapshot of the field that is used for this- If the placeholders were in limited number, it would have been an easy manual job. But we had more than 20 placeholders for different page types. And we were in a process of introducing several new modules which meant this task is going to be repetitive and time consuming. So, to save time and for accuracy, we decided that we should develop a simple PowerShell script to make this process simple. The Solution We developed below script -  $placeholders = Get-ChildItem "/sitecore/layout/Placeholder Settings/<path where your placeholders are present>" -recurse Foreach($pla

Script to read contents of media files attached to Sitecore media library items

Ever wondered how can we read contents of files uploaded to media library without rendering them? Recently, we needed a PowerShell script to read the content of our SVGs and create a list of SVGs which contained a pattern in them. After a little research, I came up with this script -  $item =Get-Item -Path "/sitecore/media library/<filepath here>" $mediaItem = New-Object 'Sitecore.Data.Items.MediaItem' $item; $blobField = $mediaItem.InnerItem.Fields["blob"] if($blobField){         $blobStream = $blobField.GetBlobStream()         if($blobStream){             try                 {                 $contents = New-Object byte[] $blobStream.Length                 $blobStream.Read($contents, 0, $blobStream.Length) | Out-Null             }              finally              {                     $blobStream.Close()                 }             $fileText = ([System.Text.Encoding]::Default.GetString($contents))         } } In the above script, $fileText will co

Sitecore PowerShell script to remove rendering parameters

This blog introduces you to concept of updating rendering and rendering parameters using Sitecore PowerShell Extension. If you are maintaining a web application based on Sitecore with loads of content items, you may be familiar with content cleanup or update requests that comes from business. This is where Sitecore PowerShell Extension comes very handy and saves our life. A little background .. We were using Media Framework based connector for Brightcove videos integration in Sitecore 8.2 website. When we upgraded to Sitecore 10.1, connector for Brightcove videos integration was also migrated from Media Framework to Data Exchange Framework. So far so good. But we realized that Media Framework based integration involved storing some video and video player related data in rendering parameters as in snapshot below -  We did not see these rendering parameters getting used anymore in Data Exchange Framework based integration. Hence, it was decided to remove these rendering parameters from r

Restart unresponsive Sitecore Publishing Service using PowerShell

 We are on Sitecore 10.1.2 and we use Sitecore Publishing Service module. We often encounter random unresponsiveness of Publishing Service where content authors report that they tried publish an item or a site but the publishing dialog seemed unresponsive and errored out as in snapshot below after few seconds - When we looked at the publishing service logs, we could see that it disposed off all the connections but did not attempt at recreating them. An application pool recycle solves the problem but the catch is this blocks the content authors from publishing their changes until a developer gets to recycle the application pool for the publishing service. We had an option of scheduled application pool recycle for the Publishing service but our application is used 24x7 across the globe by content authors for editing their content. So there were chances that application pool recycle may kill any publishing jobs and then create further problems. As an attempt to solve this problem, we star

Script to renew self-singed certificates in Sitecore

This one is an article to spread awareness about script solutions available in community but not popular.  As my self-signed certificates for Sitecore 10.0 instance expired, I want to use SIF or CLI to renew them and best also to replace thumbprints to minimize manual work. Is this possible with SIF or CLI? Or is there any ps1 script I could use. We have a LOT of test environments. As the certs have been expiring, I was complaining about the manual process I was using to change/update these. My clever co-worker Karl bowled up a powershell script that has been such a help!  This will generate a new CA cert for you to base all your self-signed certs on, or it will use one you already have. Put the pfx file in the same directory as the script. It displays all the sites in IIS and you pick the one to update - say a Sitecore 9 xconnect site, changes out the cert in the bindings, updates the thumbnails where needed in config. So easy once you've used it a couple of times. Enjoy. https://

ServiceAuthorizationManager in Sitecore Powershell Extension

Everyone loves using Sitecore PowerShell Extension i.e. SPE due to the flexibility it allows to work with Sitecore. Those who are working on SXA must be already aware that SPE is a prerequisite to SXA. In fact SXA has many inbuilt scripts which help in managing the SXA tenant and sites. These SXA scripts are accessible through Scripts item that appears in an item's context menu.  Note - Context menu is menu that opens up when you right click on an item (as in snapshot below). So far so good! One day we realized in our live environment that even content authors can access the Scripts item in context menu and they have complete access to SXA scripts  like Remove Tenant, Remove Site, Add Site Language etc.  We realized this could create potential problems on live environments. Just imagine a scenario about an unaware content author who encounters these options and decides casually to check that what can Remove Tenant script do. Scary, right! So I started working on finding a way to h

Sitecore PowerShell Script to create all language versions for an item from en version

  We have lots of media items and our business wants to copy the data from en version of media item to all other language versions defined in System/Languages. This ensures that media is available in all the languages. So, we created the below powershell script to achieve the same -  #Get all language versions defined in System/Languages $languages = Get-ChildItem /sitecore/System/Languages -recurse | Select $_.name | Where-Object {$_.name -ne "en"} | Select Name #Ensuring correct items are updated by comparing the template ID  $items = Get-ChildItem -Path "/sitecore/media library/MyProjects" -Recurse | Where-Object {'<media item template id>' -contains $_.TemplateID} #Bulk update context to improve performance New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) { foreach($item in $items){    foreach($language in $languages){ $languageVersion = Get-Item -Path $item.Paths.Path -Language $language.Name #Check if language versi

Export Sitecore media library files to zip using SPE

If you ever require to export Sitecore media files to zip (may be to optimize them), SPE (Sitecore Powershell Extension) has probably the easiest way to do this for you. It's as easy as the below 3 steps -  1. Right click on your folder (icons folder in snap)>Click on Scripts> Click on Download 2. SPE will start zipping all the media files placed within this folder. 3. Once zipping is done, you will see the Download option in the next screen. Click Download Zip containing the media files within is available on your local machine. You can play around with the images now. Hope this helps!! Like and Share ;)

Sitecore Powershell script to get all pages that contain the keyword

In this post, I am going to share a script which can help you find all the pages which contain a keyword in its content. This requirement came from the business team who wanted some insights about pages which use keywords like finance, commerce etc. One can easily get this information from search functionality on Sitecore site or client. Or one can always create a powershell script and generate a report that can be shared with business users (yes, they love reports). We had a few assumptions -  We looked for keyword only in Rich text fields. You can modify script to include more field types. We needed the page links rather than Sitecore content path as the business users were not familiar with Sitecore. Mostly, the keyword was present in content items placed within _content folder under the page. For such items, we preferred to resolve the URL to the page ancestor to the content item. For this, we checked if the item path contains _content . If yes, then we limited the URL to page ite

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