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.

6 comments:

  1. This works great! Looked for an elegant solution for a couple of hours and this is the best.

    ReplyDelete
  2. I tried this solution and it is really an elegant solution.Thanks

    ReplyDelete
  3. This works great for list items, but when I attempt to use it to open a PDF document in a document library, it doesn't work. It just continues spinning saying please wait for content to load. Suggestions for how to do this?

    This is fantastic for list items though. Best one I've found.

    ReplyDelete
  4. same here for pdfs (Stuck on "Please wait...")! Permissive is set, link is correct - same link opens fine in new page) - Any solution?

    ReplyDelete
  5. Does this work with sharepoint 2013?

    ReplyDelete
  6. Also looking for a solution for SP 2013.

    ReplyDelete