Open SharePoint List/Library Link in New Window

Why can’t a SharePoint list/library web part open links in a new window by default?

Right… maybe someday.

There are several workarounds, but here’s one of the “easy” ones that seems to make sense to site owners. It adds some Javascript in an XML Web Part or Form Web Part that’s placed below the list/library web part (maybe, even at the bottom-most zone on the page, no?). Why not use a Content Editor Web Part for the script? Well, it was a fine place to put the script in SP2007, but to survive a migration from SP2007 to SP2010, the XML Web Part seems to be the way to go for small scripts like this, and if already in SP2010, the Form Web Part should do the trick.

The code looks for the name of the list/library on page load and adds the “target” attribute to the list web part’s table tag. So, say the list name in the current site is called “Links.” The code would look for that list name in the page source by it’s Summary attribute:

<script language="javascript" type="text/javascript">
	var tbl = document.getElementsByTagName('table');
		for(var i = 0; i < tbl.length; i++)
		{
		if(tbl[i].getAttribute("Summary") == "Links")
		{
			var anc = tbl[i].getElementsByTagName('a');
			for(var j = 0; j < anc.length; j++)
			{
				anc[j].setAttribute('target', '_blank');
			}
		}
	}
</script>

For more than one list/library on the page, the code is a bit different…

<script language="javascript" type="text/javascript">
	var tbl = document.getElementsByTagName('table');
	for(var i = 0; i < tbl.length; i++)
	{
	if(tbl[i].getAttribute("Summary") == "Links1" 
	|| tbl[i].getAttribute("Summary") == "Links2"
	|| tbl[i].getAttribute("Summary") == "Links3"
	)
		{
		var anc = tbl[i].getElementsByTagName('a');
		for(var j = 0; j < anc.length; j++)
			{
			anc[j].setAttribute('target', '_blank');
			}
		}
	}
</script>

Advertisements

2 thoughts on “Open SharePoint List/Library Link in New Window

  1. Thanks! But simple and easiest solution is: Create a Site column of “Hyperlink with formatting and constraints for publishing” type and add it in your list, which enables you to specify “Open in New Window” Option!

    There are few more possible solutions, including JavaScript, JQuery, SharePoint Designer, List Schema Edit to SharePoint Link list – at SharePointDiary.com –
    http://www.sharepointdiary.com/2011/02/sharepoint-link-list-open-in-new-window_21.html#ixzz2aWtiNarl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s