+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Export Events to CSV

Export Events to CSV

10 posts, 1 answered
  1. Kevin
    Kevin avatar
    38 posts
    Registered:
    06 Sep 2011
    27 Sep 2011
    Link to this post
    I am attempting to develop an export routine that creates a comma delimited file of all the events within Sitefinity from the backend site.  The only suggestions I've found within the forum is this -- http://www.sitefinity.com/devnet/forums/sitefinity-3-x/developing-with-sitefinity/export-to-excel-for-news-letter-subscribers.aspx -- but I'm not having any luck adapting this for my purposes.

    How can I accomplish the exportation of events?
  2. Kevin
    Kevin avatar
    38 posts
    Registered:
    06 Sep 2011
    29 Sep 2011
    Link to this post
    I modified the code for exporting subscribers from the aforementioned link so as to export the events instead...
    01.public void WriteToCSV()
    02.{
    03.    EventsManager manager = EventsManager.GetManager();
    04.    IList<Event> sub = new List<Event>();
    05.    IList allevents = manager.GetEvents();
    06.    foreach (Event s in allevents)
    07.    {
    08.        sub.Add(s);
    09.    }
    10. 
    11.    string attachment = "attachment; filename=Events.csv";
    12.    HttpContext.Current.Response.Clear();
    13.    HttpContext.Current.Response.ClearHeaders();
    14.    HttpContext.Current.Response.ClearContent();
    15.    HttpContext.Current.Response.AddHeader("content-disposition", attachment);
    16.    HttpContext.Current.Response.ContentType = "text/csv";
    17.    HttpContext.Current.Response.AddHeader("Pragma", "public");
    18. 
    19.    WriteColumns();
    20. 
    21.    foreach (Event eventItems in sub)
    22.    {
    23.        WriteUserInfo(eventItems);
    24.    }
    25.    HttpContext.Current.Response.End();
    26.}
    27. 
    28.private static void WriteColumns()
    29.{
    30.    string columnNames = "Title, EventStart, EventEnd";
    31.    HttpContext.Current.Response.Write(columnNames);
    32.    HttpContext.Current.Response.Write(Environment.NewLine);
    33.}
    34. 
    35.private static void WriteUserInfo(Event lettersubscriber)
    36.{
    37.    StringBuilder stringBuilder = new StringBuilder();
    38.    AddComma(lettersubscriber.Title, stringBuilder);
    39.    AddComma(lettersubscriber.EventStart.ToString(), stringBuilder);
    40.    AddComma(lettersubscriber.EventEnd.ToString(), stringBuilder);
    41.    HttpContext.Current.Response.Write(stringBuilder.ToString());
    42.    HttpContext.Current.Response.Write(Environment.NewLine);
    43.}
    44. 
    45.private static void AddComma(string value, StringBuilder stringBuilder)
    46.{
    47.    stringBuilder.Append(value.Replace(',', ' '));
    48.    stringBuilder.Append(", ");
    49.}

    Everything looks good except for line #5 I'm receiving the error, "Using the generic type 'System.Collections.Generic.IList<T>' requires 1 type argument".  Outside of changing some of the variable names, I haven't changed the code at all... What am I doing wrong here?
  3. Kevin
    Kevin avatar
    38 posts
    Registered:
    06 Sep 2011
    29 Sep 2011
    Link to this post
    I was able to resolve my own issue... The issue was that I didn't cast line #5 "ToList"... I made the following change, which fixed everything...
    IList allevents = manager.GetEvents().ToList();
  4. Kevin
    Kevin avatar
    38 posts
    Registered:
    06 Sep 2011
    11 Oct 2011
    Link to this post
    I've simplified my code, and resolved most all major issues, but I have one remaining issue... When I create the export file, all the event entries are doubled, so if I have 6 events in Sitefinity, my code outputs 12 events.

    Here is my control...
    <%@ Control Language="c#" Debug="true" AutoEventWireup="true" CodeFile="ExportEvents.ascx.cs" Inherits="ExportEvents" %>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="WriteToCSV" />

    And here is my control code behind...
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Text;
    using Telerik.Sitefinity;
    using Telerik.Sitefinity.Modules.Events;
    using Telerik.Sitefinity.Events.Model;
    using System.Collections;
    using Telerik.Sitefinity.Lists.Model;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.OpenAccess;
    using Telerik.Sitefinity.Taxonomies;
     
    public partial class ExportEvents : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public void WriteToCSV(object sender, EventArgs e)
        {
             
            EventsManager manager = EventsManager.GetManager();
            IList allevents = manager.GetEvents().ToList();
     
            string attachment = "attachment; filename=Events.csv";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "text/csv";
            HttpContext.Current.Response.AddHeader("Pragma", "public");
     
            WriteColumns();
     
            foreach (Event s in allevents)
            {
                WriteUserInfo(s);
            }
     
            HttpContext.Current.Response.End();
        }
     
        private static void WriteColumns()
        {
            string columnNames = "\"Subject\",\"Start Date\",\"Start Time\",\"End Date\",\"End Time\",\"All day event\",\"Categories\",\"Description\",\"Location\",\"Last Modified\"";
            HttpContext.Current.Response.Write(columnNames);
            HttpContext.Current.Response.Write(Environment.NewLine);
        }
     
        private static void WriteUserInfo(Event eItems)
        {
            StringBuilder stringBuilder = new StringBuilder();
            AddComma("\"" + eItems.Title + "\"", stringBuilder); //Subject
            AddComma("\"" + eItems.EventStart.ToString("M/dd/yyyy") + "\"", stringBuilder); //Start Date
            AddComma("\"" + eItems.EventStart.ToString("h:mm tt") + "\"", stringBuilder); //Start Time
            AddComma("\"" + eItems.EventEnd.Value.ToString("M/dd/yyyy") + "\"", stringBuilder); //End Date
            AddComma("\"" + eItems.EventEnd.Value.ToString("h:mm tt") + "\"", stringBuilder); //End Time
            AddComma("\"" + "true" + "\"", stringBuilder); //All day event
            AddComma("\"" + eItems.Content.ToString() + "\"", stringBuilder); //Categories
            AddComma("\"" + eItems.Description.ToString() + "\"", stringBuilder); //Description
            string strLocation = (eItems.City.ToString() + ", " + eItems.State.ToString());
            AddComma("\"" + strLocation + "\"", stringBuilder); //Location
            AddComma("\"" + eItems.LastModified.ToString("M/dd/yyyy") + "\"", stringBuilder); //Last Modified
            HttpContext.Current.Response.Write(stringBuilder.ToString());
            HttpContext.Current.Response.Write(Environment.NewLine);
        }
     
        private static void AddComma(string value, StringBuilder stringBuilder)
        {
            stringBuilder.Append(value.Replace(',', ','));
            stringBuilder.Append(",");
        }
    }


    And here is CSV file that's created...
    "Subject","Start Date","Start Time","End Date","End Time","All day event","Categories","Description","Location","Last Modified"
    "Spring Fling","4/23/2012","5:00 AM","4/27/2012","5:00 AM","true","<p>Spring Fling Week</p>","",", ","10/06/2011",
    "Bongo Ball Mania","4/23/2012","4:19 PM","4/23/2012","8:00 PM","true","<p>10am - 4pm<br />Gym</p>","",", ","10/07/2011",
    "TEST #1","12/12/2011","5:10 PM","12/12/2011","9:00 PM","true","<p>Test of Calendar #1</p>","",", ","10/10/2011",
    "Bongo Ball Mania","4/23/2012","4:19 PM","4/23/2012","8:00 PM","true","<p>10am - 4pm<br />Gym</p>","",", ","10/07/2011",
    "Blood Drive","3/29/2012","4:00 PM","3/29/2012","11:00 PM","true","<p>March 29th 2012<br />10:00 am - 3:00 pm<br />Gym</p>","",", ","10/11/2011",
    "TEST #1","12/12/2011","5:10 PM","12/12/2011","9:00 PM","true","<p>Test of Calendar #1</p>","",", ","10/10/2011",
    "Blood Drive","3/29/2012","4:00 PM","3/29/2012","11:00 PM","true","<p>March 29th 2012<br />10:00 am - 3:00 pm<br />Gym</p>","",", ","10/11/2011",
    "Spring Fling","4/23/2012","5:00 AM","4/27/2012","5:00 AM","true","<p>Spring Fling Week</p>","",", ","10/06/2011",


    Can anyone help me with this?
  5. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    11 Oct 2011
    Link to this post
    Have you tried using FluentAPI to get the events instead of the manager object?  I think it has a method or something .ThatArePublished?  Like App.WorkWith().Events().ThatArePublished()?  ...or something like that...I don't have a copy in front of me

    **Edit** I think perhaps that might be becasue its giving you both the published and draft items (guess)
  6. Kevin
    Kevin avatar
    38 posts
    Registered:
    06 Sep 2011
    11 Oct 2011
    Link to this post
    I can't seem to find the method .ThatArePublished()
  7. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    11 Oct 2011
    Link to this post
    Appears to be just "Publihed()" :) (notice the typo)

    App.WorkWith().Events()
    .Where(ci => ci.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
                                                       .Publihed()
                                                .Get()
  8. Kevin
    Kevin avatar
    38 posts
    Registered:
    06 Sep 2011
    11 Oct 2011
    Link to this post
    Well, it certainly won't be .Publihed(), but .Published() doesn't seem to work either.  In fact when I type the code just as you've written, except for the correct word "published" instead, I still receive the error "does not contain a definition for 'Published'"... but this seems to work...
    IList allevents = App.WorkWith().Events().Get().Where(ci => ci.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).ToList();

    I hope someone can confirm if this is the most efficient method to retrieve this data.
  9. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    11 Oct 2011
    Link to this post
    No...it most certainly IS "Publihed"

    This is the code I have for a custom control which populates a RadCalendar with published events

    private IList<Event> _events = null;
    public IList<Event> Events {
        get {
            if (_events == null) {
                _events = App.WorkWith().Events()
                                        .Where(ci => ci.GetValue<IList<Guid>>("Category").Contains(new Guid("8138087D-C9FF-4BA7-AEBC-97BABA1D935C")) &&
                                               ci.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live)
                                               .Publihed()
                                        .Get().ToList();
     
            }
            return _events;
        }
    }
    (this has an extra where clause to filter based on a category though)

    Visual Studio Intellisense should show you it's "Publihed"
    Answered
  10. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    11 Oct 2011
    Link to this post
    Just to prove it, here's the DLL loaded into just decompile :)
10 posts, 1 answered