Wednesday, 3 September 2008

Edit Page - Shortcut/External link tab

Shortcut/External tab in edit mode is a useful thing. You can create different types of links there, you can also specify target frame which allows you for example to open external links in a new window. It all sounds great but do you really use it and support the target frame property in your code?

I have to admit that my code hasn't support that. Luckily for me it has changed recently but to be honest EPiServer doesn't have the best support for those properties. If you think that you will find some dedicated methods/properties to access selected values programmatically then I will save your time ... you won't!

On the other hand, all what you have to do is to get properties called 'PageShortcutType' and 'PageTargetFrame' which is not a rocket science assuming that you have a vague idea that those two properties exist.

So here is a short code snippet which I hope will save your time in future:

// get shortcut type
PropertyLinkType linkType = (PropertyLinkType) pageData.Property["PageShortcutType"];

// get terget frame
PropertyFrame targetFrame = (PropertyFrame) pageData.Property["PageTargetFrame"];

// use target frame only if we are dealing with external link
if (LinkType.External.Equals(linkType))
linkTitle.Target = targetFrame.FrameName;


Fredrik Haglund said...

I guess it also could be intresting to look at the code that the PropertyPageReferenceControl has for rendering a page reference since it is actually using the values from the target page itself.

From CreateDefaultControls:

HyperLink target = new HyperLink();
target.Text = "Link";
target.NavigateUrl = "#";
IPageSource parent = this.Parent as IPageSource;
if (parent != null)
PageData page = parent.GetPage(this.PageLink);
target.Text = page.Property["PageName"].ToWebString();
target.NavigateUrl = page.LinkURL;
if (page["PageTargetFrame"] != null)
target.Target = ((PropertyFrame) page.Property["PageTargetFrame"]).FrameName;
if (target.NavigateUrl == "#")
target.Attributes.Add("onclick", "return false;");
target.Style.Add("cursor", "default");

Mikael O said...

Tip: To find similar cases of unknown property keys, inspect the property object while debugging.

Anonymous said...

I couldn't get this to work, so I ended up using this to check if a page had an external shortcut:

if (pd.LinkType.ToString() == LinkType.External.ToString())
return pd.LinkURL;

Anonymous said...

To add a target frame from code you have to both set the PropertyFrame.Value and PropertyFrame.FrameName property, like this:

// Open in new window
Frame defframe = Frame.Load("_blank");
PropertyFrame frame = new PropertyFrame();
frame.FrameName = defframe.Name;
frame.Value = defframe.ID;
writableClone.Property.Set("PageTargetFrame", frame);
// Save writableClone

Cindy Dy said...

Thank you for putting an effort to published this article. You've done a great job! Good bless!


Leslie Lim said...

I simply want to say I am new to weblog and truly savored you’re website. More than likely I’m want to bookmark your blog . You actually have excellent writings. Thanks a bunch for sharing your web site.