Content Query Webpart and Data View Webpart – Modal Dialogs
Requirement
We have a content query webpart that displays information from a list on a subsite. When a user clicks on a link on the webpart, we want the link to open in a Modal dialog.
Solution
- Open SharePoint Designer 2010
- Go to All Files -> Style Library -> XSL Style Sheets -> ItemStyle.xsl -> Modify
- Copy one of the existing templates, e.g. Default, change the name and modify the XSL according to your needs.
Here is a simple example, that I created for ‘Job Vacancies’.
Note the bits in blue where javascript is used to force the links to open in a dialog window.
<xsl:template name=”JobVacancy” match=”Row[@Style=’JobVacancy’]” mode=”itemstyle”>
<xsl:variable name=”Created”>
<xsl:value-of select=”ddwrt:FormatDateTime(string(@Created) ,1033 ,’dd/MM/yyyy’)” />
</xsl:variable>
<xsl:variable name=”_DCDateCreated”>
<xsl:value-of select=”ddwrt:FormatDateTime(string(@_DCDateCreated) ,1033 ,’dd/MM/yyyy’)” />
</xsl:variable>
<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=”SafeImageUrl”>
<xsl:call-template name=”OuterTemplate.GetSafeStaticUrl”>
<xsl:with-param name=”UrlColumnName” select=”‘ImageUrl'”/>
</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>
<xsl:variable name=”LinkTarget”>
<xsl:if test=”@OpenInNewWindow = ‘True'” >_blank</xsl:if>
</xsl:variable>
<div id=”linkitem” class=”item”>
<xsl:if test=”string-length($SafeImageUrl) != 0″>
<div class=”image-area-left”>
<a href=”{$SafeLinkUrl}” target=”{$LinkTarget}” title=”{$DisplayTitle}”>
<xsl:value-of select=”$_DCDateCreated”/> – <xsl:value-of select=”$DisplayTitle”/>
</a>
</div>
</xsl:if>
<div class=”link-item”>
<xsl:call-template name=”OuterTemplate.CallPresenceStatusIconTemplate”/>
<script type=”text/javascript”>
function ModalDialog(url) {
var options = {
url: url,
title: “Job Vacancies”,
allowMaximize: true,
showClose: true,
width: 800,
height: 600,
dialogReturnValueCallback: silentCallback};
SP.UI.ModalDialog.showModalDialog(options);
}
function silentCallback(dialogResult, returnValue) {
}
function refreshCallback(dialogResult, returnValue) {
SP.UI.Notify.addNotification(‘Operation Successful!’);
SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
}
</script>
<a href=”javascript:ModalDialog(‘{$SafeLinkUrl}’)” target=”{$LinkTarget}” title=”{$DisplayTitle}”>
<xsl:value-of select=”$_DCDateCreated”/> – <xsl:value-of select=”$DisplayTitle”/>
</a>
<div class=”description”>
<xsl:value-of select=”@Description” />
</div>
</div>
</div>
</xsl:template>
You can also use the same bit of code to force links in your data view web part to open in a dialog window.
- Edit Webpart properties of your Data View Web Part
- Under Data View Properties -> Click on XSL Editor
- Copy the entire contents to a code editor like Visual Studio or Notepad++.
- Search for the string ‘a href’ – there should only be 1 string.
- Just above the line ‘<a href….”>
- Add in the following code:
<script type=”text/javascript”>
function ModalDialog(url) {
var options = {
url: url, title: “<change this to your title>”,
allowMaximize: true,
showClose: true,
width: 800,
height: 600,
dialogReturnValueCallback: silentCallback};
SP.UI.ModalDialog.showModalDialog(options);
}
function silentCallback(dialogResult, returnValue) {
}
function refreshCallback(dialogResult, returnValue) {
SP.UI.Notify.addNotification(‘Operation Successful!’);
SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
}
</script>
- Next, modify the URL so that it looks like this:
<a href=”javascript:ModalDialog2(‘http://sharepoint/DispForm.aspx?ID={@ows_ID}‘)“>
- Copy the XSL back to the webpart and save.
Your links should now open in a dialog window.