ConfigLabs

May 23, 2007

Using Query String and Data view WebPart.

Filed under: WSS 3.0 — Jag @ 6:21 am

When you need to pull out information from the top level SharePoint site and use it on a sub site, the answer you often get is “Use Data View”. The actually problem is very less resources point you in the right direction of ‘HOW’. So here in this article I will show you, how to use data view web-part to access the content for top level sites and set filters using query string. There is KB article from Microsoft @ http://support.microsoft.com/kb/831093. This article is for WSS 2.0 and front page 2003. For 2007 version of Share Point read further.

Follow this process.

  1. Open your sub site in SharePoint Designer 2007.

  2. Click on ‘Insert Data View’ from the Data View Menu option.

  3. This will add a Data View Webpart to your site which looks like;

    Note: Don’t panic looking at DataForm WebPart. There isn’t much of difference here. I will write about this in my later posts.

  4. Now it is time for us to connect to the data source. In this case our data source is sitting on a top level site (Site Collection). So to connect to the data source (here, it’s a list), click on ‘Connect to another Library’ as shown in the image below.

  5. This brings you a pop up asking for the site collection location. Key in our top level site location and give it a name.

  6. Once added, DataSource Library window will show you the new data sources from the different site.

  7. Select the required data source. For the sake of this article I am selecting a list called Projects under SharePoint Lists and click on it and select ‘Show Data’. This shows you the data fields in the list.

  8. Select the fields required. Here I will select two fields Project URL and Practice Group. And Click on the Button ‘Insert Selected Fields as‘ and again click on Multi Item View.

  9. Now we need to filter this list based on the site name. In my case the site name is equal to client name. So to dynamically populate this list, the best practice is to use query string. Hence we need to create a new parameter, say sitename. Click on Parameters option from Common Data View Tasks. This brings up a ‘Data View Parameters’ window, here click on ‘New Parameter‘ button to add a new parameter. Name the new parameter as sitename and select the Parameter source to be Query String. Give a query string variable as we need to use this in code.

  10. Before you save the work, we need to make a slight alteration in the aspx page. Select the webpart and go to code view. In there look for the following code

    <ParameterBinding Name=”sitename” Location=”QueryString(siteName)” DefaultValue=”"/> and change it to <ParameterBinding Name=”sitename” Location=”Postback;Connection;QueryString(siteName)” DefaultValue=”"/>. Now we can use this query string to set filters. Example;

    Note: 1. You will not see anything on the designer as it unaware of the query string.

              2. Using this query string to pass the required variable through the URL.

    In my next article I will show you how to use this query string in code behind. Untill then bye.

