Using iCal.net with Google Calendar - google-calendar-api

I've replaced the DDay iCal library with iCal.Net in an application for scheduling officials. Many of the officials like to add their game calendar using "add from URL" on their Google calendar so they can see their schedule on their phones. It seems that Google calendar does not like the .ics file that is being produced now, however. It will import fine, but trying to add it by URL results in no events showing up. I've had no issues adding a link by URL with other clients (Outlook, O365, etc.), and the file appears normal in a text editor.

This issue appeared to be caused by the OpenTextFileWriter method including a Byte Order Mark (BOM) in the default ASCII encoding. Here is what the original code for producing this file looked like:
Dim MySerializer As New CalendarSerializer()
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter(strPath, False)
file.Write(MySerializer.SerializeToString(MyCal))
file.Close()
I had to change my code that produced the ICS file to the following before Google Calendar and Apple's "Subscribe to this calendar" would work:
Dim MySerializer As New CalendarSerializer()
Dim file As System.IO.StreamWriter
Dim utf8WithoutBom As New System.Text.UTF8Encoding(False)
file = My.Computer.FileSystem.OpenTextFileWriter(strPath, False, utf8WithoutBom)
file.Write(MySerializer.SerializeToString(MyCal))
file.Close()
Interesting, Outlook and Office 365 was not picky about the BOM and it worked fine there.

Related

Trouble Understanding Code to Export Excel Spreadsheet from HTML

