Sharepoint data view web part toolbar sorting default - sharepoint

I have a data view web part on a page which is displaying a table of data.
I want to allow users to choose which column to sort by. I think the only way to do this is go into the data view properties, and enable the toolbar with sorting. This works and allows the user to select a column to sort using a dropdown list of columns. The default is 'none', and when the page first loads, there is no sorting applied (rows are shown by their ID, which is pretty much random).
I want the default sort to be a particular column - i.e. when the page is first opened, I want the data to be sorted by a 'Surname' column. I can't figure out how to achieve this using the toolbar sort.
I have tried specifying an ORDER BY SURNAME in the SQL statement which gets the data - however this ends up overriding the toolbar sort, so sorting is always by surname, regardless of the toolbar sort selection.
There is another 'sorting' option in the data view properties which allows you to specify a sort, however again this overrides everything and renders the toolbar sorting option useless (selectiong something else has no effect).
I'm thinking surely there must be a solution to this simple problem?
The generated code for the sorting toolbar is below. I have tried making the surname the selected item in the dropdown box by changing to - this makes the surname column selected in the dropdown by default but the sorting isn't actually applied.
Any ideas?
<table cellSpacing="0" cellPadding="2" border="0" class="ms-toolbar" style="margin-left: 3px; margin-right: 5px;">
<tr>
<td id="dvt_tb_sort" nowrap=""><table><tr><td nowrap="" class="ms-toolbar"><nobr>Sort by: <select>
<xsl:variable name="clvar1_dvt_sortfield">&apos; + this.options[this.selectedIndex].value + &apos;</xsl:variable>
<xsl:variable name="clvar2_dvt_sortfield">&apos; + this.options[this.selectedIndex].fieldtype + &apos;</xsl:variable>
<xsl:variable name="clvar3_dvt_sortfield">&apos; + this.options[this.selectedIndex].title + &apos;</xsl:variable>
<xsl:variable name="clvar4_dvt_sortfield">&apos; + this.options[this.selectedIndex].sorttype + &apos;</xsl:variable>
<xsl:attribute name="OnChange">javascript:<xsl:value-of select="ddwrt:GenFireServerEvent(concat('NotUTF8;dvt_sortfield={', $clvar1_dvt_sortfield, '};dvt_sortdir={', $dvt_sortdir, '};dvt_sorttype={', $clvar4_dvt_sortfield, '}'))" /></xsl:attribute>
<option value="">None</option>
<option value="Location">
<xsl:if test="$dvt_sortfield='Location'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Location</option>
<option value="Email">
<xsl:if test="$dvt_sortfield='Email'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Email</option>
<option value="Mobile">
<xsl:if test="$dvt_sortfield='Mobile'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Mobile</option>
<option value="Position">
<xsl:if test="$dvt_sortfield='Position'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Position</option>
<option value="Telephone">
<xsl:if test="$dvt_sortfield='Telephone' and not($dvt_sorttype='number')">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Telephone</option>
<option value="Telephone" sorttype="number">
<xsl:if test="$dvt_sortfield='Telephone' and $dvt_sorttype='number'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Telephone(Number)</option>
<option value="Forename">
<xsl:if test="$dvt_sortfield='Forename' or $dvt_sortfield=''">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Forename</option>
<option value="Surname">
<xsl:if test="$dvt_sortfield='Surname'">
<xsl:attribute name="selected">yes</xsl:attribute>
</xsl:if>
Surname</option>
</select><a>
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="$dvt_sortdir='descending'">javascript:<xsl:value-of select="ddwrt:GenFireServerEvent(concat('dvt_sortfield={', $dvt_sortfield, '};dvt_sortdir={ascending}'))" /></xsl:when>
<xsl:otherwise>javascript:<xsl:value-of select="ddwrt:GenFireServerEvent(concat('dvt_sortfield={', $dvt_sortfield, '};dvt_sortdir={descending}'))" /></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="$dvt_sortfield" ddwrt:cf_ignore="1"><img border="0">
<xsl:attribute name="src">
<xsl:choose>
<xsl:when test="$dvt_sortdir='descending'"><xsl:value-of select="ddwrt:FieldSortImageUrl('Asc')" /></xsl:when>
<xsl:otherwise><xsl:value-of select="ddwrt:FieldSortImageUrl('Desc')" /></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:choose>
<xsl:when test="$dvt_sortdir='descending'">Descending</xsl:when>
<xsl:otherwise>Ascending</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</img></xsl:if>
</a></nobr></td></tr></table></td><td width="99%"></td>
</tr>
</table>

