Merging document libraries using C#

To continue with my last post about using Content Query web part to merge libraries in same site collection. We didn’t find this solution a elegant one due to lack of control on functionality and styling. For example, My user asks to sort the merge library on document creation date and apply grouping.Though it’s possible if you are very good in XML/XSLT and willing to write ton of code.I have followed custom coding path using C# and here is the solution.

  • Create new Visual Web Part project in Visual Studio 2010 with C# and .NET Framework 3.5
  • Add SPGridView control on UserControl.ascx page,
  AlternatingRowStyle-BackColor="#EEEEEE" />
  • Add following code on UserControl.ascx.cs page
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using Microsoft.SharePoint.Utilities;

namespace WebPartDisplaySiteData.VisualWebPart1
    public partial class VisualWebPart1UserControl : UserControl
         protected void Page_Load(object sender, EventArgs e)

         public void getData()
             DataTable dt = new DataTable();
             DataRow dr;
             DataColumn dc;

             SPSite curSite = SPContext.Current.Site;
             SPWebCollection subSites = curSite.AllWebs;

             dc = new DataColumn("Title", Type.GetType("System.String"));
             dc = new DataColumn("ReferenceNo", Type.GetType("System.String"));
             dc = new DataColumn("Domain", Type.GetType("System.String"));
             dc = new DataColumn("Created", Type.GetType("System.DateTime"));
             dc = new DataColumn("SiteUrl", Type.GetType("System.String"));

             for (int i = 0; i < subSites.Count; i++)
                 SPListCollection lists = subSites[i].Lists;

                 foreach (SPList list in lists)
                     if (list.Title == "Published Documents")
                         SPQuery myquery = new SPQuery();

                         myquery.Query = "<GroupBy><FieldRef Name='Domain' />
                                         </GroupBy><OrderBy><FieldRef Name='Domain' /></OrderBy>";

                         SPListItemCollection items = list.GetItems(myquery);

                         foreach (SPListItem item in items)
                             if (item != null)
                                 dr = dt.NewRow();

                                 dr["Title"] = item["Title"];
                                 dr["ReferenceNo"] = item["ReferenceNo"];
                                 dr["Domain"] = item["Domain"];
                                 dr["Created"] = item["Created"];
                                 dr["SiteUrl"] = subSites[i].Url + 
                                 "/Published documents/Forms/DispForm.aspx?ID=" 
                                 + item["ID"].ToString(); ;


             dt.DefaultView.Sort = "Domain";

             HyperLinkField colTitle = new HyperLinkField();
             colTitle.HeaderText = "Title";
             colTitle.DataTextField = "Title";
             string[] SiteUrl = { "SiteUrl" };
             colTitle.DataNavigateUrlFields = SiteUrl;
             colTitle.NavigateUrl = "SiteUrl";

             BoundField colReferenceNo = new BoundField();
             colReferenceNo.DataField = "ReferenceNo";
             colReferenceNo.HeaderText = "ReferenceNo";

             BoundField colDomain = new BoundField();
             colDomain.DataField = "Domain";
             colDomain.HeaderText = "Domain";

             BoundField colCreated= new BoundField();
             colCreated.DataField = "Created";
             colCreated.HeaderText = "Created";

             spLibraryView.AllowGrouping = true;
             spLibraryView.AllowGroupCollapse = true;
             spLibraryView.GroupField = "Domain";

             spLibraryView.DataSource = dt;
  • Build and deploy the project. Add this web part and you should see the output like,

Note: I’m looping all sites in current site collection and reading only specific library which is “Published Documents” . Modify logic based on requirement.


2 Comments on “Merging document libraries using C#”

  1. Kolten says:

    I keep getting an error “The name ‘spLibraryView’ does not exist in the current context”
    This occured directly upon first build. Normally, this is caused by not having a link between the ascx and the code-behind, but in this case I am not sure that’s the problem as it is occuring on a new project without any modifications to inherits or codebehind entries.
    here is my ascx page:

    …Pretty standard! My cs file is a direct copy from above. Any idea what’s wrong?


  2. Alex says:

    Remember to set the spgridview’s EnableViewState=”false”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s