Something that has bugged me for quite some time in the Sitecore Page Editor, is the inability to remove a Link field once one has been set (at least you cannot at the time of writing in Sitecore 7.5, perhaps 8 will alter this).

As of 7.5 and before, you can only edit the link via the Page Editor, and in that dialog there is no way to remove the existing link:

The only real way to remove the link is to shift over to the Content Editor and do it there. Whilst this works, if your editors are used to using the Page Editor it’s better if they can accomplish the task without navigating away.

Fortunately, it’s quite straight-forward to add a new command - one that clears any existing link in the field. Here’s the code for the command itself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class RemoveLink : WebEditCommand
{
public override void Execute(CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
string formValue = WebUtil.GetFormValue("scPlainValue");
context.Parameters.Add("fieldValue", formValue);
Context.ClientPage.Start(this, "Run", context.Parameters);
}
/// <summary>
/// Runs the specified args.
/// </summary>
/// <param name="args">The arguments.</param>
protected static void Run(ClientPipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
if (string.IsNullOrEmpty(args.Parameters["fieldValue"]))
{
SheerResponse.Alert("There is no link to remove.");
}
else
{
SheerResponse.SetAttribute("scHtmlValue", "value", GetDefaultText());
SheerResponse.SetAttribute("scPlainValue", "value", String.Empty);
string str = args.Parameters["controlid"];
ScriptInvokationBuilder invokationBuilder = new ScriptInvokationBuilder("scSetHtmlValue");
invokationBuilder.AddString(str);
invokationBuilder.Add(false);
invokationBuilder.Add(true);
SheerResponse.Eval(invokationBuilder.ToString());
}
}
private static string GetDefaultText()
{
using (new LanguageSwitcher(WebUtil.GetCookieValue("shell", "lang", Context.Language.Name)))
{
Database database = Factory.GetDatabase("core");
Assert.IsNotNull(database, "core");
Item obj = database.GetItem("/sitecore/content/Applications/WebEdit/WebEdit Texts");
Assert.IsNotNull(obj, "/sitecore/content/Applications/WebEdit/WebEdit Texts");
return obj["Default Text"];
}
}
}

This code has been adapted from the existing EditLink and ClearImage commands, to mimic their behaviour.

The execution is very simple - first of all it checks to see if the field currently has a value in it, preventing it from clearing an already empty field. This prevents the page from being marked as ‘dirty’ and the user being warned that changes have been made if they try to navigate away without saving. If you feel the alert message here is overkill for the user, it could be removed so that in that branch of the code, it simply does nothing.

If there is a value, this just returns a javascript function call to clear the value from the field.

With this code in our solution, we need to add the command to config so that it can be referenced. This can done with a simple config file in the Include folder, with the appropriate assembly + class reference:

1
2
3
4
5
6
7
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<commands>
<command name="webedit:removelink" type="YourWebsite.Sc.Client.WebEdit.Commands.RemoveLink, YourWebsite.Sc.Client"/>
</commands>
</sitecore>
</configuration>

The last step is to create an Item in the Core database so that this command appears in the Page Editor for all Link Fields. In the folder at /sitecore/system/Field types/Link Types/General Link/WebEdit Buttons, add a new WebEdit Button item called Remove Link.

Within this item, set the appropriate fields to reference the command and choose a nice icon:

With that in place, you will immediately see a new icon in the Page Editor:

And that’s it!

I hope that proves useful, it’s a pretty straight-forward solution. I do suspect though that this could be entirely accomplished through javascript, as there is little need for any server-side processing. Please leave a comment if you found this useful, or have any suggestions.