Importing Outlook Calendar events into a JICS Calendar Portlet

By October 13, 2009 asp.net

One of the issues our JICS user’s have run across when trying to import Microsoft Outlook Calendar events into a Calendar portlet is that some calendar items end up not showing up after they go through the process of exporting to a .csv file and importing the file using the JICS Calendar import utility.

We tested various things and discovered the main issue was event listings with descriptions were the only ones not showing up.  The main culprit appeared to be carriage returns which caused the JICS import feature to simply ignore the entry all together and not import it into the system.

We read through the wiki but ultimately decided on building a small web based utility for pre-processing .csv files before importing into a JICS.  The utility basically loops through each field of the .csv and replaces any Newline character with a HTML break.

The line of code doing the work is painfully simple.  It’s just a replace on the string:
MyField.ToString().Replace(Environment.NewLine.ToString(), “<br />”);

After working with reading in the .csv file on my own (and not having any luck), I came across these helpful methods by Andreas Knab:
http://knab.ws/blog/index.php?/archives/10-CSV-file-parser-and-writer-in-C-Part-1.html

http://knab.ws/blog/index.php?/archives/10-CSV-file-parser-and-writer-in-C-Part-2.html

These methods do all the heavy work of taking a .csv file (either from a physical location or a stream) and processing it into a DataTable.  Once I added these methods into my project, I could then load that .csv into a datatable, loop through the rows and columns and replace all the carriage returns.  There are a host of other .csv processors out there but this one did it for me (so I moved onto my next project).

 

Onto the Code

This code is from the click event of a submit button on a page with only an upload file, label (for writing out errors) and the submit button:

private void btnProcess_Click(object sender, System.EventArgs e)
		{
			lblError.Text = "";

			if ((fileUp.PostedFile != null) && (fileUp.PostedFile.ContentLength > 0))
			{
				//Only .csv files
				string fileExt = System.IO.Path.GetExtension(fileUp.PostedFile.FileName);

				if (fileExt.ToLower() == ".csv")
				{				
					StreamReader reader = new StreamReader(fileUp.PostedFile.InputStream);

					DataTable dt = CsvParser.Parse(reader.ReadToEnd(), true);
					foreach (DataRow dr in dt.Rows)
					{
						foreach (DataColumn col in dt.Columns)
						{
							dr[col] = dr[col].ToString().Replace(Environment.NewLine.ToString(), "<br />");
						}						
					}
					this.ExportFile(CsvWriter.WriteToString(dt, true, true));
				}
				else
				{
					lblError.Text = "Please upload a .csv file only.";
				}
			}
			else
			{
				lblError.Text = "Please select a file to process.";
			}
		}

This function then writes a time stamped .csv file back out to the browser for the user to download:

private void ExportFile(string csvFile)
{			
	HttpContext.Current.Response.Clear();
	HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", DateTime.Now.Ticks.ToString() + ".csv"));
	HttpContext.Current.Response.ContentType = "text/csv";		
	HttpContext.Current.Response.Write(csvFile);
	HttpContext.Current.Response.End();					
}

The user can then import the file as normal into a JICS calendar.  Hopefully future releases of JICS will eliminate the need for the utility.

If anyone out there has a better way, please share.

NOTE: The above code is written (and works) for JICS 6.4 SP2 HF7D (.net 1.1) but it should work fine in JICS 7 (.net 3.5).