Javascript to open SharePoint links list items in a new window

This is harder than it sounds, mostly because of the way SharePoint renders it pages and links within. If you are displaying a links list on a page and it is GROUPED using metadata, then you are going to have a bit of a tough time accessing those links using Javascript. The key is to use SharePoint’s version of the

$( document ).ready “trigger” called¬†_spBodyOnLoadFunctionNames.push

Below is some code I used which worked nicely. There is a bit of output you can remove once you see you are getting the results you want. ūüôā Just add into a CEWP on the same page (or MP ref).Add you script tags to ref jQuery lib and surround JS below:

function sharePointReady()
{
updateLinks();

//Hook into SharePoint event to fix all links in group-by tables.
var oldExpand = ProcessImn;

ProcessImn = function(){
var results = oldExpand.apply(this, arguments);

updateLinks();

return results;
}
}

function updateLinks()
{
var url = ‘://’ + window.location.hostname;
$(“a”).each(function() {
var link = this; // assign the link object to another variable for easier managability
var linkHref = link.href.toLowerCase(); // lower case it
console.log(url);
console.log(linkHref);
if(linkHref.indexOf(url) < 0 && linkHref.indexOf(‘javascript:’) 0){ // check to see if this is a PDF
link.target = ‘_blank’; // change the target to be in the new window
$(link).removeAttr(“onclick”); //remove the SP click event
}

if(linkHref.indexOf(‘/forms/’) > 0 && linkHref.indexOf(‘).aspx’) > 0){
//check for links in the forms library
link.target = ‘_blank’; // change the target to be in the new window
$(link).removeAttr(“onclick”); //remove the SP click event
}
});

}

//this is the SharePoint preferred alternative of $(document).ready
_spBodyOnLoadFunctionNames.push(“sharePointReady”);

 

Advertisements

Problems with SharePoint List Item Events

Anyone who has had to develop a SharePoint event handler has probably run into some trouble. This path is fraught with danger and as far as I can see the general advice when coming up against an issue is “gorn work around it and leave me alone”. So here are some links and tips for getting past the usual suspects:

Event Trouble Shooting ¬†-is this a known problem or are you just “Special”

Codeplex Handler Management Page – this is a handy feature that allows you to see what handlers are associated with a site, list, content type..

Everything Eventful Рgreat post for Getting Started with SharePoint events.  Everything from writing a receiver to attaching and troubleshooting.

Tip – When in doubt, used ED events instead of ING events. Make sure you do some of this though:

 bool auu = web.AllowUnsafeUpdates;
                web.AllowUnsafeUpdates = true;

….. do stuff to handle the event

 this.DisableEventFiring();
                    item.SystemUpdate(false);
                    this.EnableEventFiring();
                    web.AllowUnsafeUpdates = auu;

Tip- Document libraries are a bit different from standard lists. Mainly because when a document is uploaded, there are 2 sets of events. You will notice that when you upload a document, if you click Cancel at the Edit Form (complete doc metadata stage), the document will still appear in the library. So the itemadding, added, updating, updated events have fired already. When you complete the metadata for the document, they fire a second time.

Tip- Check your fields are A-OK. If you have a faulty field definition deployed uisng a feature it may have an invalid attribute that was not revealed when it was deployed. Sometimes this can cause items to ignore new property values altogether. Try removing custom fields and updating/adding items to see if the problem persists.

Other oddities

Happy Handling.