Download ICS file

Home Forums Classic DFFS Download ICS file

Tagged: ,

Viewing 0 reply threads
  • Author
    Posts
    • #34111
      Alexander Bautz
      Keymaster

        Here is an example of how you can generate an ICS file based on data in your form. It will download to your computer and opens in your default calendar app to let you set up an appointment.

        Add the below function to your Custom JS. Please note that you must change the FieldInternalNames to match your fields (AttendeesPeoplePicker, EventLocation, Description, StartDate and EndDate).

        function downloadICSFile() {
            // Get attendees
            var attendees = spjs.utility.getFieldValue({
                "fin": "AttendeesPeoplePicker",
                "key": "loginName"
            });
            var attendeeArr = [];
            jQspjs.each(attendees, function(i, u) {
                var user = spjs.utility.userInfo(u);
                attendeeArr.push("ATTENDEE;CN=\"" + user.Title + "\";RSVP=FALSE:mailto:" + user.EMail);
            });
            // Event location
            var eventLocation = getFieldValue("EventLocation");
            // Summary
            var summary = getFieldValue("Title");
            // Description
            var description = getFieldValue("Description");
            // Split string in lines with max length 75 characters to comply with the ICS format
            description = description.match(/.{1,75}/g).join("\\n");
            // Append link to item
            description += "\\n\\n" + location.href;
            // Build ics file contents
            var b = [];
            b.push("BEGIN:VCALENDAR");
            b.push("PRODID:-//DFFS//SPJSBlog.com//EN");
            b.push("VERSION:2.0");
            b.push("BEGIN:VEVENT");
            b.push("ORGANIZER;CN=\""+_spPageContextInfo.userDisplayName+"\":mailto:"+_spPageContextInfo.userEmail);
            b.push(attendeeArr.join("\n"));
            b.push("DTSTAMP:" + new Date().toISOString());
            b.push("STATUS:CONFIRMED");
            b.push("UID:DFFS_" + getFieldValue("_DFFSID"));
            b.push("SEQUENCE:0");
            b.push("DTSTART:" + spjs.utility.getDateFieldAsDateObject("StartDate").toISOString());
            b.push("DTEND:" + spjs.utility.getDateFieldAsDateObject("EndDate").toISOString());
            b.push("SUMMARY:" + summary);
            b.push("DESCRIPTION:" + description);
            b.push("LOCATION:" + eventLocation);
            b.push("END:VEVENT");
            b.push("END:VCALENDAR");
            // Download file
            if (navigator.msSaveBlob) {
                // Internet Explorer
                var blob = new Blob([b.join("\n")],{"type":"text/text;charset=utf-8;"});
                return navigator.msSaveBlob(blob, "calendar.ics");
            }else{
                // Modern browsers
                var element = document.createElement('a');
                element.setAttribute('href', 'data:text/text;charset=utf-8,' + encodeURI(b.join("\n")));
                element.setAttribute('download', "calendar.ics");
                element.click();
            }
        }

        Add a button in a HTML section like this:

        <input style="margin:5px 0" type="button" onclick="downloadICSFile()" value="Download ICS">

        Alexander

    Viewing 0 reply threads
    • You must be logged in to reply to this topic.