Tuesday, July 27, 2010

SP2010 - Get the Item ID from the OpenPopUpPage Link

Just wanted to record here a simple little jQuery script useful for retrieving the item ID from the OpenPopUpPage hyperlink that is rendered for lookup column values in a Data View Web Part in SharePoint 2010. The useful thing about this is that you can then manipulate the action of the hyperlink to pass this item id to a different page - without having to look at changing the field type in any way.

The lookup field is rendered in a DVWP with the following onclick action:

OpenPopUpPage( 'http://blah/_layouts/listform.aspx?PageType=4&ListId={FFFC7FEA-62B2-459F-9D1B-80B6CA3608FF}&ID=23&RootFolder=*', RefreshPage);


The XSL (in the DVWP) that is rendering this lookup value is as follows:


Trust Partner:





[An aside: "table-based HTML?" you may ask. Well, sometimes expedience wins over web standards, though I am usually a standards-based advocate!]


The jQuery script to extract and use the item ID is:

$(function() {
$('.linktoentitydetails').each(function() {
var $link = $(this).find('a');
var href = $link.attr('href');
if (href != '') {
var itemIdMatches = href.match(/.*&id=(\d+)&.+/i)
if (itemIdMatches.length == 2) {
var itemId = itemIdMatches[1];
$link.removeAttr('onclick');
$link.attr('href', 'persondetails.aspx?eid=' + itemId);
}
}
});
});

So, in this case, I am replacing the OpenPopUpPage hyperlink action with a simple link to a custom page (persondetails.aspx) on which I can present other information about this linked item.

No comments: