Tuesday, November 27, 2012

Sort & Filter on Headers


DVWP offers a tiny feature that can be very useful to the user – “Sort & Filter on Headers”. As same as the OOTB lists and libraries, which their headers offers filtering and sorting columns, it can be added to your data view by simple click:

Under Data View Tools, Design Tab - Mark the “Sort & Filter on Headers” check box:




And now you have this feature activated, displayed like this:
 

Important! This feature doesn’t support CAML Query in the DataSource tag (such as queries and CrossList mode).

Wednesday, November 21, 2012

View Last Page in DVWP XSL

In Data View Web Part, for the user, It's not very comfortable to click again and again on the "Next" button till you've rich the end. So, Add “Last” to the paging in order to move to the last page of items.


The code to insert into the "dvt_navigation" template:

<xsl:variable name="FirstItemOfLastPage">

  <xsl:choose>
    <xsl:when test="$dvt_RowCount mod $RowLimit = 0">
      <xsl:value-of select="$dvt_RowCount - ($RowLimit - 1)"></xsl:value-of>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$dvt_RowCount - ($dvt_RowCount mod $RowLimit) +1"></xsl:value-of>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>

<a class="LastPageLink">
  <xsl:attribute name="href">javascript: <xsl:value-of select="ddwrt:GenFireServerEvent(concat('dvt_firstrow={',$FirstItemOfLastPage,'};dvt_startposition={}'))" />;</xsl:attribute>Last</a>

Sunday, November 11, 2012

Edit Form and New Form Dialog


Open a dialog model to edit current item and add new item to the list via DVWP sometimes is hard to do because of the returned values, and still, need to be modified by substring function or even more. However it can be executed by simple XSL and JS.
Define two parameters to send the as parameters to the function (considering the @FileDirRef returns 12;#/site/Lists/ListName for example):

<xsl:param name="FileURL" select="substring-after(@FileDirRef,';#')" />
<xsl:param name="FileURL2" select="concat('/',$FileURL)" />

wrap the value (title in this case) with <a> tag.

For edit:
<a href="#" onclick="javascript:openEditItemRisk({@ID},'{$FileURL2}')" ><xsl:value-of select="@Title"/></a>

For new item:
<a onclick="openNewItemRisk()">+ Create new item</a>

Attach JS file with this code:
function openNewItemRisk(){
    var options = {
    url: "../Lists/ListName/NewForm.aspx",
        allowMaximize: false,
        showClose: true,
        scroll: 0,
    dialogReturnValueCallback: silentCallbackNewForm};
    SP.UI.ModalDialog.showModalDialog(options);
}
function silentCallbackNewForm(dialogResult, returnValue) {
    location.reload();
}

function openEditItemRisk(currentId,url){
    var options = {
    url: url + "/EditForm.aspx?ID=" + currentId, //Can be changed to DispForm in order to only view the item
    allowMaximize: false,
    showClose: true,
    scroll: 0,
    dialogReturnValueCallback: silentCallbackEditForm};
    SP.UI.ModalDialog.showModalDialog(options);
}
function silentCallbackEditForm(dialogResult, returnValue) {
    location.reload();
}

Numbering Items (Descending as well) In DVWP


Having the default item ID from SharePoint can be a little bit tricky – if you erase an item, you lose the sequential order, or if you are getting the items from a number of lists you will have duplicated ID’s.
So, if the ID is not important, and you only need to number it, you can use the position (add +1 so it will start from 1 instead of 0), For example (in the rowview):

<xsl:value-of select="position()+1"/>

But, if the order should be descending you can use the number of rows and the position. For example:

In the call to rowview:
<xsl:call-template name="dvt_1.rowview">
    <xsl:with-param name="Rows" select="$Rows" />
</xsl:call-template>

In the rowview:

<xsl:template name="dvt_1.rowview">
    <xsl:param name="Rows" />
    <xsl:value-of select="count($Rows) - position() +1"/>
</xsl:template>

It will even work with paging (will preserve the number from previous page and not start over from the current page).