Sunday, December 23, 2012

Opening items from core search results in modal popup

I am a big fan of using search instead of for example CQWP, so for listing out a set of news items from one specific list (this part is important) I created a separate search centre site in this project. Instead of sending people to the listed item's original list location when clicking on the item links in core search results I wanted to keep people on the same page for ease and user friendliness when browsing through several news items in sequence. I hence wanted to open each item in a modal popup within the search results page.

Out of the box opening of items from core search results web part



To change this behaviour I obviously had to change the XSL that tells the core search web part how to list it's listed items.

Start by editing the core results web part by clicking “Edit Page” were this Web Parts is placed, then click “Edit Web Part”. In the edit panel under heading "Display properties" uncheck the “Use Location Visualization” checkbox and click "XSL editor". This enables us to edit the XSLT directly in the built in editor (but it's best to copy paste this code into other editor like SharePoint Designer).

To find what lists the original Item Title and Link within the XSLT localize the following:   <div class="srch-Title3"> around line number 300. Just underneath this add the following Javascript:

<a onclick="javascript:NewItem2(event, &quot;http://[listviewurl]&amp;ID={substring-after($url,'ID=')}&quot;);
javascript:return false;" href="[listviewurl]&amp;ID={substring-after($url,'ID=')}" target="_self"><xsl:value-of select="title"/></a>

Thanks to Pete Stilgoe and his article "How to display ‘anything’ in Sharepoint 2010 pop up / modal window" for helping me find the javascript basis.

My javascript is basically built up of three parts: The [istviewurl], the ID=... and the <xsl:value-of select="title"/>.

The [listviewurl] you will find by seeing how the specific list your items live in behave when viewing the items there (where they in fact open modal by design). Go to the list and put your cursor over an item's link, right click and copy it's link.










You will find similar to this: http://server/site/_layouts/listform.aspx?PageType=4&ListId={63628054-BF65-4FB2-9A60-8EBBEB659640}&ID=2&ContentTypeID=0x0100B0CD2426D905CB4FABC1450EF4289FC9004FAC50F90D5C224EA8956A6A6CB9D6C6

Except for the ID=2 part, this is your [listviewurl] in the javascript.

The ID=2 is of course the ID of the specific list item, so this needs to vary for each result in the search list. To find each item's ID you need this in your XSL: &amp;ID={substring-after($url,'ID=')}". What it does is to use the built in variable $url (each item's search result list URL) that of course also includes the item's ID, and extract just the ID.

Then lastly the <xsl:value-of select="title"/> lists out the item's title as the clickable part of the javascript code.

Using my list details the complete javascript comes out as:

<a onclick="javascript:NewItem2(event, &quot;http://server/site/_layouts/listform.aspx?PageType=4&amp;ListId=63628054-BF65-4FB2-9A60-8EBBEB659640&amp;ContentTypeID=0x0100B0CD2426D905CB4FABC1450EF4289FC9004FAC50F90D5C224EA8956A6A6CB9D6C6&amp;ID={substring-after($url,'ID=')}&quot;);
javascript:return false;" href="/site/_layouts/listform.aspx?PageType=4&amp;ListId=63628054-BF65-4FB2-9A60-8EBBEB659640&amp;ContentTypeID=0x0100B0CD2426D905CB4FABC1450EF4289FC9004FAC50F90D5C224EA8956A6A6CB9D6C6&amp;ID={substring-after($url,'ID=')}" target="_self"><xsl:value-of select="title"/></a>
Now, I'm no javascript expert so I'm not sure what the two almost similar url parts do, nor why they are not exactly the same when it comes to formatting. But it works!

Looking at the code in SharePoint Designer it looks like this:
As you can see I have commented out the original code that lists out the result list titles.

And the result: Voila!

Sunday, May 20, 2012

Adding a custom CSS file to your SharePoint 2010 master page

I have been meaning to blog for quite some time, but have been thinking that it will take way too much time. But let me start simple by documenting the single most impactfull change one can do to the visual side of SharePoint, namely change the master page by adding your own CSS file.

To get it to load correctly and in the right order to the native CSS files used by SharePoint you have to do it like this.

<SharePoint:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/myCustomStyles/myCustomSheet.css %>" After="corev4.css" runat="server"/>

Place the line right before the closing </HEAD> tag and you'll be off to a good start.