Archive

Posts Tagged ‘xsl’

Customization of SharePoint 2010 navigation – publishing site

February 12th, 2010 2 comments

In my previous blog, I customized the SharePoint 2010 publishing site. Now I will give you some idea that how you can create customize the navigation according to new look and feel. There are different ways to customize the navigation of SharePoint. I like to use a custom control which returns navigation in the form of xml and then I transform it from xsl. In this way you can easily control the output of the navigation or any other control.  

<SafeControl Assembly=SalmanSPControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=49908bd985021621 Namespace=SalmanSPControls TypeName=* Safe=True />You need to add a reference in your SharePoint master page. It would be like this. 

<% @ Register Tagprefix=”SWC” Namespace=”SalmanSPControls” Assembly=”SalmanSPControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=49908bd985021621″ %>  

Then I created a class for navigation inherited from ASP .Net Menu and xml based control class. This class is not doing much but just adding all the items in xml. It’s getting these items from Menu items. In the master page when you use this control you need to specify the data source id. If you search in your master page you can find a tag like this.  

<PublishingNavigation:PortalSiteMapDataSource EnableViewState=”false” ID=”leftNavDataSource” Runat=”server” SiteMapProvider=”CombinedNavSiteMapProvider” StartFromCurrentNode=”true” StartingNodeOffset=”1″ ShowStartingNode=”true” TreatStartingNodeAsCurrent=”true” TrimNonCurrentTypes=”Heading” />  

There will be two tags like above one for global navigation and other for current navigation. For left navigation you have to look the current navigation site map data source. In portal site map data source you can also change the SiteMapProvider, depending on your needs. You can use the current or combine or global. These site map providers are defined in web.config. You can find the difference of these site map providers in SharePoint documentation.
Now you have to replace the existing navigation control with your new custom navigation control. After that your master page changes are over. Now your control is ready and will display instead of default SharePoint current navigation. Here is how your navigation control will look like.  

<SWC:Navigation cacheperpage=”true” EnableViewstate=”false” ID=”LeftNav” Runat=”server” DataSourceID=”leftNavDataSource” Transform-XslName=”leftnav.xsl” Transform-CacheOutput=”false”/> 
 
But this control is just returning xml. You need to transform it in an html through some xsl. For this I created one xsl file and use some style classes to give it new look.  Here is the piece of code I use in my xsl to transform navigation xml into new look. 
 
<xsl:template match=”/>
    <xsl:if test=count(Navigation/Item) &gt; 0>
        <div id=nav-y>
             <p>
                 <a href={Navigation/Item/@Url}title=”{Navigation/Item/@Title}>
                      <xsl:value-of select=”Navigation/Item/@Titledisable-output-escaping=”yes/>
                 </a>
             </p>
             <ul>
                 <xsl:apply-templates select=”Navigation/Item>
                     <xsl:with-param name=”levelselect=”1/>
                     <xsl:with-param select=”@Titlename=”sectionTitle/>
                 </xsl:apply-templates>
              </ul>
        </div>
    </xsl:if>
 </xsl:template>
 
<xsl:template match=”Item“>
    <xsl:param name=”level” />
    <xsl:param name=”sectionTitle” />
    <xsl:choose>
        <xsl:when test=”$level != 1“>
            <li>
                <xsl:if test=”@Selected=’True’ or @Current=’True“>
                    <xsl:attribute name=”class“>Selected</xsl:attribute>
                </xsl:if>
                <a href=”{@Url}title=”{@Title}“>
                    <xsl:value-of select=”@Titledisable-output-escaping=”yes“/>
                </a>
                <xsl:if test=”count(Item) &gt; 0 and @Current = ‘True“>
                    <ul>
                        <xsl:apply-templates select=”Item“>
                            <xsl:with-param name=”levelselect=”$level + 1” />
                            <xsl:with-param select=”@Titlename=”sectionTitle” />
                        </xsl:apply-templates>
                    </ul>
                </xsl:if>
            </li>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates select=”Item“>
                <xsl:with-param name=”levelselect=”$level + 1” />
                <xsl:with-param select=”@Titlename=”sectionTitle” />
            </xsl:apply-templates>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
 
 So now after doing all these changes my new site will look like this.
  
   

Hope you enjoyed customization of SharePoint 2010 navigation in publishing sites.  

Cheers

Paging in xsl

October 22nd, 2009 3 comments

You might have seen a lot of paging options using xml and xsl. In such options you need to pass some parameters from xml and then you do the paging on it. I have implemented such pagings before. But this time it’s a simple example of creating all the paging from xsl.

Recenetly I was using a third party dll to generate xml, my task was to implement paging on returned xml without doing any change in server side code. So here is the solution.

 

<xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>

 

      <xsl:output method=html indent=yes omit-xml-declaration=yes />

 

  <!– Define some primary variables–>

  <xsl:param name=qs-p />

  <xsl:variable name=newsCount select=count(rows/row) />

  <xsl:variable name=pageMaxLimit select=10 />

 

  <xsl:template match=/>

    <!– This page constant has been defined just to make some calculation for starting

    number and end number, we will asume constant value as 0 if there is no page variable

    value in query string–>

    <xsl:variable name=page_constant>

      <xsl:choose>

        <xsl:when test=$qs-p = ”>

          <xsl:value-of select=0/>

        </xsl:when>

        <xsl:otherwise>

          <xsl:value-of select=$qs-p – 1/>

        </xsl:otherwise>

      </xsl:choose>

    </xsl:variable>

   

    <xsl:apply-templates select=/rows/row >

      <!– Calculate the records start number and end number, later we will mactch

      records position for starting number and end number to display records for current page–>

      <xsl:with-param name=start_no select=($page_constant * 10 ) + 1 />

      <xsl:with-param name=end_no select=($page_constant * 10 ) + 10 />

    </xsl:apply-templates>

 

    <xsl:if test=$newsCount &gt; 10>

      <xsl:call-template name=paging />

    </xsl:if>

  </xsl:template>

 

  <xsl:template match=row>

    <xsl:param name=start_no />

    <xsl:param name=end_no />

    <!– Display the records for the current page number by making a check on their position –>

    <xsl:if test=position() &gt;= ($start_no) and position() &lt;= ($end_no)>

      <p>

        <a href=/{substring-after(FileRef/text(), ‘#’)} class=bold>

          <xsl:value-of select=Title/>

        </a>

        <br/>

        <xsl:value-of select=Intro/>

      </p>

    </xsl:if>

  </xsl:template>

 

  <xsl:template name=paging>

    <br></br>

    <xsl:variable name=pageCount select=floor($newsCount div 10) + ($newsCount mod 10 != 0) />

    <!–Call the template to create page numbered links,

    send loop iterator ‘i’, page count ‘count’ and current page ‘pageNo’ as parameters.–>

    <xsl:call-template name=page_links>

      <xsl:with-param name=i select=1 />       

      <xsl:with-param name=count select=$pageCount />     

      <xsl:with-param name=pageNo select=$qs-p />

    </xsl:call-template>

  </xsl:template>

  <!–Loop to create page numbers.–>

  <xsl:template name=page_links>

    <xsl:param name=i      />

    <xsl:param name=count  />

    <xsl:param name=pageNo />

   

    <xsl:if test=$i &lt;= $count>

      <xsl:variable name=page_no>

        <xsl:value-of select=$i – 1 />

      </xsl:variable>

      <a href=?p={$i} style=padding-right:2px;>

        <xsl:choose>

          <xsl:when test=$i = $pageNo or $pageNo = ”>

            <strong>

              <xsl:value-of select=$i />

            </strong>

          </xsl:when>

          <xsl:otherwise>

            <xsl:value-of select=$i />

          </xsl:otherwise>

        </xsl:choose>

      </a>

      <xsl:text xml:space=preserve> </xsl:text>

    </xsl:if>

    <xsl:if test=$i &lt;= $count>

      <xsl:call-template name=page_links>

        <xsl:with-param name=i>

          <xsl:value-of select=$i + 1/>

        </xsl:with-param>

        <xsl:with-param name=count>

          <xsl:value-of select=$count/>

        </xsl:with-param>

        <xsl:with-param name=pageNo>

          <xsl:value-of select=$pageNo/>

        </xsl:with-param>

 

      </xsl:call-template>

    </xsl:if>

  </xsl:template> 

</xsl:stylesheet>

Categories: XSLT Tags: , ,

Replace function for xsl

July 17th, 2009 4 comments

If you are using xsl 1.0, you will face small things missing from the functions set. String manipulation is very common and you need in every programming and scripting language. In xsl 1.0 most of the functions are missing. Today I’m going to give you idea that how can we create and use Replace function for strings.

<xsl:template name=string-replace>
   <
xsl:param name=text />
   <
xsl:param name=pattern />
   <
xsl:param name=replace-with />
  
<xsl:choose>
      <
xsl:when test=contains($text, $pattern)>
         <
xsl:value-of select=substring-before($text, $pattern) />
         <
xsl:value-of select=$replace-with />
         <
xsl:call-template name=string-replace>
            <
xsl:with-param name=text select=substring-after($text, $pattern) />
            <
xsl:with-param name=pattern select=$pattern />
            <
xsl:with-param name=replace-with select=$replace-with />
         </
xsl:call-template>
      </
xsl:when>
      <
xsl:otherwise>
         <
xsl:value-of select=$text />
      </
xsl:otherwise>
   </
xsl:choose>
</
xsl:template>

Here how to use this string-replace function. Here I’m creating a variable name MYTextReplaced, and then calling the function replace-with. I’m passing the orignal text (MyText), pattern string ([Title]) to search and replace with value (Replaced-text).

For example value of MyText is “This is a sample [Title], created by Salman”.

<xsl:variable name=MyTextReplaced>
   <
xsl:call-template name=string-replace>
      <
xsl:with-param name=text select=xml/MyText />
      <
xsl:with-param name=pattern select=‘[Title]‘ />
      <
xsl:with-param name=replace-with select=‘Replaced-Text’ />
   </
xsl:call-template>
</
xsl:variable>

Now here I’m displaying the replaced string. The output string will be “This is a sample Replaced-Text, created by Salman”. One more thing to notice is that this is a replace-all method and will replace all the occurrence of searched text.
<xsl:value-of select=$MyTextReplaced disable-output-escaping=yes />
 
I hope you enjoy it.

Cheers.

Categories: XSLT Tags: , ,

Use querystring values in xsl

July 17th, 2009 Comments off

It’s just a small tip of the day. Sometime when you work with xsl you need to use querystring values for some processing or display. It’s very simple to use querystring values without posting them from server side code in your xml document.

On top of your xsl document just define a variable like this

<xsl:param name=qs-title/>

here title is a querystring variable and now you can use this xsl variable any where like this

<xsl:value-of select=$qs-title />
 

 

Is not it simple enough?

Categories: XSLT Tags: ,