38 Comments »

  1. Very Usefull. Thanks for posting.

    Comment by Rai Umair — May 28, 2007 @ 1:28 am

  2. I discoverd how Server Parameters work. Basically there is bug in SP and to see the Servers Parameters in action just make the changes and come back to the page using a new browser or retyping the same url. Refresh won’t work.

    Comment by Rai — June 5, 2007 @ 5:27 am

  3. When will you publish your post about how to pass the querystring? i did it and it worked, but when i click on a page from the menu, it doesnt have the querystring variable, so the dataview doesnt work. i also need to filter it by the name of the site im currently in.
    thank you!!

    Comment by Eli — June 15, 2007 @ 5:34 pm

  4. Hi Eli

    I am presently busy with the projects and hence i am unable to post anything. But anyway, if you want to have the query string in the menu context you can achieve it by generating the quick launch items by using the SPNavigationalNode OM. I guess that will solve your navigational problem. But you will still have breadcrumb trail without the query string. So i figured out another way to filter the data view webpart with out using the query string. I will post the article very soon. Thanks for following the article.

    Jag

    Comment by Jag — June 16, 2007 @ 3:14 am

  5. Great read. Same as Eli, I would like to filter my data by the name of the site I’m currently in.
    For example, on my top site I have a custom list which contains info regarding my subsites such as site names; site descriptions; etc. So, from my subsite, I would like to obtain info from the top site depending on the name of the subsite.
    Cheers,
    Rick

    Comment by Rick — June 25, 2007 @ 10:15 pm

  6. Hi Rick

    Thanks,

    Your case exactly suits with mine. you can display the info (site names, site descriptions etc) on a sub site and filter the data based on the site name. As said previously, you can achieve this by using the query string. But using query string you will suffer the pain of worng navigational links from various pages. So there is a work around for this problem. Ok what it is?

    Don’t use query string !!

    I have a work around for filtering the data view webpart (with the info from top level site) based on the site name by tweaking the the dataview webpart code using the site name. I will post this in detail, when I get some time to breath.

    Jag

    Comment by Jag — June 25, 2007 @ 11:48 pm

  7. Thanks Jag… I am looking forward to your new post.
    Cheers,
    Rick

    Comment by Rick — June 26, 2007 @ 11:48 am

  8. Hi guys
    I’ve been working on a similar requirement and I thought I would share how I managed to do this. Every data query contains the variable “httpvdir”. This is very similar to the “current user” or “current date” variables. To get the data view to show this variable you need to add a parameter of

    You also need to add a Parameter declaration using the statement below (just find the others)

    Once this is done you can use the variable $httpvdir anywhere in the xslt. This produces the current site in formatted as http://server/sitename .You can display it as follows

    Now you only want to get the sitename right?
    Create a new xslt template as below (not my code but found it on the net somewhere (big thanks to person that wrote it)

    Then in the section before the first template call add a variable section as below

    What this does is call the template every time the “‘/’” (notice the single quotes in the double quotes) is found in the httpvdir string and deletes everything before the “’/’”
    We end up with a variable then that can be used as follows to filter what is displayed.

    This took me about 5 days to work out being relatively new to xslt, so I hope this helps someone else in the future.
    Cheers

    Comment by Riccardo Gozzi — June 27, 2007 @ 12:25 pm

  9. well that didn’t work. if you want the details mail me at riccardo.gozzi@gmail.com
    Cheers

    Comment by Riccardo Gozzi — June 27, 2007 @ 12:27 pm

  10. I filter Data View with parameter from Parameter Source: Server Variable, Server Variable Name: URL. This parameter provides full page path name after http://hostnname.

    Comment by Rustam — July 20, 2007 @ 3:41 pm

  11. [...] 12th, 2007 by Jag In my previous post on using the data view web part and query string, I used the concept of using the query string to know the site name. So that we can filter the data [...]

    Pingback by Filtering Data View Web Part based on Site Name « Footprint of IT — August 9, 2007 @ 8:48 am

  12. @Eli
    Perhaps you can create a control in the page as hidden, and write a little code that will set the value of the control with the site name you are currently in.
    you can then use this control to filter your dataview as dataview has the option to filter data based on a control, just like it has for querystring.
    try it out and let me know if u have any problem…

    Comment by chris — August 31, 2007 @ 8:11 am

  13. I have a requirement to Print the QueryString above the webpart so anyone please help me out .

    How to display the query string data in the page containing the web part ???

    It s very urgent

    Comment by Atul Sharma — December 19, 2007 @ 2:43 pm

  14. hey could some body help how to filter lookup field values filter based on query string!!!

    Comment by Aswath — January 4, 2008 @ 11:07 am

  15. hi

    nice artice…..

    it would be great …if u tell me about session ,form,server variable parameters using dataview webpart …

    Comment by var — January 30, 2008 @ 11:06 am

  16. Hello Jag,

    Have posted the next part of this article, if so please mention the url for that article.

    This article was really helpful, Thanks,

    Atish.

    Comment by Atish — January 31, 2008 @ 12:51 pm

  17. Thanks for that article.

    Can you think of a way that a data view accepts a webpart connection coming from a SPListFilterWebPart (ITransformableFilterValues with AllowMultipleVaules=”True” I suppose) and filters the list with all (1 or many) parameters logically ANDed together?

    -George

    Comment by George — March 8, 2008 @ 6:09 pm

  18. [...] To find more information from the source here [...]

    Pingback by Border Crossing Stats » Using Query String and Data view WebPart. Footprint of IT — March 11, 2008 @ 9:21 am

  19. Hey George,

    I will research on how to enable a data view webpart to accept a web part connection and blog about it very shortly.

    Thanks for the idea anyway.

    Jag

    Comment by Jag — March 13, 2008 @ 1:03 am

  20. Very useful, thanks

    Comment by Indranil — April 10, 2008 @ 6:14 pm

  21. HI Jag

    Great Post! I have a similar scenario but it is kind of reverse. So I have a site and several subsites. All the pages in subsites use a page layout. Inside the page layout I configured a DVWP to pull in a list. And this list has to change as per the subsite user is in. This is how I am thinking….
    But real scenario is to display a respective document list in each subsite. I would like to avoid the configuration of CQWP on each page they try to build.

    Any ideas will be much appriciated. By the way do have second part to this article. Keep blogging and you are excellent.

    Comment by Bhanu — June 26, 2008 @ 10:07 am

  22. Hi Bhanu,

    You can change (filter) the list on each site as long as you got a relevant column in the list. Attaching a DVWP in the Page Layout is a good idea. Give me some more insight into the requirement and I can help you with it.

    Thanks for reminding me off the second part for this article I completely lost track of it. I will work on it very shortly.

    Jag

    Comment by Jag — June 26, 2008 @ 10:39 am

  23. Hi Jag,
    Thank you very much for the quick response.
    More details about the requirement. I have a main site and several subsites each subsite has its own document library. When user is in subsiteA then user needs to get documents from document library of subsiteA. If I have a DVWP in pagelayout how can I configure it to refer the document library of SubsiteA or SubsiteB?

    If I have one document library at root level then yes I can use filter and pass in filter parameters.

    Thank you,

    Comment by Bhanu — June 27, 2008 @ 2:49 am

  24. Okay Guys,
    I am fairly new to MOSS v3 and SPD 2007. So I truely have no idea what I am doing but hopefully someone here can give me some guidance. Here is my problem.

    I am trying to create a data view that filters info that is on a list form web part.

    For example a projects page. When that project is open and the info is displayed I want the dataview I have added to show only information for that project. Not all projects.

    But I can’t get it to work. I have found a template from microsoft that does something similar. I tried copying all the settings from that dataview but it shows either all or nothing.

    I have created a param. called ProjectID with the query string “ID” (Matches existing dataview on page that works) I then created a filter and had the project field equal [ProjectID] (This is not the same as the existing dataview, The existing dataview just has “” in the value field)

    But when I do that the data goes away. I’m sure its something simple.

    Any help would be great.

    Comment by Kevin — August 7, 2008 @ 11:13 am

  25. Hi jag

    I am trying to retrieve a part of the url (not querystring) and want to store it in a variable so that i can pass it to my sql string.

    for example: http://cars/mazda/pages.default.aspx.

    I need mazda from that and put it in $param. On the page i can’t use any asp code, its all xml. any help or comments appreciated. thanks

    Comment by Deep — October 14, 2008 @ 12:23 pm

  26. OK, let me repeat that… Tell me about my soft conspiracy Wanna joke?) Samantha: Doctor, my son thinks that he is a TV channel changer. What are the odds that you can save him? Doctor: Remote!!

    Comment by seapkeews — October 29, 2008 @ 1:09 pm

  27. Deep,
    Try this link :
    http://www.jwc3.net/2008/05/how-to-get-url-in-xsl.html
    Cheers

    Comment by Χρήστος — October 30, 2008 @ 5:44 pm

  28. Hi,

    I need to change the code of an existing sharepoint custom web part. This webpart was developed by someone else. I recently started learning sharepoint. I dont know where to find this code to make the changes. Can you please help.

    thank you

    Comment by Neesha — February 7, 2009 @ 1:48 am

  29. Hi,

    thank you for your reply.

    The problem is, i’m not sure if the source code exists. Because i dont know where to check for the code? can you tell me as to where should I be looking to get this code?

    Comment by Neesha — February 10, 2009 @ 3:09 am

    • Hi Neesha,
      What web part are working on? With out this details I am unable to help you.
      Thank You,
      Jag

      Comment by Jag — February 13, 2009 @ 11:28 am

  30. Hi,

    I have a dataview web part which takes querystring parameters.
    My requirement is :

    Custom List – Article has metadata of Category and Filters
    Category and filter are lookups and can have multiple values.

    Article A stores data in the following format
    – Category [Product Research;Ingredients;Applications)
    – Filter – Cheese; Milk; Dairy;Non-Dairy;Butter;Butter Milk

    How can I have the filter setup so that all the possible categories(6) and filters(11)can help filter the Data view to get the right results.
    A custom webpart changes the querystring based on the values selected by the user with parameters say NC1,NC2…for Categories and NF1,NF2..for filters.

    Fetch all articles which have the Filters and Category specified.
    A should be fetched if
    1. Category & filter – Product Research,Cheese
    2. Category & filter – Product Research;Ingredients;Applications,Cheese; Milk; Dairy;Non-Dairy;Butter;Butter Milk
    3.Category & filter – Ingredients,Milk
    etc.

    Is there some advance Filter formula that can fetch the records for the data view?

    Comment by Pallavi — September 12, 2009 @ 10:39 am

    • ok this is confusing! So all you wanted to do is set multiple filters based on the query strings. Please correct me. Thank You.

      Comment by Jag — September 17, 2009 @ 10:55 am

  31. yes you are correct.

    Comment by Pallavi — September 17, 2009 @ 2:10 pm

  32. check this link if you haven’t already.
    http://sharepointxperiments.wordpress.com/2009/08/03/multiple-filters-on-a-data-view-webpart/

    Comment by Jag — September 17, 2009 @ 2:32 pm

  33. Awesome…Thanks that is it!!

    Comment by MOSSConsultant — September 17, 2009 @ 11:28 pm

  34. in my all items page when i click new
    a pop window appear saying wrong url

    Comment by Mustafa — October 14, 2009 @ 9:50 pm

  35. [...] can easily create a parameter in a Data View Web Part as shown here, and it worked quite well.  Once someone clicked on a project, the Project Details opened up as [...]

    Pingback by SharePoint Steve » Filtering Data View Web Parts By The Query String With Out of the box WebParts — November 10, 2009 @ 2:10 am


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.