Do You Still Have Hard-Coded Media Paths URLs? Let’s Find That Out And Fix It!

Why This Is A Problem?

It is not unusual to land on an old Sitecore project which you don’t know everything about. Even if it is old, it still needs to be maintained, right?. With that in mind, you start giving it some love, looking for improvements from and architectural perspective and also for small wins. Every win counts. That’s when you realize that there are a bunch of unused media items that could be removed to make the instance lighter and cleaner.

Since you are a PowerShell lover just like me, you quickly write an script that uses the Links database to find media items which are never referenced. That would give you a list of the media items you can get rid of.

That’s what happened to me, until we realized that some media items were not being referenced by its relative path, instead, they were using hard-coded media paths to put them on a few pages. That broke entirely our plan.

So Sad

How To Find Those Hard-Coded Media Path?

Don’t panic. The plan is still good, your script is still useful, we just need to identify those hard-coded media paths and fix them first.

Dont Panic

What about another Sitecore Powershell script? One can never have enough of those, or at least that’s what we believe. And here I am to share with you guys this little boy to save you the time of creating it.

The goal here is to have an script which is not only capable of finding the items with hard-coded media paths, but which can also list which fields have them. That’s going to make our life easier when it comes the time to fix them later.

To archive that, the first thing we created was a function to get all fields containing hard-coded media paths from a given item. It simply goes through the text fields (single-line, multi-line and rich-text) and checks if their content contains a text which represents the project media item folder (or folders). If you find some, you already know it’s a hard-coded path, since relative Sitecore media paths should be like “-/media/611d68e7f2a1436e9a7ba12bbd797e0e.ashx” and as you can see, there is no folder name on it. The function must returns a list with those fields. That’s what we came up with:

function Get-HardcodedMediaFields($scitem) {
	$fieldsList = [System.Collections.ArrayList]@()
	
	foreach($field in $scitem.Fields) {
			
		if ($field.Type -eq "Single-Line Text" -or $field.Type -eq "Multi-Line Text" -or $field.Type -eq "Rich Text") {
			if ($field.Value.Contains("/media/siteA") -or $field.Value.Contains("/media/siteB")) {
				$fieldsList.Add($field);
			}
		}
	}			
	return $fieldsList;
}

With that in hands, the next step would be creating another function to identify if a given page has at least one field with a hard-coded media path. In order to have the problematic list field in the final SPE report, we are going to add another property to the page which will contain this field list. Here is the code for this function:

function Has-HardcodedMediaPath {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [Sitecore.Data.Items.Item]$Item
    )
	
	$fieldsWithHardcodedMediaPath = Get-HardcodedMediaFields $Item	
	
	if ($fieldsWithHardcodedMediaPath.length -eq 0) {
		return "";
	}
	$fieldsStr = $fieldsWithHardcodedMediaPath | Where-Object { $_.Name.length -ne 0 } | ForEach-Object { $_.Name };
	
	$Item | Add-Member -NotePropertyName FieldsNameWithHardcodedMediaPath -NotePropertyValue $fieldsStr;
	$Item | Add-Member -NotePropertyName FieldsWithHardcodedMediaPath -NotePropertyValue $fieldsWithHardcodedMediaPath;
	
    return $Item;
}

And finally, in order to be able to scope this search for an item and its children and also have a nice and beautiful report instead of just printing lines on the SPE console, we added this piece of code:

$item = Get-Item "master:\content"
$result = Read-Variable -Parameters `
    @{ Name = "item"; Title="Start Item"; Root="/sitecore/content/"} `
    -Description "A report of items and their urls" `
    -Title "Select root item for report" -Width 500 -Height 480 -OkButtonName "Generate" -CancelButtonName "Cancel"

if($result -eq "ok")
{
    Get-ChildItem -Item $item -WithParent -Recurse `
      | Where-Object { (Has-HardcodedMediaPath $_) -ne "" } `
      | Show-ListView -Property Name, FieldsNameWithHardcodedMediaPath, Language, Version, ID, TemplateName, ItemPath
}

Here is an example of what we get when we run everything on Sitecore: PowerShell Script Results

How To Fix Them?

Ok, now we have the list of issues, but it is worth nothing if we don’t fix them.
Rich Text Editor

If you take one of those media paths which are being hardcoded and open your Navigation -> Links menu, you will see that there are no references for it on there. That’s the initial problem we discussed at the beginning of this article. Links

We have two options now. We can either create another SPE function to find the media item, get its relative path and replace it in the content field, or we can just do this manually. In my case, since I did not have hundreds of those cases, we went manually and fixed them ourselves. That’s the result after doing it:
Image
Results

Conclusion

Withing a few minutes you are now able to go ahead with the initial plan and remove every single media item not being used. Authors will be less confused, the Sitecore instance is going to be more performant, and everybody will be happier. See you on the next blog post!

Bye


Hugo Santos
Search Practice Lead
Konabos Consulting
Contact Me

Follow us on Twitter
Follow us on LinkedIn
Follow us on YouTube