Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

C# code explaination.

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
aspnet-scotland
Asked:
aspnet-scotland
  • 2
1 Solution
 
strickddCommented:
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
 
aspnet-scotlandAuthor Commented:
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
 
aspnet-scotlandAuthor Commented:
entry would be:


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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now