Reinoud van Dalen

July 23, 2014

Fixing Sitecore 7 Internal Link with SPEAK

Sitecore 7 and later releases was shipped with the new Insert (internal) link modal window setup with the SPEAK interface. However, editing an existing link did not automatically select the existing link for you, forcing you to reopen the entire tree. Time to fix this annoying little bug.

Where is the modal window?

“Normally” when we try to edit/change the modal windows, we would look in the /Sitecore/Shell/Applications/Dialogs folder and search for an xml file. But this particular modal window is setup with the SPEAK interface and you can find items in the core database. The location in the core database seems to have changed in later Sitecore versions but you can always check the exact location in the Sitecore.Speak.config.

Some description

So let’s take a look at the InsertLinkViaTreeDialog. The item has layout details but I can’t seem to edit any of the renderings, however, I can view the raw values in which I found this particular rendering:

<r id="{DAFAFFB8-74AF-4141-A96A-70B16834CEC6}" par="PageCodeTypeName=Sitecore.Speak.Dialogs.InsertLinkDialogTree%2c+Sitecore.Speak.Dialogs" ph="Page.Code" uid="{CB6D1540-CB2D-4A4A-9AF8-753E57EECE36}" />

This is one key part of the solution as this is sort of the codebeside we used to know in those dialog xml files.

Some description

I found another rendering which is:

<r id="{08B6767B-8E8A-41C8-8880-EFEBB4EAE0E3}" par="Id=TreeView&amp;IsCheckboxEnabled=0" ph="DialogContent.Main" uid="{2F4302C9-E2C6-445B-8122-3F30F389B660}" />

After digging some more I found this is a “View rendering” located here:
/sitecore/client/Speak/Layouts/Renderings/Lists and Grids/TreeView

And it refers to a view file located here:
/sitecore/shell/client/Speak/Layouts/Renderings/ListsAndGrids/TreeViews/TreeView.cshtml

Some description

And we’re game! We have the code and presentation, now let’s see if we can fix this.

So what's the problem?

Debugging the view (yes, yes you can :)) showed me that none of the parameters are received and extending + replacing the InsertLinkDialog showed me that the QueryString does not contain much that makes sense.

Some description

If you look at the view it is a bit silly because most of the parameters are there for show. The rootItemId is always null so the database rootItem is used. And the preLoadPath is passed, but since it is always null it will never have any effect. 

So how can I fix it?

Filling the preLoadPath manually with any item’s longID showed me that the Editor now gave me a treeview with the path pre-opened for me. Ok, this is looking promising, now all I have to do is get the selected item somehow and pass it to the view.

The InsertLinkViaTreeDialog class seems the right place to do it, but the QueryString there made no sense… or does it? I hoped to see some sort of id corresponding to the selected item, so of course I checked if the guid value of hdl was an item in Sitecore but it wasn’t. It turns out that the hdl parameter is called a handle and Sitecore uses a technique where they put querystrings into the Session object with a guid as Key. Check out this post about the Handle: http://sitecorejunkie.com/2012/11/09/get-a-handle-on-urlhandle/

A quick look into the session made me hopeful:

Some description

Here you can see the raw value of the link field. Yes! I copied some handy parsing logic from the original InternalLink form and ended up with this:

Some description

I've collapsed the property and ParseLink method because I did not change anything to it, but I did modify the GetLink method using the UrlHandle.Get()[“va”] instead of using the QueryString. This is the actual fix plus using the id value and assign the LongID to the preLoadPath of the TreeView.

Hope this helps anyone else and maybe Sitecore can add this in the next release. I’ve also fixed the External link form which is suffering from the same issue. I’ll try to add another post about that as soon as possible.

TAGS: sitecore content editor


Comments
Göran

Great post