Solved

C# code explaination.

Posted on 2011-09-30
3
219 Views
Last Modified: 2013-12-16
Hi,

I'm new to c# and have been tasked to re-develop an application consisting mainly of the attached code. First I need to understand what it currently does if any experts can help? From what I can make of it, the "Go" method iterates through the specified lists (sharepoint lists), exporting the specified columns (sharepoint list columns defined from the "Event" content type), using the GetDefinition method to attach the columns field contents (defined from the "Title" content type) into excel for reviewing? ...and I'm not too sure what the "Page_Load" methods function is? - do I have to use the query string to initiate the export from my .aspx page?

Thanks.
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SPExcelExport.Exporter.ExcelExporter;

namespace SPExcelExport
{
    public partial class Test : System.Web.UI.Page
    {
        bool _titleOnly = false;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Logger.Type = LogType.Page;
            Logger.Level = LogLevel.Info;
            Logger.LoggingPage = this;

            try
            {
                bool doNotOutput = false;
                if (this.Page.Request.QueryString["export"] == "no")
                {
                    doNotOutput = true;
                }

                int maxItems = 0;
                if (this.Page.Request.QueryString["max"] != null)
                {
                    maxItems = Int32.Parse(this.Page.Request.QueryString["max"]);
                }

                if (this.Page.Request.QueryString["titleOnly"] == "yes")
                {
                    _titleOnly = true;
                }

                if (this.Page.Request.Url.ToString().Contains("localhost"))
                {
                    Test1(doNotOutput, maxItems);
                }
                else
                {
                    Go(doNotOutput, maxItems);
                }
            }
            catch (Exception ex)
            {
                Logger.LogException("Error", ex);
            }

        }

        private void Test1(bool doNotOutput, int maxItems)
        {
            ExcelExporter excelExporter = new ExcelExporter("http://www.abc.com/sites/test", "joebloggs",
                                                            "password", "test2011",
                                                            "Event.xls", "Events",
                                                            this.Page.Request.PhysicalApplicationPath);

            excelExporter.MaxItems = maxItems;
            excelExporter.DoNotOutput = doNotOutput;

            excelExporter.OutputFileDefinition.Columns.Add("Title", typeof(string));
            if (_titleOnly == false)
            {
                excelExporter.OutputFileDefinition.Columns.Add("Modified", typeof (DateTime));
                excelExporter.OutputFileDefinition.Columns.Add("MultiChoice", typeof (string));
                excelExporter.OutputFileDefinition.Columns.Add("MultiLookup", typeof (string));
                excelExporter.OutputFileDefinition.Columns.Add("SingleChoice", typeof (string));
                excelExporter.OutputFileDefinition.Columns.Add("SingleLookup", typeof (string));
                excelExporter.OutputFileDefinition.Columns.Add("Note", typeof (string));
                excelExporter.OutputFileDefinition.Columns.Add("Note2", typeof (string));
                excelExporter.OutputFileDefinition.Columns.Add("Number", typeof (double));
                excelExporter.OutputFileDefinition.Columns.Add("Currency", typeof (decimal));
                excelExporter.OutputFileDefinition.Columns.Add("User", typeof (string));
                excelExporter.OutputFileDefinition.Columns.Add("Link", typeof (string));
                excelExporter.OutputFileDefinition.Columns.Add("Calculated", typeof (decimal));
            }

            ExcelExporterListDefinition definition = new ExcelExporterListDefinition("List12");
            definition.Fields.Add("Title");
            if (_titleOnly == false)
            {
                definition.Fields.Add("Modified");
                definition.Fields.Add("MultiChoice");
                definition.Fields.Add("MultiLookup");
                definition.Fields.Add("SingleChoice");
                definition.Fields.Add("SingleLookup");
                definition.Fields.Add("Note");
                definition.Fields.Add("Note2");
                definition.Fields.Add("Number");
                definition.Fields.Add("Currency");
                definition.Fields.Add("User");
                definition.Fields.Add("Link");
                definition.Fields.Add("Calculated");
            }

            excelExporter.ListDefinitions.Add(definition);

            excelExporter.ExportLists(this);
        }