I have been asked to make changes to an ASP.NET WebForms application written in VB (I normally use C#).
One task is to try and fix an Excel download. The client reported that he gets an error about the spreadsheet being corrupt when he attempts to open it in Excel.
The code that exports the Excel download appears in the Load event of a dedicated ASPX page. And looks something like this:
Dim mytable As New HtmlTable
mytable = [Populate HTML Table Here]
mytable = returnclass.displaytable
mytable.Border = 1
mytable.BorderColor = "#CCCCCC"
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Buffer = True
Response.Write("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">")
Response.Write("<head>")
Response.Write("<!--[if gte mso 9]><xml>")
Response.Write("<x:ExcelWorkbook>")
Response.Write("<x:ExcelWorksheets>")
Response.Write("<x:ExcelWorksheet>")
Response.Write("<x:Name>" & worksheetTitle & "</x:Name>")
Response.Write("<x:WorksheetOptions>")
Response.Write("<x:Print>")
Response.Write("<x:ValidPrinterInfo/>")
Response.Write("</x:Print>")
Response.Write("</x:WorksheetOptions>")
Response.Write("</x:ExcelWorksheet>")
Response.Write("</x:ExcelWorksheets>")
Response.Write("</x:ExcelWorkbook>")
Response.Write("</xml>")
Response.Write("<![endif]--> ")
Response.Write("</head>")
Response.Write("<body>")
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=hhexport.xls ")
HttpContext.Current.Response.Charset = ""
'ouput table to html so excel can interperet.
Me.EnableViewState = False
Dim stringWriter As New System.IO.StringWriter()
Dim htmlWriter As New System.Web.UI.HtmlTextWriter(stringWriter)
mytable.RenderControl(htmlWriter)
HttpContext.Current.Response.Write(stringWriter.ToString)
I really don't understand what this is trying to do.
Questions:
The code produces a regular ASP.NET HtmlTable and assigns it to mytable. On what planet can Excel open HTML?
I'm really kind of loss by the XML in general here, and by the <!--[if gte mso 9] comment. Can anyone help me understand what is going on here.
The result appears valid but I'm just not familiar with what the intent is here. Any tips appreciated.
EDIT
On further testing, the problem seems related to the extension given to the file (xls). The current version of Excel will go ahead and load the file if I indicate that. But all formatting is lost. Any suggestions on what type of file this would be?
EDIT
And it looks like the original author got the idea from here, although that page doesn't really describe what is happening.
UPDATE
Thanks for everyone's response. I will credit those replies according to how they addressed the questions above. However, for my purposes the code appears to have worked all along. It just appears that newer versions of Excel now warn the user that an XLS file that contains HTML is a file of a different type than suggested by the file extension. And it appears there is nothing that can be done about this except for exporting using CSV, OpenXML or some other approach. I found more details in this blog.
The code basically wraps an HTML table in some special XML tags that relate to Excel (defining a Workbook and Worksheets, etc). This is supposed to allow the output to be opened by either Excel or a browser.
To answer your questions:
The code produces a regular ASP.NET HtmlTable and assigns it to mytable. On what planet can Excel open HTML? Actually, that's a feature of Excel. You can use a special combination of XML and HTML tags to create files that are open-able on the web and in Excel. See this MSDN article: How to format an Excel workbook while streaming MIME content
I'm really kind of loss by the XML in general here, and by the <!--[if gte mso 9] comment. Can anyone help me understand what is going on here. That specific comment is checking for the availability of MS Excel (whether it's being opened by Excel or a browser), I believe. The XML is specific tags that have special meaning in MS Excel. There's a reference you can download here: Microsoft® Office HTML and XML Reference
I found this article on C# Corner to be pretty helpful in understanding this type of code: Creating a Dynamic Excel Using HTML.
As far as I know Excel has been able to read HTML for quite a while. This particular approach is pretty common, but it's definitely not best practice.
The important part of this logic is here:
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=hhexport.xls ")
HttpContext.Current.Response.Charset = ""
'ouput table to html so excel can interperet.
Me.EnableViewState = False
Dim stringWriter As New System.IO.StringWriter()
Dim htmlWriter As New System.Web.UI.HtmlTextWriter(stringWriter)
mytable.RenderControl(htmlWriter)
HttpContext.Current.Response.Write(stringWriter.ToString)
The Response.Write logic is just being used to control the workbook and worksheet that gets outputted. If that logic was not there, the file would open with three worksheets similar to a new Excel workbook.

Why won't Google Calendar load my dynamically generated ICS file?

I've been given the task of creating an ICAL feed of conference calls for members of our organization. I created a handler in ASP.NET that loops through our database, gets the call data from the database, and creates output that appears valid to me based on what I've read of the ICAL format, and the examples I've seen/disassembled.
Outlook 2007 reads the resulting output and displays the calendar, no problem (screenshot here shows how it renders).
30 Boxes also has no problem with it. (see test here).
But when I try to load the same output into Google Calendar, I get the message "We could not parse the calendar at the url requested":
What's wrong with my output that's causing Google to reject it? You can see the temporary data I'm testing with at this URL: http://www.joshuacarmody.com/temp/icaltest.ics. This is a snapshot of the output from my .ASHX file, unaltered except the phone numbers and passcodes have been sanitized.
Edit with additional Info:
I just tried the following
Created a copy of my test file called "icaltest-1googevent.ics"
Deleted all the VEVENT data from the file
Exported one of my Google calendars to ICS
Copied one VEVENT from Google's exported data into my test file
Attempted to subcribe to icaltest-1googevent.ics in Google Calendar.
I still got an error message. So I'm guessing the issue isn't with my VEVENT data, but with something else about the file. Maybe there's something wrong with my VCALENDAR definition?
the severinghaus ics validator seems to think there is something funny ( a ? ) before the BEGIN CALENDAR
http://severinghaus.org/projects/icv/?url=http%3A%2F%2Fwww.joshuacarmody.com%2Ftemp%2Ficaltest.ics
In my testing google was a lot fussier/rigourous/pedantic - once you get it working with the validator and google it should work in most places.
After lots of trial-and-error, and comparing my output with Google's, I got it working. There were a few problems with my ICS file:
Unescaped characters (I didn't know I had to escape commas!)
Inconsistent line return characters. They didn't show up in my text editor, but I had to use .NET's String.remove() to remove "\r" from my output to get Google to recognize it
The file was missing VCALENDAR:END. Apparently Outlook doesn't much care. Google does.
I had not one, but three funny characters before the BEGIN:VCALENDAR, decimal codes 239, 187, 191.
I found them thanks to the severinghaus.org link above, thanks!
It turns out they're a prefix called BOM in UTF-8, you can read up on it here: http://en.wikipedia.org/wiki/UTF-8#Byte_order_mark
Google doesn't handle this, but after stripping these three characters from the file and uploading to the server, I was able to susbribe to that calendar in Google Calendar (from URL).
I hope this helps someone passing by this page in the future...
I had similar problem until I realised that opening the generated .ics file in Notepad++ wasn't in UTF-8. I was using a method to convert my string to a byte array, but wasn't using an encoder for this, so no matter what content headers I used, the file would never be generated using UTF-8. This simple fix resolved the UTF-8 generation and Google is now happy with my feed:
var utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(myString);
myString= utf8.GetString(utfBytes, 0, utfBytes.Length);

How to find RSS feed of a particular website?

How to find RSS feed of a particular website? Whether there is any particular way to find it?
You might be able to find it by looking at the source of the home page (or blog). Look for a line that looks like this:
<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="http://example.org/rss" />
The href value will be where the RSS is located.
There are multiple ways to get the RSS feed of the website.
What you can do is get the page source of a website and search for this link tag of type="application/rss+xml"
That will contain the RSS feed of that website, if any.
Here is a simple program in python that will print the RSS feed of any website, if any.
import requests
from bs4 import BeautifulSoup
def get_rss_feed(website_url):
if website_url is None:
print("URL should not be null")
else:
source_code = requests.get(website_url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.find_all("link", {"type" : "application/rss+xml"}):
href = link.get('href')
print("RSS feed for " + website_url + "is -->" + str(href))
get_rss_feed("http://www.extremetech.com/")
Save this file with the .py extension and run it. It will give you the rss feed url of that website.
Google also provides APIs to find the RSS feeds of a website.
Please find them here: Google Feed API
You need to loop through all urls on your website and then find one that's containing "rss".
Method above maybe won't work in some cases if url in href tag looks something like feed.xml, so in that case you'll need to loop through all tags containing href AND rss, then just parse url from href attribute.
If you want to do this through browser, press CTRL+U to view source, then CTRL+F to open find window and then just type in rss. RSS Feed url should appear immediately.
Firefox's Tools menu now has a "Page Info" command. One of the tabs in that tool displays discovered feed info.
I needed to find sites with RSS feeds. Using Visual Studio (VB) I was able do that. Following code is just a fragment. It dies after the loop finishes but it does find any ref to an rss page on the site. That's all I needed so I never quite finished it. But it worked for me.
Imports System.Net
Imports System.IO
...
Dim request As WebRequest
request = WebRequest.Create("http://www.[site]")
Dim response As WebResponse = request.GetResponse()
Dim responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
Dim line As String = reader.ReadLine()
Dim intPos As Integer
Do
line = reader.ReadLine()
intPos = line.IndexOf("/rss")
If intPos > 0 Then
MessageBox.Show(line + " " + intPos.ToString)
End If
Loop While Not line Is Nothing
....

Downloaded word file displaying incorrectly

I am working on a website at the moment which is displaying a strange bug with generated word documents. The site has a feature on it which allows the user to download a word document containing information related to their visit. This file is generated via some vb.net code and takes an xml template of the final document and inserts the relevant content required.
The strange behaviour is that on some machines the .doc file generated displays fine and on others it displays as XML when opened in Word. Both behaviours have been seen in the same version of Office (2003) but on seperate machines. My question is really whether the error lies with the set up of word on the individual machines, or whether there is an error in the code.
The code to create the file and download it is as follows:
Response.Clear()
Response.ClearHeaders()
Response.AddHeader("content-disposition", "inline; filename=MyNewFile")
Response.ContentType = "application/msword"
'Create the word file as a byte array based off an xml template document'
Dim objWordGenerator As New WordFileGenerator
Response.BinaryWrite(objWordGenerator.GetWordBytes)
Response.Flush()
Response.Clear()
Response.End()
The actual xml template is quite large so probably not suitable to post here but I can provide any more information if necessary.
Update:
Having managed to fix the original bug (it turns out that the original filename being used didn't have the .doc extension) I have found another bit of strange behaviour.
When the file is opened it opens in Word correctly, however when you go to save it the default file type is XML. When saved as an XML file it will open in Word correctly, but I feel this is slightly confusing behaviour for the end user. I would like the file to default to saving as a DOC file instead. Is there a way to force this to happen?
Update 2:
Below is a section of the XML that relates to the Document properties. The rest of the document deals with content and styles etc, so my assumption is that this is the most relevant section. To reiterate, my problem is that when the downloaded .doc file is opened in word, the default "save as" option is as an XML file.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve">
<o:DocumentProperties>
<o:Title>Fancy Word Doc</o:Title>
<o:Author>Bob Bobertson</o:Author>
<o:Characters>999</o:Characters>
<o:Company>A Fancy Company</o:Company>
<o:Version>1.1.1</o:Version>
</o:DocumentProperties>
Cheers
The File -> SaveAs filetype is XML because that is what the file open in Word is. If you want it to say 'Word Document (*.doc) then you will need to create a real Word document on the server and not an XML. Just by putting a .doc extension on the filename doesn't change it's real contents. Word knows the file type that is loaded into it and suggests that as the file type when saving. I don't know of any way to override this behavior.
I've been using Office XML with Excel for awhile now and this is very similar to the code that I'm using to send it down to the client. You might want to try and see if it works for you.
Dim xml As XmlDocument = New XmlDocument()
xml.Load("report.doc")
Response.ContentType = "application/vnd.ms-word"
Response.AppendHeader("CONTENT-DISPOSITION", "attachment; filename=report.doc")
Response.Write(xml.OuterXml)
Try it with firefox and you will probably find that it will be saved with the correct extension.
IIRC, since version 3 IE prefers to ignore the mime type and sniff the file content to see what the "correct" file format is. Maybe is uses the magic cookie?
Is this Word 2007 or later? Try
Response.AddHeader("content-disposition", "attachment; filename='MyNewFile.doc'")
attachment encourages the browser to save the file instead of displaying it.
I ran some tests and could not reproduce your problem on my system in Word 2003. Without a specific example (and actual file that is misbehaving), it would be pure speculation to make any suggestions.

Embed word document into ASP.net page and access using VBscript

I have some code that opens a word document using VBScript on an ASP.net page:
set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open "c:\inetpub\wwwroot\JSWordTest\test.doc", False, False, False
This works great but opens the word doc in another window. Ideally I would like to make this look as if it is contained in the current page perhaps in an IFrame. I have some other buttons which paste text into the word document when clicked.
I cannot just set the src of the iframe to the word document as need a reference to the word document (objWord) to allow me to paste text into it in real time again using Vbscript to do this.
Not sure if this is possible but any ideas/alternatives welcome?
Requirements:
The word doc needs to be displayed from web browser
At the side of the word document will be some buttons which when clicked paste text into it
You can use this technique to get the contents of the Word document without displaying any windows at all.
' Declare an object for the word application '
Set objWord = CreateObject("Word.Application")
objWord.Visible = False ' Don''t show word '
objWord.Documents.open("C:\test.doc") ' Open document '
objWord.Selection.WholeStory ' Select everything in the doc '
strText = objWord.Selection.Text ' Assign document contents to var'
objWord.Quit False ' Close Word, don't save '
Once you've got the contents of the document in the variable you can do what you want with it as far as writing it out with a document.write or whatever method you want to use.
You can find more detail on the MS Word application object and its methods here: http://msdn.microsoft.com/en-us/library/aa221371(office.11).aspx
If it is an option to install an ActiveX component at the client machines, you can try EDraw Office Viewer component or the cheapter Ultra Office Control. Both are based on the DSOFramer example by Microsoft and provider similar methods to interface with the documents.
Sample code is given and shows how to trigger dialogs, insert text, etc.
You can get inspirations from Excel Viewer component. It is like EDraw Office Viewer but free and open source. Currently, it opens office documents only but you can easily change it to work with Word.
You could try saving to HTML format
Const wdFormatHTML = 8
dim doc
set doc = objWord.Documents.open("C:\test.doc")
doc.SaveAs "doc.htm", wdFormatHTML
' etc ...
and then use that as the source of your iframe document. Bear in mind that when saving to HTML format, Word creates a corresponding resources folder (for images etc), so you might need to take that into account.

Resources