Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
Programming Blogs - Blog Catalog Blog Directory
 
 
 

Links

Social

Creating downloadable calendar item of SPListItem

Recently I prepared some code to download calendar item from SharePoint web part to user computer. I was lucky because the web part will only be used on details view of one list. The testing code I wrote was simple and short.


protected void downloadButton_Click(object sender, EventArgs e)

{

    SPListItem item = SPContext.Current.ListItem;

 

    try

    {

        string location = item["Location"] as string;

        string subject = item.Title;

        string description = item["Description"] as string;

 

        var beginDate = (DateTime)item["From"];

        var beginDateString = beginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z");

 

        var endDate = (DateTime)item["Thru"];

        var endDateString = endDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z");               

 

        string[] contents = {

                      "BEGIN:VCALENDAR",

                      "PRODID:-//My company//My product//EN",

                      "BEGIN:VEVENT",

                      "DTSTART:" + beginDateString,

                      "DTEND:" + endDateString,

                      "LOCATION:" + location,

                      "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + description,

                      "SUMMARY:" + subject, "PRIORITY:3",

                      "END:VEVENT",

                      "END:VCALENDAR" };

 

        HttpResponse response = HttpContext.Current.Response;

 

        response.Clear();

        response.ContentType = "text/calendar";

        response.AddHeader("Content-disposition", "attachment;filename=event.ics");

        response.Write(string.Join("\r\n", contents));

        response.Flush();

        response.End();

    }

    catch (System.Threading.ThreadAbortException taex)

    {

        // Ignore this exception

    }

}


Feel free to use this code if you like it.

Posted: Jan 24 2009, 04:15 PM by DigiMortal | with 6 comment(s)
Filed under:

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# January 24, 2009 11:21 AM

Creating downloadable calendar item of SPListItem | Web Hosting and Domains said:

Pingback from  Creating downloadable calendar item of SPListItem | Web Hosting and Domains

# January 24, 2009 11:40 AM

Waldek Mastykarz said:

Why not use the standard functionality instead: site/.../owssvr.dll{list ID}&CacheControl=1&ID=[item ID]8&Using=event.ics

# January 26, 2009 5:09 AM

Jonas said:

Gunnar,

You can also get this functionality by installing our REST API for MashPoint/SharePoint in this preview we have built in support for XHTML, ATOM+XML (ADO.NET Data-services former Astora), JSON, iCal and vCard.

community.bamboosolutions.com/.../sharepoint-support-for-icalendar-and-vcard-using-the-mashpoint-rest-api.aspx

You can easily write your own "serializers" for additional representations. As with MashPoint this is a free download. We are really interested in feedback and community involvement so please try it out.

Thanks

/Jonas

# January 26, 2009 2:41 PM

Christopher Steen said:

Link Listing - January 26, 2009

# January 27, 2009 1:46 AM

Christopher Steen said:

Sharepoint New Release of the SmartTools for SharePoint Project [Via: Jan Tielens ] Creating SharePoint...

# January 27, 2009 1:47 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)