        private void Go(bool doNotOutput, int maxItems)
        {
            ExcelExporter excelExporter = new ExcelExporter("https://abc.com/sites/events", "Events.xls", "Events",
                                                            this.Page.Request.PhysicalApplicationPath + "Temp\\");
            excelExporter.MaxItems = maxItems;
            excelExporter.DoNotOutput = doNotOutput;

            excelExporter.OutputFileDefinition.Columns.Add("Content Type", typeof(string));
            excelExporter.OutputFileDefinition.Columns.Add("Event", typeof(string));
            if (_titleOnly == false)
            {
                excelExporter.OutputFileDefinition.Columns.Add("Display Title", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Start Time", typeof(DateTime));
                excelExporter.OutputFileDefinition.Columns.Add("All Day Event", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Recurrence", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Leader / host", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Co-host", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("If leader / host not on list please add", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Main contact", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Created by", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Audience by business function", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Audience by type", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Invited number of attendees", typeof(int));
                excelExporter.OutputFileDefinition.Columns.Add("Actual attendees", typeof(int));
                excelExporter.OutputFileDefinition.Columns.Add("Meeting type", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Meeting specifics", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("City or Town", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Name of UK venue", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Name of international venue", typeof(string));
                excelExporter.OutputFileDefinition.Columns.Add("Name of venue (UK and international)", typeof(string));
			}

            string queryStringLists = this.Page.Request.QueryString["lists"];

            if (String.IsNullOrEmpty(queryStringLists))
            {
                string[] lists = {
                                     "Operate",
                                     "Services",
                                     "Functions",
                                     "Wholesale",
                                     "Shops",
                                     "Design",
                                     "Comms",
                                     "Special Events"
                                 };

                foreach (string list in lists)
                {
                    Logger.LogInfo("Loading definition: " + list);

                    ExcelExporterListDefinition definition = this.GetDefinition(list);
                    excelExporter.ListDefinitions.Add(definition);
                }
            }
            else
            {
                string[] lists = queryStringLists.Split(',');

                foreach (string list in lists)
                {
                    Logger.LogInfo("Loading definition: " + list);

                    ExcelExporterListDefinition definition = this.GetDefinition(list);
                    excelExporter.ListDefinitions.Add(definition);
                }
            }

            Logger.LogInfo("Exporting");

            excelExporter.ExportLists(this);
        }

        private ExcelExporterListDefinition GetDefinition(string listName)
        {
            ExcelExporterListDefinition definition = new ExcelExporterListDefinition(listName);

            definition.Fields.Add("Content Type");
            definition.Fields.Add("Title");
            if (_titleOnly == false)
            {
                definition.Fields.Add("Display Title");
                definition.Fields.Add("Start Time");
                definition.Fields.Add("All Day Event");
                definition.Fields.Add("Recurrence");
                definition.Fields.Add("Leader / host");
                definition.Fields.Add("Co-host");
                definition.Fields.Add("If leader / host not on list please add");
                definition.Fields.Add("Main contact");
                definition.Fields.Add("Created by");
                definition.Fields.Add("Audience by business function");
                definition.Fields.Add("Audience by type");
                definition.Fields.Add("Invited number of attendees");
                definition.Fields.Add("Actual attendees");
                definition.Fields.Add("Meeting type");
                definition.Fields.Add("Meeting specifics");
                definition.Fields.Add("City or Town");
                definition.Fields.Add("Name of UK venue");
                definition.Fields.Add("Name of international venue");
                definition.Fields.Add("Name of venue (UK and international)");
			}

            return definition;
        }
    }
}

Open in new window

0
Comment
Question by:aspnet-scotland
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 36893456
Go() and Test1() are very similar in what happens. The both create an excel exported and use the parameters to determine how many rows should be pulled, if it should be output, and what columns should be displayed in the output. The main difference is the columns output.

As for the Page_Load, see comments below.

protected void Page_Load(object sender, EventArgs e)
        {
            Logger.Type = LogType.Page;
            Logger.Level = LogLevel.Info;
            Logger.LoggingPage = this;

            try
            {
                bool doNotOutput = false;
//Check the "export" value in the querystring to determine if the excelExporter.DoNotOutput should be true or false
                if (this.Page.Request.QueryString["export"] == "no")
                {
                    doNotOutput = true;
                }

                int maxItems = 0;
//Check the "max" querystring value to set excelExporter.MaxItems
                if (this.Page.Request.QueryString["max"] != null)
                {
                    maxItems = Int32.Parse(this.Page.Request.QueryString["max"]);
                }

//Determines if additional fields should be exported or not: ("Modified"), ("MultiChoice"), ("MultiLookup"), "SingleChoice"), etc.
                if (this.Page.Request.QueryString["titleOnly"] == "yes")
                {
                    _titleOnly = true;
                }

//If testing on localhost, call Test1(...) with the appropriate values
                if (this.Page.Request.Url.ToString().Contains("localhost"))
                {
                    Test1(doNotOutput, maxItems);
                }
//If not on localhost, call Go(...) with the appropriate values
                else
                {
                    Go(doNotOutput, maxItems);
                }
            }
            catch (Exception ex)
            {
//Log the exception to custom error log
                Logger.LogException("Error", ex);
            }

        }

Open in new window

0
 

Author Comment

by:aspnet-scotland
ID: 36904344
Thanks strickdd,

So what would my typical query string entry be?...

https://abc.com/sites/events?export=yes,max=10,titleOnly=no 

Also, will I have to specify the additional fields in the query string?

Thanks.
0
 

Author Comment

by:aspnet-scotland
ID: 36910240
entry would be:


https://abc.com/sites/events?export=yes&max=10&titleOnly=no 
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question