Solved

C# code explaination.

Posted on 2011-09-30
3
211 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
  • 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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now