Click here to Skip to main content
15,894,405 members
Articles / Web Development / ASP.NET

Export BDC(Business Data Catalog) Data to Excel/PDF

Rate me:
Please Sign up or sign in to vote.
4.99/5 (112 votes)
23 Jul 2009CPOL2 min read 84.2K   573   38  
Way to export search results(BDC datalist/Enterprise Search) to Excel/PDF
<%@ Assembly Name="mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" %> 
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>   
<%@ Assembly Name="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %> 
<%@ Assembly Name="Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ WebHandler Language="C#" Class="ExcelExporter" %> 
 
using System;  
using System.Web;  
using System.Web.UI;  
using System.Web.SessionState;  
using System.Collections;  
using System.Collections.Generic;  
using Microsoft.SharePoint;  
using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;  
using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;  
using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;  
 
public class ExcelExporter : IHttpHandler, IReadOnlySessionState  
{  
  public bool IsReusable {  
    get { return false; }  
  }  
  public void ProcessRequest(HttpContext context) {  
    SPSite siteColl = SPContext.Current.Site;  
    SPWeb site = SPContext.Current.Web;  
    Boolean first = true;  
 
    context.Response.ContentType = "application/vnd.ms-excel";  
    try  
    {  
        NamedLobSystemInstanceDictionary ObjInstances = ApplicationRegistry.GetLobSystemInstances();  
        LobSystemInstance objInstance = ObjInstances[(string)context.Session["Entity"]];  
        Entity ObjEntity = ObjInstance.GetEntities()[(string)context.Session["Instance"]];  
        MethodInstance ObjMethodInstance = ObjEntity.GetMethodInstances()[(string)context.Session["Method"]];  
          
        DbEntityInstanceEnumerator ObjEnum = (DbEntityInstanceEnumerator)ObjEntity.Execute(ObjMethodInstance, ObjInstance);  
 
        List<string> displayedFields = (List<string>)context.Session["Fields"];  
        while (ObjEnum.MoveNext())  
        {  
            DbEntityInstance dbInstance = (DbEntityInstance)ObjEnum.Current;  
            string s = dbInstance.ToString();  
            Microsoft.Office.Server.ApplicationRegistry.MetadataModel.View v = dbInstance.ViewDefinition;  
            if (first)  
            {  

                FieldCollection titles = v.Fields;  
                foreach (Field title in titles)  
                {  
                    if (filterFields)  
                    {  
                        if (displayedFields.Contains(title.Name))  
                        {  
                            string titleName;  
                            if (title.ContainsLocalizedDisplayName)  
                            {  
                                titletitleName = title.LocalizedDisplayName;  
                            }  
                            else  
                            {  
                                titletitleName = title.DefaultDisplayName;  
                            }  
                            context.Response.Write(titleName);  
                            context.Response.Write('\t');  
                        }  
                    }  
                    else  
                    {  

                        string titleName;  
                        if (title.ContainsLocalizedDisplayName)  
                        {  
                            titletitleName = title.LocalizedDisplayName;  
                        }  
                        else  
                        {  
                            titletitleName = title.DefaultDisplayName;  
                        }  
                        context.Response.Write(titleName);  
                        context.Response.Write('\t');  
                    }  
                }  
                context.Response.Write("\n");  
                first = false;  
            }  
 
 
            FieldCollection c = v.Fields;  
            foreach (Field f in c)  
            {  
                if (filterFields)  
                {  
                    if (displayedFields.Contains(f.Name))  
                    {  
                        TypeDescriptor td = f.TypeDescriptor;  
                        Object vName = dbInstance.GetFormatted(f.Name);  
                        context.Response.Write(vName);  
                        context.Response.Write('\t');  
                    }  
                }  
                else  
                {  

                    TypeDescriptor td = f.TypeDescriptor;  
                    Object vName = dbInstance.GetFormatted(f.Name);  
                    context.Response.Write(vName);  
                    context.Response.Write('\t');  
                }  
            }  
            context.Response.Write("\n");  
        }  
        ObjEnum.Dispose();  
    }  
    catch (Exception exception)  
    {  
        context.Response.Write("Error reading:\tBDC");  
        context.Response.Write("\n");  
        context.Response.Write("Entity:\t" + context.Session["Entity"]);  
        context.Response.Write("\n");  
        context.Response.Write("Instance:\t" + context.Session["Instance"]);  
        context.Response.Write("\n");  
        context.Response.Write("Method:\t" + context.Session["Method"]);  
    }  
  }  
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Johnson Manuel Devadoss ("Johnson Smith") is the Technical Lead/Administrator/Architect, and is responsible for Solution Architecture at global energy clients. Working exclusively with SharePoint Products and Technologies for 10+ years, he has had the pleasure to provide SharePoint training, mentoring, and consulting to dozens of to Fortune 50 clients.With over 10+ years experience in Microsoft Products and Technologies, he has a proven record of helping clients deliver global "Leading-Edge" technology integration solutions.

He has participating in the architectural design, development, customization and integration efforts of enterprise solutions involving Collaboration, Portals, Enterprise Content Management, Business Process and Forms, and Business Intelligence. Also, he is responsible for ensuring that the solutions are implemented in an efficient manner and ensures the satisfaction of the users by providing exceptional application technical support, by researching issues, and by problem solving and interaction with business users and vendors throughout all project phases.

Specialties:

Related areas of expertise are:

Technology Adoption & POC Management
Enterprise Portal Integration Solutions (SAP & MOSS)
Human Workflow & Systems Orchestration Solutions
Service Oriented Architectures (SOA) Implementations

Comments and Discussions