SharePoint Events

  5/13/2013 - Conference: SharePoint Summit 2013
  5/21/2013 - Webcast: SharePoint 2013 and ECM: Content Migration and Storage
  5/22/2013 - Webcast: Managing CAD in SharePoint
  5/23/2013 - Webcast: SharePoint Document Automation and E-Forms for Financial Services
  5/24/2013 - Webcast: What's New in Search for SharePoint 2013

 SharePoint Videos

  Why SharePoint 2013
  SharePoint 2013 Launch
  SharePoint 2013 Migration and Governance
  SharePoint 2013 and Enterprise Content Management
  Top Benefits of SharePoint 2013
  What's New in Business Intelligence in Office and SharePoint 2013
  SharePoint and Office 2013 Integration
  SharePoint 2013 Infrastructure Preview
  SharePoint, Lync, and, Exchange in the Cloud with Office 365
  Advanced Reporting in SharePoint with Microsoft Power View

 Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
1 - 10 Next
Displaying a Document Library From a Different Site Collection

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.

The_SharePoint_Blog_Displaying_a_Document_Library_from_a_Different_Site_Collection

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!

The_SharePoint_Blog_Displaying_a_Document_Library_from_a_Different_Site_Collection

By: Neil Barkhina

        

Comments

Nirushan

HI Neil, Can you give me step by step proc to enable Browse Directories??? Thanks
at 3/12/2012 1:21 AM

David Pinilla

Don't suppose there's an easy way to handle folders, is there?  This chokes with null exceptions when you get to hitting folders.
at 5/22/2012 5:15 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy