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,
<SharePoint:SPGridView
  runat="server"
  ID="spLibraryView"
  AutoGenerateColumns="false"
  RowStyle-BackColor="#DDDDDD"
  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)
        {
            getData();
        }

         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"));
             dt.Columns.Add(dc);
             dc = new DataColumn("ReferenceNo", Type.GetType("System.String"));
             dt.Columns.Add(dc);
             dc = new DataColumn("Domain", Type.GetType("System.String"));
             dt.Columns.Add(dc);
             dc = new DataColumn("Created", Type.GetType("System.DateTime"));
             dt.Columns.Add(dc);
             dc = new DataColumn("SiteUrl", Type.GetType("System.String"));
             dt.Columns.Add(dc);

             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.Rows.Add(dr);
                             }
                         }
                     }
                 }
             }

             dt.DefaultView.Sort = "Domain";

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

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

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

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

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

             spLibraryView.DataSource = dt;
             spLibraryView.DataBind();
         }
    }
}
  • 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.

Advertisements

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?

    Thanks

  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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s