Display a list in another site (Cont’d)

Two months ago I published a simple method to display a SharePoint list in another site.

A few days ago, Nathan posted the following comment:

The one (minor) inconvenience I have found is that when you click the ID or Title field to navigate to the full record (dispform.aspx), the Close button will redirect you to the default view of the source list […] Would it be possible to modify the script in order to append something like Source=location.href to the ID/Title hyperlink? That way, the Close button on dispform.aspx would redirect you back to the starting page.

So here we go. As Nathan points out, SharePoint offers a very convenient “Source” querystring that tells where the user should be redirected after an action is completed.

This is the modified JavaScript version:

<!-- Load and display list - iframe version -->
<!-- Questions and comments: Christophe@PathToSharePoint.com -->

<DIV id="ListPlaceholder"><IMG src="/_layouts/images/GEARS_AN.GIF"></DIV>

<!-- Paste the URL of the source list below: -->


<script type="text/javascript">
function DisplayThisList()
{
var placeholder = document.getElementById("ListPlaceholder");

var displaylist = null;
var sourcelist = document.getElementById("SourceList");

try {
   if(sourcelist.contentDocument)
      // Firefox, Opera

      {displaylist = sourcelist.contentDocument.getElementById("WebPartWPQ1") ;}
   else if(sourcelist.contentWindow)
      // Internet Explorer

      {displaylist = sourcelist.contentWindow.document.getElementById("WebPartWPQ1") ;}
   else if(sourcelist.document)
      // Others?

      {displaylist = sourcelist.document.getElementById("WebPartWPQ1") ;}
}
catch(err) { alert ("Loading failed");}

displaylist.removeChild(displaylist.getElementsByTagName("table")[0]);
var allDescendants = displaylist.getElementsByTagName("*");
for (i=0;i<allDescendants.length;i++) {
allDescendants&#91;i&#93;.removeAttribute("id");
allDescendants&#91;i&#93;.removeAttribute("onclick");
allDescendants&#91;i&#93;.removeAttribute("onfocus");
allDescendants&#91;i&#93;.removeAttribute("onmouseover");
try {
if (allDescendants&#91;i&#93;.href.indexOf("?") > 0) {allDescendants[i].href = allDescendants[i].href + "\&Source=" + location.href;}
else {allDescendants[i].href = allDescendants[i].href + "?Source=" + location.href;}
}
catch(err) {}
}
placeholder.innerHTML = displaylist.innerHTML;
}
</script>

And the modified jQuery version:

<!-- Load and display list - jQuery version -->
<!-- Questions and comments: Christophe@PathToSharePoint.com -->
<DIV id="ListPlaceholder"><IMG src="/_layouts/images/GEARS_AN.GIF"></DIV>

<script type="text/javascript">
// Paste the URL of the source list below:
var SelectedView = "http://domain.com/SiteCollection1/SourceSite/SourceList/MyView.aspx";
$("#ListPlaceholder").load(SelectedView+" #WebPartWPQ1 .ms-listviewtable",function() {
$("#ListPlaceholder *").removeAttr("id").removeAttr("onclick").removeAttr("onfocus").removeAttr("onmouseover");
$("#ListPlaceholder a").each(function() { 
if ($(this).attr("href").indexOf("?") > 0) {$(this).attr("href", $(this).attr("href")+"\&Source="+location.href);}
else {$(this).attr("href", $(this).attr("href")+"?Source="+location.href);}
});
});
</script>

See the original post for more details. In particular, if you are using MOSS remember to replace WebPartWPQ1 with WebPartWPQ2 in the above scripts.

Update (March 24th): I have modified yesterday’s post to make the scripts work for document libraries as well. I have also corrected the jQuery version (thanks to Peter Allen for alerting me).

Update [6/16/2009]: I have modified the jQuery code for better performance. Thanks Danny van Loon for the advice!

Display the Quick Launch in a Web Part page

quicklaunchMost pages on your SharePoint site display a left navigation, also known as “Quick Launch”. However, new Web Part pages that you add through the Create menu do not have it.

So how can we add the Quick Launch to these pages, using only the SharePoint UI? Here again, the iframe can help. On our Web Part page we are going to:
1/ load the site home page in the background
2/ extract the Quick Launch
3/ include it in our page
4/ do some cleanup to avoid conflicts with the rest of the page

This is very similar to the method I described to display a list in another site – just here everything happens within the same site.

Continue reading