Using a DataSource in a Data View Web Part (DVWP) in a Different Site in SharePoint Designer 2010

For whatever reason, Microsoft in its infinite wisdom seems to have removed the ‘Connect to another library…’ link in the Data Source Library in SharePoint Designer 2010. Maybe that capability is hidden somewhere else now, but as my pal Chris Givens pointed out the other day in his Missing Features from SharePoint Designer 2010 post, it’s not “findable”.

Fortunately there’s a workaround and all of your 2007 Data View Web Parts (DVWPs) should work in 2010. (At least if this is the only issue!) You can simply add the same line into the DataSource that you would get if you used the ‘Connect to another library…’ link in 2007. The net-net in 2007 is that you end up with the WebPartPages:DataFormParameter named WebURL in your DataSource section. That’s the entire difference in the code if you want to access content in a different site: the WebURL parameter.  By setting that parameter to the relative URL of the site where the content resides, you can access content in any other site with a DVWP, just like in 2007. You just have to do it manually in the Code View.

Here’s an example of accessing an Announcements list from the root site in a SharePoint 2010 Site Collection which resides in a subsite called ‘Marc’. Note that I’ve also switched from ListID to ListName in this example. As long as your information architecture is solid, that switch can be useful to make your DVWP code more readable. It also can make your DVWP more portable to other sites or Site Collections.

<DataSources>
  <SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" UseServerDataFormat="true" selectcommand="&lt;View&amp;gt;&lt;/View&gt;" id="dataformwebpart1">
    <SelectParameters>
      <WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="Announcements"/>
      <WebPartPages:DataFormParameter Name="WebURL" ParameterKey="WebURL" PropertyName="ParameterValues" DefaultValue="/Marc"/>
    </SelectParameters>
  </SharePoint:SPDataSource>
</DataSources>

<UPDATE date=2012-05-07>

Note that you may also need to remove several attributes in the WebPartPages:DataFormWebPart header. If you are getting an error, check for attributes like this:

ListId="465831c5-3df1-4259-ba87-b9278007aba5" ListName="{465831C5-3DF1-4259-BA87-B9278007ABA5}"

and remove them. Removing these attributes won’t break the DVWP, and will fix the error.

</UPDATE>

Similar Posts

89 Comments

  1. Hi Marc,

    So this is working for you? I tried this exact thing and when I imported the web part I’d get the error “Webpart cannot be imported”

    Hopefully I was just making a small mistake!

    1. Shawn:

      I’ve tested this by editing a DVWP in a 2010 page directly and also by copying a DVWP from a 2007 installation into a 2010 page. I don’t tend to worry too much about the whole ghosting/unghosting/customization thing. I add my DVWPs into the pages directly and have literally never had a problem with that approach.

      M.

      1. Marc, I am surprised that you were able to copy the whole Web Part from 2007 to 2010, and would agree with Shawn. You should be able to keep the DataSourcesString, but you’ll need to use the new version (14 instead of 12).
        I can’t believe the feature is missing, I’ll keep looking! (it took me a couple weeks to find the view selector on lists).
        The ListName is definitely a handy tip, very useful when you plan to reuse your customizations!
        I wonder what you mean by “never had a problem with that [unghosting] approach”. I think it is well known that unghosting affects performance.

        1. Christophe:

          I’m not positive that I copied every single character of the DVWP from 2007 to 2010, but the code which makes it up is identical. There may be a namespace difference in the xsl:stylesheet definition, perhaps, but I can’t think of anything else which would be different.

          The “well known that unghosting affects performance” thing is, to me, a bugaboo. I’ve never seen a whit of difference whether the page is unghosted or not. I firmly believe that it falls into the category of “SharePoint urban myth”. As long as you are treating your databases well, I don’t believe that there will be any significant different between loading a page from the database and from the WFE file system.

          I’d love to hear that you found the “Connect to another library…” link, but I haven’t been able to find it.

          M.

  2. Hi Marc.
    First thanks for your blog.
    I was using that sucessfully with sp2007, but in 2010 I still have problems. What I’ve done:
    – create a Webpart page test in designer and add my dataview
    – try to use only ListName and then ListName with WebUrl
    => Both work very well when I see the test page in the browser
    – then export the WPart in the Webpart galery and add it to a content page. Now it doesn’t work at all

    Get this error:
    “The page you selected contains a list that does not exist. It may have been deleted by another user.0x81020026”

    I have same kind of error when I export my XLV webpart in the webpart Galery from SP Designer (and choose No to be abble to reuse it ) .

    Cannot find any solution for this bug.

    Any idea ?
    Thanks in advance
    Emmanuel

    1. Emmanuel:

      I think that the issue lies in the export/import step. I think you’d have the same issue in 2007. You have to build the DVWP in the same context as where you want to deploy it, or else the settings won’t work.

      I always just built the DVWP in a copy of the page where I want it to sit. So, for instance, I make a copy of default.aspx and call it default2.aspx. I do my work in default2.aspx and when everything looks good, I just swap the pages.

      M.

  3. Thanks, Marc. You’re a lifesaver! I got the DFWP working finally, thanks to your code. I’d replaced all the ListID with ListName, but was missing this piece. Now I’ve got a web part pointing to a root list :)

  4. Hi, Marc. I thought I had the DFWP working but it’s giving me a “List does not exist” if I try to use it in a subsite; at the root level it works fine with the WebURL=”/”. What am I doing wrong? I’ve deleted and created the DFWP what seems like a hundred times and still can’t figure it out. This is on SP 2010. Thanks.

  5. Thanks, Marc. Hmmm…. From everything I’m seeing, the list is at the root level; it displays it as http://vmdev195/Lists/WebContacts/AllItems.aspx At the subsite level, I’m trying to access it from http://vmdev195/health/SitePages/Home.aspx using the web part, which says list doesn’t exist. From the root, all works well. I’m now trying a content query web part with a custom ItemStyle template to see if that will work better, though I’m getting lost in the XSLT :)

  6. Thanks for the article, it took a few hours before I get it working because I had to also remove “listname” attribute from the WebPartPages:DataFormWebPart tag. Don’t know where it came from, but now it works nicely!

  7. It’s very possible that my knowledge of SharePoint is still too basic and that I might be missing some of the point but It looks like this post only addresses retrofitting a 2007 DVWP so that it will work in a 2010 environment.

    What if I want to do this from scratch in 2010? I’m learning SharePoint for my company and I want to put a DVWP containing the most recent blog posts (from the ‘Posts’ list on a subsite) on the home page of the site.

    I know this is very basic but please bare with me: In order to modify the datasource in the code, I need to have code to modify. Do I need to insert a DVWP and just select SOME list just to get the code on the page? If so does it matter what I choose? Am I missing something obvious?

    Thanks in advance!

    1. Shaun:

      Since your current site doesn’t have a Post list (assuming it isn’t also a blog site), you could add a DVWP with any list as its DataSource and then adapt it to point to the Posts list.

      Alternatively, build the DVWP in the blog site and then copy it into the parent site when you are finished.

      M.

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.