James,
Search for the parameter binding labeled dvt_sortfield, then manually write in: Default="FIELDNAME" line.
This should do the trick.
ParameterBinding Name="dvt_sortfield" Default="ID" Location="Postback;Connection"
If you need to change the default sort there is also a sort parameter called dvt_sortdir and you can set it again in the ParameterBinding tag.
Dan

What happens if you select the Surname column first in the SQL?

Related

XSL to Excel: Choose not processing formula

I declared a variable with a simple Excel formula (not the formula I will eventually use, just something simple for testing)
<xsl:variable name="nistcci" ss:Formula="=RC19"></xsl:variable>
Then I am trying to use a Choose to determine if the attribute data is empty, then return based on that determination.
<Cell ss:StyleID="stig_rules"> <!-- IA Control(s) -->
<Data ss:Type="String">
<xsl:choose>
<xsl:when test="STIG_DATA/VULN_ATTRIBUTE[node()='IA_Controls']/../ATTRIBUTE_DATA != ''">
<xsl:value-of select="STIG_DATA/VULN_ATTRIBUTE[node()='IA_Controls']/../ATTRIBUTE_DATA" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$nistcci"/>
</xsl:otherwise>
</xsl:choose>
</Data>
This works if I do something simple like:
<xsl:variable name="nistcci">sean</xsl:variable>
But isn't working with a formula within a referenced variable.
Thanks for the help. Sean.
It's not clear what you want your variable to contain.
It is quite clear that the xsl:variable instruction cannot have a ss:Formula attribute, or any other attribute except select and (in XSLT 2.0) as.
Your variable needs to be constructed as:
<xsl:variable name="myVar" select="myExpr"/>
where myExpr must be a valid XPath expression.
Alternatively, you could use something like:
<xsl:variable name="myVar">
<xsl:attribute name="ss:Formula">=RC19</xsl:attribute>
</xsl:variable>
to construct a variable that holds an attribute.

Xstl global variable set in for each and used after foreach

First of all, I'm new to XSLT.
I'm working with Sharepoint list and I need to get a link to show up if there's data in specific quarters. If there's no data in a certain quarter, I need to have a label that says so.
So what I did is that i created a foreach loop for each data of the same Month of a given year.I know that i cannot re-assing a variable in xslt but I dont know how to do what I want.
Here's a sample of my code. Since I'm working with Sharepoint i dont have acces to the XML. :/
<xsl:variable name="DataQ1" select="'False'"/>
<xsl:variable name="DataQ2" select="'False'"/>
<xsl:variable name="DataQ3" select="'False'"/>
<xsl:variable name="DataQ4" select="'False'"/>
<xsl:for-each select="../Row[generate-id()=generate-id(key('MonthKey', substring(#Date,6,7))[substring('#Date',1,4) = $varYear)][1])]">
<xsl:variable name="currentMonth" select="number(substring(#Date,6,7))"/>
<xsl:choose>
<xsl:when test="$currentMonth >= 1 and $currentMonth $lt;=4">
<!--set $DataQ1 to true-->
</xsl:when>
<xsl:when test="$currentMonth >= 4 and $currentMonth $lt;=7">
<!--set $DataQ2 to true-->
</xsl:when>
<xsl:when test="$currentMonth >= 7 and $currentMonth $lt;=10">
<!--set $DataQ3 to true-->
</xsl:when>
<xsl:otherwise>
<!--set $DataQ4 to true-->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<div>
<xsl:choose>
<xsl:when test="$DataQ1= 'True'">
<a>
<xsl:attribute name="href">
<xsl:value-of select="www.example.come"/>
</xsl:attribute>
<xsl:value-of select="'LinkToDataofQ1'"/>
</a>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'There's no data for this quarter.'"/>
</xsl:otherwise>
</xsl:choose>
</div>
You use the key function in your example code but you didn't post the declaration of your key. But I think you can achieve what you want with the following code:
<div>
<xsl:choose>
<xsl:when test="../Row[substring(#Date, 1, 4) = $varYear and substring(#Date, 6, 2) >= 1 and substring(#Date, 6, 2) < 4]">
LinkToDataofQ1
</xsl:when>
<xsl:otherwise>There's no data for this quarter.</xsl:otherwise>
</xsl:choose>
</div>
Some other notes:
In your test for Q1 you wrote $currentMonth <= 4. I think what you want is $currentMonth < 4.
To extract the month from #Date you used substring(#Date, 6, 7). The third argument to substring is the length of the substring, not the end index. So you'll probably should write substring(#Date, 6, 2).
Instead of <xsl:value-of select="'string'"/>, you can simply write string.

