Data View Web Part Parameters Based on Server Variables

I wanted to get at the site name to use in a Data View Web Part’s (DVWP) filter.  An example is where you have a site per project, with the project number being the site name and you want to based various filters on that value.  To do this, you can set up a parameter within the DVWP based on the value of the IIS Server Variable "URL" and parse out the project number using XSL.

Here are the steps (you will be doing all of this within SharePoint Designer — I’ll assume that you know how to set up DVWPs already):

  • Once you have your DVWP set up on your page, click on the Common Data View Tasks (the little > twiddle button in the upper right of the DWVP when you’ve highlighted it) and choose "Parameters…"
  • Click the "New Parameter" button
  • Give your parameter a name, and set the "Parameter Source" to "Server Variable"
  • For "Server Variable Name", type the text value of the Server Variable.  There’s no help here from Designer; you need to type the value exactly right or it won’t work.  See the link below for your options.  In this case, we’ll type "URL" (without the quotes).  Click OK when you are finished.
  • Now click on "Filter:".  Under "Field Name", select the column that you’d like to filter on, the "Comparison" (in this case "Equals"), and choose the Parameter that you just created in the dropdown for "Value".  Check the "Add XSLT Filtering" checkbox and click "Edit".
  • Enter the XSL to trim the value that you want out of the URL.  for example, if all of your projects are below http://servername/Projects/, then the XSL would look like [substring-after(substring-before($URL,’/default.aspx’), ‘/Projects/’)]

Now the filter that you wanted should be in place.  XSL has its vagaries, so if you get an error at any step, use your old friend Ctrl-Z to step back and try again.

A full reference of the available IIS Server Variables is at this link:
http://msdn2.microsoft.com/en-us/library/ms524602.aspx

Note: I previously blogged on this method referencing a post from Maarten Eekels, but there are many more options, as outlined in the link above.

Similar Posts

40 Comments

  1. Hi Marc,

    I posted over on the other forum, and I think my problem is with extracting what I need from the URL.

    So on my page, if I display my $URL variable just as it is.
    I get the entire URL of the site, minus the querystring at the end. According to this page, that is expected right?
    http://msdn.microsoft.com/en-us/library/ms524602.aspx

    So now I’m stuck at how to retrieve just what I want,
    To troubleshoot, I’m trying to display this, to see if I only get the string. But I get nothing.

    If the page I’m adding this too lives here
    http://servername/sites/directoryname/SitePages

    Is what I have above correct, for extracting the string from the URL?

    I hope I’m close to making this work, I’ve been stuck on it for a couple days now.

    1. Chris:

      It’s not clear from what you’ve posted above exactly what you’re trying to do, but if you set up a new parameter bound to the QUERY_STRING server variable, you’ll get the Query String. (Also not sure what other forum you mean!)

      M.

  2. In your last comment, you mentioned to post on the endusersharepoint forum, about my problem.
    http://www.endusersharepoint.com/STP/viewtopic.php?f=9&t=1933&start=10

    What I’m trying to do is filter my DVWP using another DVWP (as a dropdown).
    I thought this blog post was for doing this, but I don’t see it mentioned to use QUERY_STRING in the instructions.

    I think I may have misunderstoon how this is suppose to work?

    I guess I’m confused now, should I not be using URL?

    1. Sorry. I totally lost the thread, as I didn’t realize that “Chris” was the same as “illegal_operation”. I’ll go and answer over at STP.

      M.

  3. Hi Marc,

    Thanks for the nice article. In SPD > DVWP > Filter I can see the option to filter where
    “FieldName contains Param(ServerVariable(URL))”

    Is there anyway so I can instead have:
    “Param(ServerVariable(URL)) contains FieldName”?

    Thanks a lot!

  4. Thanks for reply Marc,

    I will appreciate any guidance or the right website link on how to do this with XSL, I am a newbie trying to Google high and low.

    Thanks again.

    1. Manu:

      It’s hard to tell exactly what you are trying to do, but once you have the parameter defined, something like this:

      <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[contains($URL, @ColumnName)]"/>
      

      M.

      1. Thanks again, sorry to be so stupid but where exactly would I enter this?

        I need to filter a list (show items only where CurrentPageURL contains text in ColumnABC).

        From DVWP I can filter Where ColumnABC contains CurrentPageURL but I need to do opposite as above.

        Really appreciated!

        1. It finally worked, thanks to Marc.
          For anybody else having same issue here is what I did:
          To filter list items by page URL (Where PageURL contains text in ColumnABC):

          – In SPD > Convert List Web Part to XSLT View.
          – Click on the arrow on top right of the web part.
          – Parameters > add new param called SV-URL and select Server Variable option from drop down. Close dialog.
          – Again click on the arrow on top right of web part
          – Click on Filter
          – Add XSLT Filter
          – In Edit XPATH Xpression type this
          [contains($SV-URL, @ColumnABC)]

          That’s all!

          Thanks Marc, your XSL query nailed it.

  5. It is probably me doing some thing daft. However, I am using some xlst pinched from my temporary dvwp in a core search results web part . I want to display the sub sites for a given web app and allow security trimming to handle what my users can see or not

    So I use a system var:

     
    <ParameterBinding Name="ServerURL" Location="ServerVariable(SERVER_NAME)" DefaultValue=""/> 
    

    In my xlst I add an extra col which is displaying the expression I have in my xlst filter

     
       <!-- Debug -->
    	<td class="ms-vb">
    				<xsl:value-of select="concat($ServerURL,'/projects')"/>
    	</td>
    

    Results:

    $ServerURL is completely empty!!!!! Help!! SOS!!!!

    Regards

    Daniel

    1. Daniel:

      It looks like you’re doing things right. Make sure that you have the line:

      <xsl:param name="ServerURL"/>
      

      at the top of your XSL. Without that the $ServerURL variable should be unrecognized (therefore giving you an error). If you’re getting no value, then try testing with the server variable URL. I use that one all the time, and that will at least show you that you have the syntax all correct.

      M.

  6. Marc

    it looks like one of those days as this is one of three issues I am trying to resolve.

    I am suspecting a SPD delete the cache issue I have tried

    In the xlst I place

    '

    searchresultsfile.xml

    But still


    just results in ‘projects’ in my dvwp

  7. Hi Marc
    I want to pass the Full Name of the user to a Parameter (SQL stored proc), can you please tell me how i can do this.

  8. Stinking heck Marc, every time I spend a day racking my brain to work something out, one of your blog posts ends up having the answer. Bless your cotton socks!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.