Import Events from different database

Posted by Community Admin on 03-Aug-2018 20:26

Import Events from different database

All Replies

Posted by Community Admin on 17-Mar-2011 00:00

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!

Posted by Community Admin on 17-Mar-2011 00:00

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!

Posted by Community Admin on 17-Mar-2011 00:00

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;
        
 
    

Posted by Community Admin on 18-Mar-2011 00:00

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();
    

Posted by Community Admin on 18-Mar-2011 00:00

Yes, this is what I needed. Thank you very much for the example!

Posted by Community Admin on 13-May-2011 00:00

Just wanted to say, input like this is gold.  Thank you so much!

Posted by Community Admin on 13-May-2011 00:00

Can someone also tell me why one would choose Fluent API over the traditional method?
I just don't see any advantages.

Posted by Community Admin on 19-May-2011 00:00

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  ?

Posted by Community Admin on 26-May-2011 00:00

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

This thread is closed