XSL / XSLT - Adding a value-of as a style attribute for a DIV

I'm using SharePoint 2010 and the Content Query Web Part to output a list of dates from a SharePoint list. The display of this list is controlled by an XSL stylesheet called ItemStyle.xsl
I have made progress with the general appearance but would now like to add one of the fields it retrieves as a background/style attribute.
I believe the problem I am having relates to the xsl value-of select having a closing bracket at the end of the tag and therefore inadvertently closing my DIV. Can someone look at the code below and suggest alternative way of printing the CategoryColour within the opening DIV.
I also will occasional have "xmlns:ddwrt" inserted into the html where I would expect to at least see "style:background...."
Many Thanks
<xsl:stylesheet
version="1.0"
exclude-result-prefixes="x d xsl msxsl cmswrt"
xmlns:x="http://www.w3.org/2001/XMLSchema"
xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<!-- PrettyCal Template -->
<xsl:template name="PrettyCal" match="Row[#Style='PrettyCal']" mode="itemstyle">
<xsl:variable name="Start"><xsl:value-of select="#EventDate" /></xsl:variable>
<xsl:variable name="End"><xsl:value-of select="#EndDate" /></xsl:variable>
<xsl:variable name="AllDay"><xsl:value-of select="#AllDayEvent" /></xsl:variable>
<xsl:variable name="Location"><xsl:value-of select="#EventLocation" /></xsl:variable>
<xsl:variable name="CategoryColour"><xsl:value-of select="#EventCategoryColour" /></xsl:variable>
<div class="upcoming-events" style="background: {CategoryColour}" ><xsl:value-of select="$CategoryColour"/>
<h2 class="event-title">
<a>
<xsl:attribute name="onClick">
javascript:SP.UI.ModalDialog.showModalDialog({ url: '/services/marketing/Lists/College%20Calendar/DispForm.aspx?ID=<xsl:value-of select="#ID" />', title: 'Event Details' }); return false;
</xsl:attribute>
<xsl:value-of select="#Title" /></a></h2>
</div>
</xsl:template>
</xsl:stylesheet>
You have:
<div class="upcoming-events" style="background: {CategoryColour}" ><xsl:value-of select="$CategoryColour"/>
You actually wanted:
<div class="upcoming-events" style="background: {$CategoryColour}" ><xsl:value-of select="$CategoryColour"/>
Your missing a $ in front of the variable CategoryColour.

DataView WebPart in sharepoint designer

