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

Forums / General Discussions / Import Events from different database

Import Events from different database

9 posts, 1 answered
  1. Amanda Shafer
    Amanda Shafer avatar
    198 posts
    Registered:
    12 Oct 2012
    17 Mar 2011
    Link to this post
    Hello,
    Here is the situation I am facing. We maintain all our information about our events in another database. I want to import  information on these events into Sitefinity 4.0 so they don't have to be manually entered.  What is the best way to go about this? And if there's any examples could you point me in that direction?

    Thanks!
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    17 Mar 2011
    Link to this post
    I had to do something similar to this in the past. The way I did it was to create a web forms page like Import.aspx and in the code behind, I would retrieve all the events from my database, then use the Sitefinity Events API to create the events programmatically.

    Then I would just run that page and the script would import everything.

    Here is the documentation for using the API to create events: http://www.sitefinity.com/40/help/developers-guide/sitefinity-essentials-modules-events-creating-events.html

    simply loop through your incoming events and match up the properties with the Sitefinity events and save.

    Hope this was helpful!
  3. Amanda Shafer
    Amanda Shafer avatar
    198 posts
    Registered:
    12 Oct 2012
    17 Mar 2011
    Link to this post
    I thought that might be the answer. It seems that putting content items right into the database from another is generally bad.

    Unfortunately I'm not much of a programmer and this might be beyond my skill set. You wouldn't happen to have some example code that I could look at?

    I got the page with the code behind. Copying over the example for creating an event using the fluid api doesn't seem to work for me. I think I have the references set.

    using System;
    using System.Linq;
    using Telerik.Sitefinity;
    using Telerik.Sitefinity.Events.Model;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Modules.Events;
     
    namespace SitefinityWebApp
    {
        public partial class testpage2304 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
     
            }
     
            public Guid CreateEventFluent(string eventTitle, string summary, string city, string country, string content)
            {
                Guid itemID = Guid.Empty;
                App.WorkWith().ContentItem().CreateNew()
                    .Do(eI =>
                    {
                        itemID = eI.Id;
                        eI.Title = eventTitle;
                        eI.Summary = summary;
                        eI.City = city;
                        eI.Country = country;
                        eI.EventStart = DateTime.Today;
                        eI.EventEnd = DateTime.Today.AddDays(3);
                        eI.PublicationDate = DateTime.Today;
                        eI.ExpirationDate = DateTime.Today.AddDays(365);
                        eI.Content = content;
                    })
                    //without the publish method, the content is saved as draft.  
                     .Publish()
                     .SaveChanges();
                return itemID;
            }
     
    }
        }
  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    18 Mar 2011
    Link to this post
    I haven't tried this with the fluent API, using the Events Manager might be a more straightforward way to do it.

    first you have to have a way to retrieve your existing items tho, something like the example below. the Importer.GetExistingItems is just a dummy placeholder that should be replaced with custom logic to retrieve your existing items.

    Then loop through the list of items and copy the data from your item. Your existing event data might not have all the properties (city, state, etc) so you can put in defaults with literal strings

    hope this is helpful!
     
    public void ImportEvents()
    {
        // you'll have to build this part yourself or use an API from the existing site if there is one
        var importedItems = Importer.GetExistingItems();
      
        //Initialize the EventsManager with the default provider
        EventsManager manager = new EventsManager();

     // loop through all your content items 
     
        foreach (var item in importedItems)
        {
        //Create an Sitefinity Event
        Event eventItem = manager.CreateEvent();
      
        //Set the newly created event properties from the existing event 
        eventItem.Title = item.eventTitle;
        eventItem.Summary = item.summary;
        eventItem.City = "Houston";
        eventItem.Country = item.country;
        eventItem.EventStart = item.StartDate
        eventItem.EventEnd = item.EndDate;
        eventItem.PublicationDate = DateTime.Today;
        eventItem.ExpirationDate = DateTime.Today.AddDays(365);
        eventItem.Content = item.content;
      
        //Generate an URL for the content item and Save the changes.
        manager.RecompileItemUrls<Event>(eventItem);
      
        //up to now, the item is in Draft State. We hae to call Publish method to publish it.
        manager.Publish(eventItem);
        manager.SaveChanges();
        }
    }
    Answered
  5. Amanda Shafer
    Amanda Shafer avatar
    198 posts
    Registered:
    12 Oct 2012
    18 Mar 2011
    Link to this post
    Yes, this is what I needed. Thank you very much for the example!
  6. Richard
    Richard avatar
    77 posts
    Registered:
    24 Oct 2007
    13 May 2011
    Link to this post
    Just wanted to say, input like this is gold.  Thank you so much!
  7. Richard
    Richard avatar
    77 posts
    Registered:
    24 Oct 2007
    13 May 2011
    Link to this post
    Can someone also tell me why one would choose Fluent API over the traditional method?
    I just don't see any advantages.
  8. Amanda Shafer
    Amanda Shafer avatar
    198 posts
    Registered:
    12 Oct 2012
    19 May 2011
    Link to this post
    I've just got around to doing this today and so far it's working like a charm.  I do have one more questions someone might be able to help me out with.

    I created a custom field called "meetingid". Is there a way to programmatically import that? So it would be eventItem.MeetingID  ?
  9. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    02 Dec 2016
    26 May 2011
    Link to this post
    Hi Amanda Shafer,

    You can use the GetValue() method (please note this is an extension method, and you'll need to include a reference to Telerik.Sitefinity.Model in your code) to access the value stored in your custom fields (SetValue() accordingly allows you to change the value in it). Please check the sample code below:
    var eventItem = App.WorkWith().Events().Where(ev => ev.Status == ContentLifecycleStatus.Live && ev.Title == "title").Get().First();
    var myCustomFieldValue = eventItem.GetValue("MeetingID");
    If you have any further questions please let me know, I'll be glad to help you.

    Best wishes,
    Boyan Barnev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
9 posts, 1 answered