Monday, May 21, 2012

Generating ics file from sharepoint calendar

This code will generate the outlook's meeting file format.ics file. This codes were written for the scenario where the meeting will be announced in SharePoint Calendar with EventID and the column named Meeting Attendees [People Picker with multiple values] to capture the acknowledgement of the end user. Every time user generate the ics file,their details will be logged into different List. This will also address the how to insert the SPUser value into SPUserFieldValueCollection and SPFieldUserValue programmatically in People Picker control.
 using (SPSite site = SPContext.Current.Site)
                {
                      StringBuilder sb = new StringBuilder();
        
                    using (SPWeb web = site.OpenWeb())
                    {

                        SPList list = web.Lists["Calendar"];
                        string _CamlQuery = "" + Convert.ToInt32(Request.QueryString["EventID"].ToString()) + "";
                        SPQuery query = new SPQuery();
                        query.Query = _CamlQuery;
                        DataTable dt = list.GetItems(query).GetDataTable();
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            lblEventId.Text = dt.Rows[i]["EventID"].ToString();
                            lblTitle.Text = dt.Rows[i]["Title"].ToString();
                            lblDescription.Text = dt.Rows[i]["Description"].ToString();
                            lblStart.Text = dt.Rows[i]["EventDate"].ToString();
                            lblEnd.Text = dt.Rows[i]["EndDate"].ToString();
                            lblLocation.Text = dt.Rows[i]["Location"].ToString();
                        }

                        DateTime start = Convert.ToDateTime(lblStart.Text);
                        DateTime end = Convert.ToDateTime(lblEnd.Text);
                        sb.Append("BEGIN:VCALENDAR"); sb.Append(",");
                        sb.Append("PRODID:-");
                        sb.Append("VERSION:1.0"); sb.Append(",");
                        sb.Append("BEGIN:VEVENT"); sb.Append(",");
                        sb.Append(start.ToString("yyyyMMdd\\THHmmss\\Z")); sb.Append(",");
                        sb.Append(end.ToString("yyyyMMdd\\THHmmss\\Z")); sb.Append(",");
                        sb.Append("LOCATION:" + lblLocation.Text); sb.Append(",");
                        sb.Append("CATEGORIES:Meeting"); sb.Append(",");
                        sb.Append("DESCRIPTION:" + lblDescription.Text); sb.Append(",");
                        sb.Append("meeting=0D=0A"); sb.Append(",");
                        sb.Append("SUMMARY:" + lblTitle.Text); sb.Append(",");
                        sb.Append("PRIORITY:3"); sb.Append(",");
                        sb.Append("END:VEVENT"); sb.Append(",");
                        sb.Append("END:VCALENDAR");
                        string[] contents = sb.ToString().Split(',');
                        File.WriteAllLines("C:\\windows\\Temp\\" + lblTitle.Text.ToString() + ".ics", contents);
                    }

                }