I would like to display the list items using DataView WebPart and I am successful so far. But I would like to show the items in two columns for each row, instead of one columns per one row. How can i achieve this.
<tr>
<xsl:if test="position() mod 2 = 1">
<xsl:attribute name="class">ms-alternating</xsl:attribute>
</xsl:if>
<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
<td class="ms-vb" width="1%" nowrap="nowrap">
<span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(#ID))" ddwrt:ammode="view"></span>
</td>
</xsl:if>
<xsl:variable name="ImageURL">
<xsl:value-of select="#ImageURL" />
</xsl:variable>
<td class="ms-vb">
<img alt="" src="{$ImageURL}" />
</td>
</tr>
I would like to show the items from a list in two columns and the table should be increased dynamically based on the number of items. Can someone guide me on how to achieve this.
Actually you can achieve this more easily with Item Lister Web Part. Try to check that out.

Outputting SharePoint Hyperlink Column as URL

I have some document URLs stored in a Sharepoint publishing column. When I output the info into a HTML page using:
<xsl:value-of select="#[ColumnName]" />
in ItemStyle.xml, I get [url], [document name] in the page. I would like to display this as a URL can anyone help with the XSL?
You could use:
<xsl:value-of select="substring-before(#[ColumnName],',')"/>
or whatever the separator is.
Thanks everyone, in the end I figured out the following based on a post at sguk
<xsl:variable name="Doc">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="Title" select="#DocumentLink1"/>
</xsl:call-template>
</xsl:variable>
with the following a tag code:
<a href="{substring-before($Doc,',')}">
<xsl:value-of select="substring-after($Doc,',')" />
</a>
or for an image:
<xsl:variable name="Image">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="Title" select="#img" />
</xsl:call-template>
</xsl:variable>
with the following img tag:
<img src="{substring-before($Image,',')}" alt="{substring-after($Image,',')}" />
I'm posting the solution back here as this proved ludicrously hard to figure out (probably my fault as I don't really 'get' XSL) but just in case anybody is looking for it, this code outputs images or links from the 'Hyperlink or Picture' column type in Sharepoint.
Another thing you can do is to take a list that shows URLs properly (like a Links list) and use SharePoint Designer to convert it to a DataView WebPart. In there will be the proper XSL for doing the conversion.
The easiest way to do this is with SharePoint designer:
click the field that shows "http://link, description"
a box with an > will appear, click it and you will get a "common xsl:value-of tasks" flyout.
It will have the field name and type, with the type set to "text".
Change the type to "hyperlink" and you will get a box allowing you to format the hyperlink. It will have all the necessary xsl already populated but you can input your own text or remove the link.
This hopefully helps. It shows "Project Site" when the hyperlink is entered and spaces when not.
<!--Project Site--><TD Class="{$IDAAO2UG}">
<xsl:variable name="Field" select="#Project_x0020_Site" />
<xsl:choose>
<xsl:when test="substring-before(#Project_x0020_Site, ', ')=''"><xsl:value-of select="substring-after(#Project_x0020_Site, ', ')" /></xsl:when>
<xsl:otherwise><A HREF="{substring-before(#Project_x0020_Site, ', ')}">
<xsl:choose>
<xsl:when test="substring-after(#Project_x0020_Site, ', ')=''"><xsl:value-of disable-output-escaping="no" select="substring-before(#Project_x0020_Site, ', ')" /></xsl:when>
<xsl:otherwise><xsl:value-of select="substring-after(#Project_x0020_Site, ', ')" /></xsl:otherwise>
</xsl:choose>
</A></xsl:otherwise>
</xsl:choose>
</TD>
In SharePoint 2013 you have to do things a bit differently because the #Url attribute is is no longer delimited with a comma. There is now a .desc sub property of #Url. Below is an example of how this works hopefully this saves someone else some time.
<xsl:template name="dvt_1.rowview">
<xsl:if test="string-length(#URL) > 0">
<div class="link-item item">
<a title="{#Comments}" target="_blank" href="{#URL}">
<xsl:value-of select="#URL.desc" />
</a>
</div>
</xsl:if>
</xsl:template>

Resources