Thursday, December 6, 2012

SPDataSource

Example:
1.  <SharePoint:SPDataSource runat="server"
2.  DataSourceMode="List"
3.  SelectCommand="<View></View>"
4.  id="dataviewdatasource1" >
5.  <SelectParameters>
6.  <WebPartPages:DataFormParameter Name="ListID"
7.  ParameterKey="ListID"
8.  PropertyName="ParameterValues"
9.  DefaultValue="00000000-0000-0000-0000-000000000000" />
10. <asp:Parameter DefaultValue="{sitecollectionroot}" Name="WebUrl"></asp:Parameter>
11. </SelectParameters>
12. </SharePoint:SPDataSource>

DataSourceMode

Data Source Mode is a type of WSS data to query. The valid modes are:
  • List - Query items in a single list. Multiple rows of list data are returned. Each row represents a list item. This mode is similar to using an instance of the SPQuery class against a list. This mode can get lookup fields. This is the default mode.
  • ListOfLists - Query property values of all lists in a Web site. Multiple rows of data are returned. Each row represents a list.
  • CrossList - Query items in multiple lists in multiple Web sites in the same site collection. Multiple rows of list data are returned. Each row represents a list item. This mode is similar to using an instance of the SPSiteDataQuery class against lists in a site collection. This mode cannot get lookup fields. used for recursive query.
  • Webs - Query property values of all sub sites of a specified Web site. Multiple rows of data are returned. Each row represents a Web site.
  • ListItem - Query field values in a list item. A single row of list data is returned. The row represents a single list item.

SelectCommand

  • Select Command is an actual query for that data type.
  • Some modes don’t require a query at all.
  • In List mode, the SelectCommand roughly maps to the SPQuery object and accepts Collaborative Application Markup Language (CAML) fragments to specify things like the set of return fields, filtering, and sort order.
  • Briefly, this command will draw the wanted fields we want to draw from the data source to the DVWP. For example, if we want to draw the field “Title”, the code will be: SelectCommand="<View><ViewFields><FieldRef Name=’Title’/></ViewFields></View>"
  • But, if we want to create a query, the code will be written in CAML.SelectParameter
  • Select Parameter is each command expects a number of different parameters.
  • These parameters can refer to the parameter binding’s collection in the Data View, as well as take most ASP.Net types of parameters like Control and QueryString.

Where from? <asp:Parameter DefaultValue="…" Name="WebUrl"></asp:Parameter>

  • {sitecollectionroot} – will get the data from the root site: <asp:Parameter DefaultValue="{sitecollectionroot}" Name="WebUrl"></asp:Parameter>
  • If we want a specify sub-site: <asp:Parameter DefaultValue="/sub-site" Name="WebUrl"></asp:Parameter>

Parameteres

List and ListItem modes require the “DataFormParameter” list ID or list name, for example (CrossList for example need that parameter but not using it):
<WebPartPages:DataFormParameter Name="ListID" ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="00000000-0000-0000-0000-000000000000" />
<WebPartPages:DataFormParameter Name=" ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="<List Name>" />
  • ListID – GUID that corresponds to a List’s ListID.
  • ListName – Display name for a List.
  • ListItemId – ID for a single item in ListItem mode
  • WebUrl – Url to the web. When not specified, SPDataSource uses the current web to resolve the previous parameters. You can also use this parameter to access lists in other Web sites in the same Site Collection assuming you have permissions to that list.

- Thanks to Eray Chou' post.



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).



Wednesday, October 10, 2012

MasterPageFile Reference

On top of the page there is a line where the master page can be defined manually, or to be the default and custom master page:
<%@ Page language="C#" MasterPageFile=  ../_catalogs/masterpage/<Master Page File Name>.master meta:progid="SharePoint.WebPartPage.Document" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" meta:webpartpageexpansion="full" %>

MasterPageFile Value
Definition
MasterPageFile="~masterurl/custom.master"

Dynamic custom master page defined in the “Site Settings -> Look and Feel -> Master Page” section.
MasterPageFile="~masterurl/default.master"

Defines the master page according to the page layout. On OOTB page layout, the master page will be v4.master for example.
MasterPageFile="../_catalogs/masterpage/Master.master"

Static specific master page definition.

Navigation Controls Values

There are some controls to navigate through in the master page, for example, logo control, site name control and more.
<SharePoint:SPLinkButton runat="server" NavigateUrl="~site/" id="onetidProjectPropertyTitleGraphic">

        <SharePoint:SiteLogoImage name="onetidHeadbnnr0" id="onetidHeadbnnr2" LogoImageUrl="/_layouts/images/siteIcon.png" runat="server"/>
    </SharePoint:SPLinkButton>

NavigateUrl Value
Result
~sitecollection/ or /
Leads to the root welcome page (site collection)
~site/
Leads to the current site welcome page.

Hope it helps,
Me.