Jack
Jack

Reputation: 91

Display page url SharePoint xslt

I was following this link http://spyralout.com/2009/03/16/current-page-url-using-xsl-for-content-query-web-part/

I want to display query string value on my page. For that I have seen one solution on stackoverflow which I found bit complicated.So my approach is :

1) Get the current url

2) Use substring-after on that url to get the value of query string..

I can explain this with some e.g..

My Page url is www.stackoverflow.com/questions.aspx?display=18..I want to display 18 on my page...

What I have done so far...

1)I have added namespace in my xslt xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"

2) Added a parameter

<xsl:param name=”PageUrl” />

3)Added this value in variable

<xsl:variable name=”DetailPageLink” select=”$PageUrl” />

4) Now when I am trying to use this in substring-after, its not displaying any value...?

<xsl:value-of select="substring-after($DetailPageLink,'=')" />

5) I have also tried

<xsl:value-of select="$DetailPageLink" /> but its not giving any value..?

Any idea where I am going wrong...

Thanks again Dimitre for answering it...but still the problem is same...as I said I don't want to pass url into global parameter..I want it to be dynamic...to answer your questions ..I don't know the logic behind it...I think its because of this namespace what I have used....PageUrl gets the current page url....but when I am trying

xsl:copy-of select="$DetailPageLink"/> or

xsl:value-of select="$DetailPageLink" /> or

xsl:value-of select="substring-after($DetailPageLink,'=')" /> ...

Its not displaying any output...but interestingly its showing page url when I am using

a href="{$DetailPageLink}">Jack /a>

So that means PageUrl is getting the value ....otherwise a tag should not have shown any output...

If you know any other approach to get the current page url , please let me know....

Upvotes: 2

Views: 9713

Answers (4)

To initialize the Global Variable "PathUrl" you must:

1) Add the following line into ParameterBindings

<ParameterBinding Name="PageUrl" Location="ServerVariable(QUERY_STRING)" DefaultValue=""/>

2) Add the following line into the xsl:stylesheet after the ParameterBindings

<xsl:param name="PageUrl"></xsl:param>

3) now you can use the following line to show the URL parameters

<xsl:value-of select="$PageUrl"/>

for other server variables you can visit http://joshmccarty.com/2012/06/using-asp-net-servervariables-in-a-sharepoint-data-view-web-part/

Regards :)

Upvotes: 0

Alan McK
Alan McK

Reputation: 19

The issue here is that the parameter:

xsl:param name="PageUrl"

doesn't actually work on the web page. Even if you did add in the ParameterBinding line (as suggested above). It does appear to work in SP Designer, but when viewed through the browser, the PageUrl parameter returns an empty or Null value. The right parameter to use to return the current page URL is the ServerVariable one. So set up the ParemeterBinding like this (you'll need to add your own angle brackets as this website won't like it if I type them in):

ParameterBinding Name="PageAddr" Location="ServerVariable(URL)" DefaultValue=""/

The PageAddr is just a random name I'm assigning to the Parameter, which won't be confused with the PageUrl one. Next call the Parameter in the StyleSheet, like this:

xsl:param name="PageAddr" /

Now you can call the Parameter anywhere in your XSLT code using $PageAddr .

Be warned this Parameter doesn't work in SP Designer, but will work in the browser.

[Does anyone know why Microsoft thought having one Parameter work in SPD and another work in the browser was a good idea?]

Upvotes: 1

Nils
Nils

Reputation: 989

I think the easiest way to display QueryString information is due a ParameterBinding:

In ParameterBindings add:

<ParameterBinding Name="subsiteLink" Location="ServerVariable(PATH_INFO)" DefaultValue=""/>

In XSL-stylesheet of DataView add:

<xsl:param name="subsiteLink"></xsl:param>
path_info: <xsl:value-of select="$subsiteLink"/><br/>

This is simplified way of displaying data.

Upvotes: 0

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

1)I have added namespace in my xslt xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"

This isn't necessary.

2) Added a parameter

xsl:param name=”PageUrl” />

3)Added this value in variable

<xsl:variable name=”DetailPageLink” select=”$PageUrl” />

4) Now when I am trying to use this in substring-after, its not displaying any value...?

<xsl:value-of select="substring-after($DetailPageLink,'=')" />

5) I have also tried

<xsl:value-of select="$DetailPageLink" /> but its not giving any value..?

Any idea where I am going wrong...

The facts above clearly show that the global parameter $PageUrl either isn't initialized or anyway, its string value is the empty string.

Solution:

You need to set the global parameter $PageUrl with the appropriate string value before initiating the transformation. You may also give it a default value in the select attribute of the global xsl:param and if the default value is picked this proves that the parameter wasn't initialized and passed by the initiator of the XSLT transformation/

Below is correct XSLT code that either uses the default value of $PageUrl or the value that is provided for this global parameter at the time of the initiation of the transformation:

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

 <xsl:param name="pPageUrl" select=
 "'www.stackoverflow.com/questions.aspx?display=18&amp;somethingElse=xyz'"/>

 <xsl:template match="/t">
  <xsl:variable name="vQSPart" select=
  "concat('&amp;', substring-after($pPageUrl,'?'),'&amp;')"/>

  <xsl:value-of select=
    "substring-before
        (substring-after($vQSPart, '&amp;display='),
         '&amp;')
    "/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document (not used), the wanted, correct result is produced:

18

When I set the value of the global parameter outside of the transformation to: 'www.stackoverflow.com/questions.aspx?something=abcd&display=123&somethingElse=xyz' and then again perform the transformation, I again get the correct result:

123

Upvotes: 1

Related Questions