Import External Data into Sitefinity Custom Modules

by
Tim Williamson
Nov 20, 2013

So, you’ve created a custom module, great! Now what? Well, now it’s time to get some data in there. Sitefinity’s Module Builder generates code samples that you can use to start interacting with custom content types right away. In this post, I’m going to show you how I used those samples to help import a list of store locations to the Quantum Computers website.

Laying the ground work

No matter how many code samples Module Builder gives us, we’re still going to need a way to read our source data. Usually, my source data comes in CSV form and my favorite way to interact with those is by using the FileHelpers library found on SourceForge. (I’m using version 2.0 for this example.)

I’m only going to cover some simple usage of the FileHelpers library. Please take a look at the project documentation for more in-depth information.

To start using the FileHelpers Library download the .zip and add a reference to FileHelpers.dll in your project. You can find it in the Release directory of the distribution.

After adding that reference, we can start building our import script. The fastest way to get started is by creating a quick Web Form in the root of the Sitefinity project. That will provide a perfect place to start coding quickly and without any fuss.

We need to define a class that maps to our source data in order to use FileHelpers. The properties of that class need to be in the same order that the values appear in the CSV. Here’s a sample line from the stores.csv file I'm using and what the matching class will look like:

After that code runs, we’ll have all the information we need to start creating our Sitefinity items. The best part is that now we don’t have to do a lot of crazy data parsing on our CSV data. FileHelpers did it for us!

Tying in to Sitefinity

Now that we’ve got an array full of Store objects, let’s loop through it and start creating some Sitefinity content. This is where the code reference that Sitefinity provides eliminates almost all of our remaining coding effort. I’ve copied the code from the “Create a Store” section of the code reference and used it to create the following method.

// Creates a new store item

publicvoidCreateStore(Store s)

{

// Set the provider name for the DynamicModuleManager here. All available providers are listed in

I want to point out the call to “GetGeoCoordinates”. That’s a method I pieced together that calls Google and requests the latitude and longitude for a given address. That wasn't provided for us but I’ve included it (along with a convenience class for passing the coordinates back) for reference below.

There are a lot of comments included by default and they do a good job of pointing out what’s going on. There are a few that I added to explain areas where I tweaked the code.

Doing the import

All that’s left to do is build the solution and make a quick visit to our script via a browser. I named the script "StoreImport.aspx" so i just need to visit "~/StoreImport.aspx" to run it. Once our code finishes, click back over to the module to see all the items we just created.

And there we have it. Minus a few lines of code for accessing our external data and the Google API, we just populated a custom module using a bunch of code we didn’t have to write!

I invite you to join me on December 10th for my webinar on Module Builder where I'll be diving deeper into the code above and handing out tips on using it to be more productive.

4 comments

This should be built in like mobile at no additional cost, however thank you very much for addressing.

I am looking forward to the webinar.

BoaMike Nov 24, 2013

I tend to agree with Bill... If it's this easy to import data from a CSV into a custom module, why isn't importing data supported along with importing of structure, out of the box for those who don't have Site-Sync?

Still, a very handy post, though...Thanks Tim!

Richard Sep 12, 2014

So we're roughly a year on...has this functionality been added to Sitefinity? or will I still need to wade through Tim's code with my limited programming skills and figure out how to import this data...?

Webinars

Tools

Progress, Telerik, Sitefinity and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries.