Tuesday, May 15, 2012

Open List items in Modal Dialog from CQWP /XSLT


I was working on a requirement to open list items in Modal dialog listed in Content Query Web Part.  

So what we need to do here is just remove href attribute and adding onclick event to call ModalDialog and open the url (SafeLinkUrl). Also added onmouseover event to show hand as cursor when mouse over on the text.

<a onclick="javascript:SP.UI.ModalDialog.ShowPopupDialog('{$SafeLinkUrl}'); return false;" onmouseover="javascript:this.style.cursor='hand';" title="{@LinkToolTip}">

Here are the steps 
  1. Open the root site with in SharePoint designer and go to All items > Style library> XSL Style Sheets.
  2. Open ItemStyle.xsl file and edit it (take the backup to be safe)
  3. Paste below section to the end of the file just above </xsl:stylesheet>.
<xsl:template name="ModalDialogBullets" match="Row[@Style='ModalDialogBullets']" mode="itemstyle">
        <xsl:variable name="SafeLinkUrl">
            <xsl:call-template name="OuterTemplate.GetSafeLink">
                <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="DisplayTitle">
            <xsl:call-template name="OuterTemplate.GetTitle">
                <xsl:with-param name="Title" select="@Title"/>
                <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
            </xsl:call-template>
        </xsl:variable>
        <div class="item link-item bullet">
            <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
            <a onclick="javascript:SP.UI.ModalDialog.ShowPopupDialog('{$SafeLinkUrl}'); return false;" onmouseover="javascript:this.style.cursor='hand';" title="{@LinkToolTip}">
                        <xsl:value-of select="$DisplayTitle"/>
            </a>
        </div>
    </xsl:template> 

4.      Save the file and check it in. 
5.      You need to update the ItemStyle of CQWP and apply this one.  Edit CQWP > Presentations > ItemStyle.

That’s all. Hope this Help.