Opening Links in new Windows

This question gets asked a lot in the newsgroups. The default behaviour of the links list (which is just a template afterall) is to open the link in the same window. I took a look at Jim Duncan's cBlog site definition recently that had a nice modification that I think should be on everyone's SharePoint box. He added a simple checkbox to set a link to open in a new window.

First you'll need a new site definition so copy STS to another one for this purpose. Then copy the Links list definition to a new one (it's in a folder called FAVORITES). Alternately, you can just work directly on the definition itself but see the note at the end of this blog about that. In the LINKS definition they'll be a SCHEMA.XML file which defines all the fields and how the list behaves.

First let's add the new checkbox field that will hold our new option:

<Field Type="Boolean" DisplayName="Open in New Window" Name="NewWindow"/>

Great. Now it'll show up on the New and Edit forms and be saved with the list. How do you get it to show up in the rendered HTML? This is done in the DisplayPattern tag for the URL field. The default Links list part that we're interested looks like this:

<Default>
<HTML><![CDATA[<A onfocus="OnLink(this)" HREF="]]></HTML>
<Column Name="URL" HTMLEncode="TRUE"/>
<HTML><![CDATA[">]]></HTML>
<Switch>
<Expr><Column2 Name="URL"/></Expr>
<Case Value=""><Column Name="URL" HTMLEncode="TRUE"/></Case>
<Default><Column2 Name="URL" HTMLEncode="TRUE"/></Default>
</Switch>
<HTML><![CDATA[</A>]]></HTML>
</Default>

So we want to do a check on our NewWindow field and if it's set, add in a "target=_blank" piece of HTML to our HREF tag. So the new DisplayPattern tag should look something like this (changed highlighted in Red):

<Default>
<HTML><![CDATA[<A onfocus="OnLink(this)" HREF="]]></HTML>
<Column Name="URL" HTMLEncode="TRUE"/>
<HTML><![CDATA[">]]></HTML>
<Switch>
<Expr><Column2 Name="URL"/></Expr>
<Case Value=""><Column Name="URL" HTMLEncode="TRUE"/></Case>
<Default><Column2 Name="URL" HTMLEncode="TRUE"/></Default>
</Switch>
<Switch>
<Expr>
<Field Name="NewWindow"/>
</Expr>
<Case Value="Yes"><HTML><![CDATA[ target="_blank"]]></HTML></Case>
</Switch>
<HTML><![CDATA[</A>]]></HTML>
</Default>

That's it. A simple change to one file and all your links have the capability to open in new windows across all your sites. You can preset this value if you want (using the <ROWS> tag in your sitedef) and hide the field or let your users choose. Of course, modifying your base SharePoint install isn't recommended as the next service pack or update may wipe out those changes however for this change I'm willing to do the file management myself to avoid this. Your mileage may vary.

The other trap is that all the sites that are out there, already created using the STS template might break. Adding a new field to a definition usually is ok but it's something you need to test big time if you have a lot of sites out there already created (NUnitAsp is great for this). It would be nice if it was part of the base system though wouldn't it?

6 Comments

  • This is something I wanted to do a while back for external links (I agree, you'd expect it to be in &quot;out of the box&quot;) but couldn't find a way of doing it.



    In the end I followed Dustin's advice from SharePoint University to cycle through the &lt;a&gt; tags on the page using javascript and change the target attribute on the fly.



    Works well but does (obviously) unghost the page (not a problem for me though).

  • I've been looking for a mod like this for some time. I also agree that this should be an 'out of the box' feature.



    However, I'm not getting it to work. Neither the Add nor Edit Link pages will display the new field. I've edited the SCHEMA.XML file in the ...\60\TEMPLATE\1033\STS\LISTS\FAVORITE folder. What did I miss?



    Thanks in advance.

  • After sleeping on it, the new checkbox field now appears. Great!



    Next problem: Links still open in the same window. When I view source after adding a link, the target=&quot;_blank&quot; is not inserted anywhere. Is the 'Case Value=&quot;Yes&quot;' not recognizing the check box?



  • I'm having the same problem. Getting the check box in there works fine, but I can't get the '_blank' html into the page. I tried to put it in as default in the switch and that didn't work either, so the switch logic isn't the problem.



    Any clues?

  • Same thing here. I think is has to do with the indent of the code.

  • Hey Bil, I sent you an email with the corrected code so this will work... any chance you could post it?

Comments have been disabled for this content.