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:
Quantum Los Angeles West,3611 W 30th St,Los Angeles,90018,US,CA,store5.jpg,+1-888-365-2779
Now it’s time to actually read the file. For this example, I’ve placed it in the App_Data folder of the Sitefinity project. Here’s the snippet to read it:
FileHelperEngine engine =
// To Read Use:
Store stores = engine.ReadFile(HttpContext.Current.Server.MapPath(
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
// Set the provider name for the DynamicModuleManager here. All available providers are listed in
// Administration -> Settings -> Advanced -> DynamicModules -> Providers
var providerName = String.Empty;
// Set the culture name for the multilingual fields
var cultureName =
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
Type storeType = TypeResolutionService.ResolveType(
DynamicContent storeItem = dynamicModuleManager.CreateDataItem(storeType);
// This is how values for the properties are set
, s.Title, cultureName);
, s.Phone, cultureName);
//LibrariesManager libraryManager = LibrariesManager.GetManager();
//var image = libraryManager.GetImages().FirstOrDefault(i => i.FilePath == s.StoreImage && i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
// I commented out the default code above. This is a useful way to find images by filename
var image = App.WorkWith().Images().Where(i => i.Status == ContentLifecycleStatus.Master).Get().ToList().Where(i => Path.GetFileName(i.FilePath) == s.StoreImage).FirstOrDefault();
Address address =
CountryElement addressCountry = Config.Get<LocationsConfig>().Countries.Values.First(x => x.Name ==
address.CountryCode = addressCountry.IsoCode;
address.StateCode = s.State;
address.City = s.City;
address.Street = s.Street;
address.Zip = s.Zip;
// This was added to get Geo Location coordinates from Google
GeoCoordinate geoValue = GetGeoCoordinates(s.Street +
+ s.City +
+ s.State +
+ s.Zip +
address.Latitude = geoValue.Latitude;
address.Longitude = geoValue.Longitude;
address.MapZoomLevel = 8;
// modified a bit to give us a good Url Name
, Regex.Replace(s.Title.ToLower(), @
// Modified to publish instead of set items as draft
// You need to call SaveChanges() in order for the items to be actually persisted to data store
// Use lifecycle so that LanguageData and other Multilingual related values are correctly created
DynamicContent checkOutStoreItem = dynamicModuleManager.Lifecycle.CheckOut(storeItem)
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.
GeoCoordinate geoCoord =
XmlDocument objXmlDocument =
XmlNodeList objXmlNodeList = objXmlDocument.SelectNodes(
// This is for convenience only
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.