Import Events from different database
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!
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;
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();
Yes, this is what I needed. Thank you very much for the example!
Just wanted to say, input like this is gold. Thank you so much!
Can someone also tell me why one would choose Fluent API over the traditional method?
I just don't see any advantages.
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 ?
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"
);