Posts Tagged ‘Content By Query Web Part’

Open SharePoint 2010 Dialog Box from Content By Query Web Part

July 25, 2010

 

Content By Query web part uses xml style sheets for UI and therefore allow us to customized its look and feel by modifying these style sheets. The style sheet which is applied to the items displayed in Content Query Web Part is “Item Style” and you can find it in the folder called “XSL Style Sheets” in the out of the box style library in your site collection.

image

Here is how you can add your own style to ItemStyle.xsl:

1. Download ItemStyle.xsl and open it using Visual Studio.

2. You’ll see multiple "<xsl:template” nodes each of which indicates a Item style.

3. You can create your own style by either copying & pasting existing style and customizing it or create your own style by scratch.

4. I have created my style by copying and customizing style called “Bullets” and following is how my customized style looks like:

<xsl:template name=MyBullets match=Row[@Style=’MyBullets’] 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 href=# title={@LinkToolTip}>
                    <
xsl:attribute name=onclick>
                        javascript:

                        function DialogCallback(dialogResult, returnValue)
                        {
                            SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
                        }

                        function onQuerySucceded()
                        {
                        
                        var currentSiteUrl = this.site.get_url() + ‘/_layouts/MyPage.aspx?Source=<xsl:value-of select=@LinkUrl/>&amp;ItemId=<xsl:value-of select=@ID/>‘;

                        var options =
                        {
                        url: currentSiteUrl,
                        title: ‘My Dialog’,
                        allowMaximize: false,
                        showClose: true,
                        width: 600,
                        height: 300,
                        dialogReturnValueCallback: DialogCallback
                        };

                        SP.UI.ModalDialog.showModalDialog(options);
                        }

                        function onQueryFailed()
                        {
                        alert(‘Failed’);
                        }

                        var ctx = SP.ClientContext.get_current();
                        this.site = ctx.get_site();

                        ctx.load(this.site);

                        ctx.executeQueryAsync
                        (
                        Function.createDelegate(this, onQuerySucceded),
                        Function.createDelegate(this, onQueryFailed)
                        );

                    </xsl:attribute>
                <
xsl:value-of select=$DisplayTitle/>
            </
a>
        </
div>
    </
xsl:template>

I modified the hyperlink action and called the JavaScript to open a SharePoint dialog box. You can find lots of posts on how to open a SharePoint 2010 Dialog box and hence i am not including explanation for that here. Also, i have placed the JavaScript directly in xsl file but you can create a separate JS file and can call the JS method in the xsl file.

Notice that i have passed two query string parameters to my dialog page and used @LinkUrl and @ID for Item URL and Item ID respectively. You can do the experiments with your custom columns as well.

5. Upload the ItemStyle.xsl back in Style Library.

6. Now edit the Content By Query Web part and expand the "Presentation" group under content query tool part.

7. Here you will find the newly added style as “My Bullets”.

8. Select the style and you click ok. Now when you’ll click on the link of item in the web part it will open the your dialog page and will pass Item URL and Item Id as query string parameters to it.

Advertisements