By: Neil Barkhina
In SharePoint 2010, there is no out of the box way of displaying a library from another site collection. So the best way that I have found is to build your own web part.
Building the web part
You can start by creating a Visual Web Part project in Visual Studio 2010. My personal preference is to put all the codebehind and the visual elements in the user control. This way when you make builds or update the code to layouts using a tool like CKSDev, you don’t need to incur an IISReset. This is a great way to develop because you also get intellisense within the code functions. The only different here is instead of adding USING statements you use the Import page directives to add whatever references you will need.
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Data" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FirmWideUserControl.ascx.cs" Inherits="FirmWide.FirmWideUserControl" %>
<%@ Import Namespace="Microsoft.Office.Server" %>
<%@ Import Namespace="Microsoft.SharePoint.Portal" %>
<%@ Register TagPrefix="OSRVWC" Namespace="Microsoft.Office.Server.WebControls" Assembly="Microsoft.Office.Server, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="OSRVUPWC" Namespace="Microsoft.Office.Server.WebControls"
Assembly="Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls"
Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="MSSWC" Namespace="Microsoft.SharePoint.Portal.WebControls"
Assembly="Microsoft.Office.Server.Search, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
In my case, I was building this web part for a customer which needed to use the UserProfile API to figure out the MySites host site collection, but you can just delete that piece of code if you don’t need it. Then it’s a simple exercise of opening the SPSite object using the SharePoint API and getting the SPListItemCollection from the default view as you can see below. After that I populate the columns I want into a datatable which then gets bound to the grid.
<script runat="server">
private Microsoft.Office.Server.UserProfiles.UserProfileManager profilemanager;
protected void Page_PreRender(object sender, EventArgs e)
{
lblError.Text = "";
SPContext current = SPContext.Current;
SPServiceContext serviceContext = SPServiceContext.GetContext(current.Site);
profilemanager = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);
SPSite site = new SPSite(profilemanager.MySiteHostUrl);
SPWeb web = site.OpenWeb();
SPList list = web.Lists["Firm Wide Documents"];
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("NameText", typeof(string));
dt.Columns.Add("Modified", typeof(string));
dt.Columns.Add("Modified By", typeof(string));
dt.Columns.Add("Image", typeof(string));
SPListItemCollection coll = list.GetItems(list.DefaultView);
int counter = 0;
foreach (SPListItem itm in coll)
{
string img = "<img src=\"/_layouts/images/" +
itm.File.IconUrl + "\" />";
object[] row = { itm.File.ServerRelativeUrl, itm.Name, itm.File.TimeLastModified.ToString(),
itm.File.ModifiedBy.Name, img };
dt.Rows.Add(row);
counter++;
if (counter > 9)
break;
}
grdDocuments.DataSource = dt;
grdDocuments.DataBind();
}
</script>
The trick here is I want the grid to look like the SharePoint grids so I use an SPGridView control. I also want to get the icons to display based on the type of document it is in the library. This is easily done by accessing the File.IconUrl property of the list items. Then I format the Image tag in html and display it using a literal control within an ItemTemplate of the grid.
<asp:Label runat="server" ID="lblError" ForeColor="Red"></asp:Label>
<asp:GridView ID="GridView1" AutoGenerateColumns="true" runat="server">
</asp:GridView>
<SharePoint:SPGridView ID="grdDocuments" runat="server" AutoGenerateColumns="false" >
<HeaderStyle HorizontalAlign="Left" />
<Columns>
<asp:TemplateField HeaderText="Type" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Literal Text='<%# Bind("Image") %>' runat="server" ID="lit1"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:hyperlinkfield DataTextField="NameText" DataNavigateUrlFields="Name" HeaderText="Name" HeaderStyle-HorizontalAlign="Left"/>
<SharePoint:SPBoundField DataField="Modified" HeaderText="Modified" HeaderStyle-HorizontalAlign="Left" />
<SharePoint:SPBoundField DataField="Modified By" HeaderText="Modified By" HeaderStyle-HorizontalAlign="Left" />
</Columns>
</SharePoint:SPGridView>
Setting up Site Permissions
There is one more step you will need to do and that is modifying the site permissions to allow Browse Directories for people that will be viewing this web part.

Adding the Web Part
Once you’re done with all this you can simply add the web part to the site and you’re good to go!

By: Neil Barkhina