The XSLT and SharePoint and HttpVDir

Some might say both these technologies come from Old Nick himself , but I’m afraid I’ll have to disagree right there.  XSLT has always been one of my all time favourite technologies, it’s uses are endless and there are many places in SharePoint where it’s a definite bonus to know XSLT to a good level.

So I just wanted to give you an idea of how neat a technology XSLT is and how integrated into the SharePoint stack it is. 

Recently I was working on a custom list view which needed some custom XSLT and on this page I needed to create a link to another custom listview page.  Now the problem was that the URL of our test box was http://ourtest.url/sites/intranet whereas the production farm was http://ourproduction.url .This created an issue in that if we weren’t careful each time we deployed to production we’d have to change the URL in the XSLT manually.

So initially our XSLT looked thus:

<ul class="nav"> 
  <li>
    <a href="/sites/intranet/list/Forms/NotWon.aspx">Not Won</a>
  </li>
  <li>
    <a href="/sites/intranet/list/Forms/Won.aspx">Won</a>
  </li>
</ul>

Thankfully there is a simple solution to this issue and that’s to use one of the XSLT Parameters that SharePoint has thankfully provided for us. 

XSLT Parameters are value handed to us from outside of the Style sheet in this case by SharePoint.  If you navigate to 14/TEMPLATE/LAYOUTS/XSL you will see the OOTB XSLT files used by SharePoint and you’ll see one called mail.xsl.  If you open that up you see a whole bunch of <xsl:param /> tags with difference names.  About half way down you’ll see HttpVDir.  This is really handy as it gives us the full URL of the current SPWeb item.  Check out MSDN for full details (in a second).  And this value is available to you in any custom XSL you care to write for your listviews.  So in order to construct a URL that would update automatically when deployed to other farms, we simply changed the above XSLT to read as follows:

<ul class="nav">
  <li>
    <a href="{$HttpVDir}/sites/intranet/list/Forms/NotWon.aspx">Not Won</a>
  </li>
  <li>
    <a href="{$HttpVDir}/sites/intranet/list/Forms/Won.aspx">Won</a>
  </li>
</ul>

Notice the shortcut syntax in XSLT used with attributes where the node or variable or in this case parameter appears simply in curly braces. Parameters in XSLT are always preceded by a $ sign. Job done.  Have a play around with some of the other parameters and you’ll be pleased that you did!

Cheers

Dave Mc

Advertisements

About davemcmahon81
Software Developer & Architect, User Group Leader, Speaker, Writer, Blogger, Occasional Guitarist, Man-made Global Warming Sceptic, Climate Change Believer, General Optimist but most of all proud Husband and Dad ...

2 Responses to The XSLT and SharePoint and HttpVDir

  1. Raja says:

    Hello Dave,

    I am trying to write a SharePoint Script. But when i combine the two scripts its not generating out put. Below is my script hope you can help me fix this.

    #***********************************************
    #Display Current System Time
    #***********************************************
    Get-Date
    #**********************************************
    #Displays SharePoint Services on Services.msc
    #**********************************************
    Get-Service W3SVC, SPAdminV4, SPSearch4, SPUserCodeV4, SPTraceV4, SPWriterV4, WebAnalyticsService,ProfSvc |Select DisplayName, Status
    #***********************************************
    #Within the Windows PowerShell we can add SharePoint snap in to run SP cmdlets
    #***********************************************
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    #*****************************
    #List all the Webapplications
    #*****************************
    Get-SPWebApplication | Select URL, Status
    #*************************
    #List all SP Services Status on all servers in the FARM where it is hosted
    #*************************
    Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
    $services = new-object system.collections.sortedlist
    $servers = (get-spfarm).servers
    foreach ($server in $servers)
    {
    foreach($service in $server.serviceinstances)
    {
    if ($service.status = “Online”)
    {
    $s = $service.typename
    if ($services.contains($s))
    {
    $serverlist = $services[$s]
    $servername = $server.name
    $services[$s] = “$serverlist – $servername”
    }
    else
    {
    $services[$s] = $server.name
    }
    }
    }
    }
    $services

    #********************************
    #List All Service Applications Status
    #********************************
    Get-SPServiceApplication | Select URL, Status

    #*******************************************
    #Adds IIS snap in, We don’t have IIS Snap in installed
    #*******************************************

    Import-Module WebAdministration
    Get-WebItemState IIS:\AppPools\11ca010994114457bbf73ec80bcd7b85
    Get-WebItemState IIS:\AppPools\11ca010994114457bbf73ec80bcd7b85
    Get-WebItemState IIS:\AppPools\582e9ccb5b6041289eb20cf6dc7a0dda
    Get-WebItemState IIS:\AppPools\bd5c0c275496426a9b0afdfc6324a4fd
    Get-WebItemState IIS:\AppPools\Classic .NET AppPool
    Get-WebItemState IIS:\AppPools\SharePoint – 2100
    Get-WebItemState IIS:\AppPools\SharePoint – 55555
    Get-WebItemState IIS:\AppPools\SharePoint – 80
    Get-WebItemState IIS:\AppPools\SharePoint Central Administration v4
    Get-WebItemState IIS:\AppPools\SharePoint Web Services Root

    When i run them individually they are working, i want them to run combine and generate out put in Excel

    • Not quite sure what you’re attempting to achieve here, some more background please? Also apologies for slow reply – just been very very busy and also in the US, got back last Friday!

      Cheers

      Dave

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: