Solved

C# code explaination.

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating textboxes in red square borders. 2 34
Problem to Office 1 45
Selenium and Xpath 4 32
insert an Jpeg/bmp image onto the visio drawing using VBA/C# 5 28
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 …
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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