Dear fellow Sitecorians, a lot of news from the Sitecore Symposium. Read all about it – https://symposium.sitecore.com/
Today’s post is a sequel to the previous post – Handle URL rewrites in a Kubernetes cluster running a Sitecore multisite solution with NGINX using GitOps with Sitecore CLI – Part 2.
It’s about the part when the editor updates/changes rewrite rules in the Sitecore Media Library.
If you need to make a change to a media file in the Sitecore Media Library, you have to upload the changed file after every change. This can be very tedious. For example, say you have a CSV file and need to add some lines or a txt file and quickly want to add some changes. To do this, the editors must download the file, make the change and upload the file again.🥱
What if we could make life easier for the editors? Instead of downloading the file, we could allow them to update it directly in the Sitecore Media Library. Is this possible? It sure is, thanks to our good friend SPE 😊
In my previous post, I showed the faulty rewrite rules in a pop-up.
Unfortunately, it was not possible to make corrections directly in the pop-up windows. Instead, they had to download the faulty file to their computer, open it, make the changes, and save it. And after all that work, they also had to upload the file to Sitecore again😡
But thanks to SPE, it is possible to let the editors change the file directly in the pop-up window. Meaning the editors will make the change in the input field, Rewrite rules, click on the “Fix errors” button, and the change will be saved to Sitecore 😊
WHAT SORCERY IS THIS!
No need to be alarmed, citizens. Here is the “magic” code piece:
$anItemInTheMediaLibrary = Get-Item . $encodedBytes = [System.Text.Encoding]::UTF8.GetBytes($theChangedContentFromThePopUp) $stream = new-object System.IO.MemoryStream(,$encodedBytes) $mediaItem = [Sitecore.Resources.Media.MediaManager]::GetMedia([Sitecore.Data.Items.MediaItem]$anItemInTheMediaLibrary); $mediaItem.SetStream($stream, "txt")
Let me go through the code🎃
We convert the “string” content from the input field to a byte array. Then we turn the byte array into a memory stream and finally update the media item with SetStream.
Pretty simple. It will work for all txt/CSV files.
That’s all for now folks 😊