?
Solved

C# code explaination.

Posted on 2011-09-30
3
Medium Priority
?
221 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

762 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