Search This Blog

Thursday, February 12, 2015

Export To Excel Using SPContext.Current

SPContext.Current is NULL for IHttpHandler

While trying to implement IHttpHandler in SharePoint 2010, got the Error : SPContext.Current is "null" and cannot retrieve the context of the site.

Client call:
function ExportCSV() {
    var _reportStartDate = $("#_startDate").val();
    var _reportEndDate = $("#_endDate").val();

    window.location = webUrl + "/_layouts/TimeSheet/ExportCSV/ExportCSV.ashx?startDate=" + _reportStartDate + "&endDate=" + _reportEndDate;
}
Error code:
public void ProcessRequest(HttpContext context)
{
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
       // 'SPContext.Current' null reference error
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
        {
            using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
            {
               // codes goes here
            }
        }
    });
}

Fixed:
 HttpHandler
  try
            {
                //SPWeb currentWeb = SPContext.Current.Web;
                SPSite curSite = SPContext.Current.Site;
                SPWeb curWeb = SPContext.Current.Web;

                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPSite site = new SPSite(curSite.ID))
 
                    {
                        using (SPWeb Web = site.OpenWeb(curWeb.ID))
                        {
                            SPList list = Web.Lists[LIST_NAME];

                            SPQuery q = new SPQuery();
                            q.ViewFields = VIEW_FIELDS;
                            q.Query = strQuery;

                            SPListItemCollection items = list.GetItems(q);

                            if (items.Count == 0)
                            {
                                HandleError(context, "Report is empty ");
                            }
                            else
                            {
                                listDataTable = items.GetDataTable();
                                listDataTable.Columns.Remove("ID");
                                listDataTable.Columns.Remove("Created");
                                listDataTable.Columns.Remove("Modified");
                                listDataTable.Columns["Author"].ColumnName = "Submitted By";
                                listDataTable.Columns["WorkTypeText"].ColumnName = "Work Type";
                                listDataTable.Columns["Project"].ColumnName = "Project Name";
                                listDataTable.Columns["ActivityName"].ColumnName = "Activity";
                                ExportToSpreadsheet(context, listDataTable);
                            }            
 
                        }
                    }
                });

            }
            catch (Exception ex)
            {
                HandleError(context, ex.Message);
                return;
            }

tem in client /simulated client environment. The customer will usually perform user Acceptance Testing (UAT).

No comments:

Post a Comment