I am new to Umbraco and in the process of evaluating the platform with a view to standardising on it as our main CMS. I love the separation of content from presentation and the fact that you have complete control over the presentation output through templates. I am also familiar with the ASP.NET MVC way of developing web apps.
My question is about Umbraco best practice in surfacing content from an external source. I would like to create a Content Type which matches the data format of the external source (web service or database) and then programmatically create content to be displayed in a site page from this source. The most obvious analogy would be a news service which I could display news items in a panel on pages in the Umbraco site. Now I am aware I could throw a bunch of code into a Razor view template, but I'd like to keep that out of my views. From a plain MVC perspective I would probably create some data access code and inject components into a MVC controller which would create some model for display in a MVC view.
What is the best way of achieving this with Umbraco? Does Umbraco use MVC controllers in the same way - with action methods, model binding etc?
If someone could point me in the right direction I would be very grateful - and any documentation resources would help too.
I guess one just needs to poke around the documentation - RTFM as they say. It would appear that SurfaceControllers are the way to achieve what I need. If anyone has any comments or additional information I would still be delighted to hear.
You're correct about using SurfaceControllers to accomplish this. We also used the Content Service in v6 (https://umbraco.com/follow-us/blog-archive/2013/1/22/introducing-contentservice-aka-the-v6-api.aspx) to programatically add/update/remove documents so that our internal Umbraco data matched our external data.
In our case, we connected to a RESTFul web service, got our JSON data, and interated through that to see what matched our content in Umbraco. We performed our add/update/remove logic from there.
Here's a quick and dirty CSHTML web page that we created, which could easily be turned into a SurfaceController. These are kinda nice since they are easy to create and debug. However, it should probably be put into a controller so that we can create a unit test for it.
@using RETS.Models
@using RETS.Wrapper
@using Umbraco.Core.Models
@using Umbraco.Core.Services
@using umbraco.editorControls.SettingControls.Pickers
@{
var searchRequest = new ResidentialListingSearch(); // Function to get search listings.
var residentialListings = new List<ResidentialListing>();
try
{
residentialListings = new SearchRequest().Search(searchRequest);
}
catch (Exception)
{
}
int pagesCreated = 0;
int pagesUpdated = 0;
int pagesDeleted = 0;
if (residentialListings.Count > 0)
{
var contentService = new ContentService();
var homeDetailsSection = contentService.GetById(1293);
var homeDetailsSectionChildren = contentService.GetChildren(homeDetailsSection.Id);
foreach (var residentialListing in residentialListings)
{
var listingExists = false;
foreach (var homeDetailsSectionChild in homeDetailsSectionChildren)
{
// Update Page
if (residentialListing.MlsNumber.ToString() == homeDetailsSectionChild.Name)
{
listingExists = true;
var page = UpdateContentValues(homeDetailsSectionChild, residentialListing);
contentService.SaveAndPublish(page);
pagesUpdated++;
}
}
// Create Page
if (!listingExists)
{
var newListingPage = contentService.CreateContent(residentialListing.MlsNumber.ToString(), homeDetailsSection, "HomeDetail", 0);
newListingPage = UpdateContentValues(newListingPage, residentialListing);
contentService.SaveAndPublish(newListingPage);
pagesCreated++;
}
}
// Delete Expired Pages
foreach (var homeDetailsSectionChild in homeDetailsSectionChildren)
{
var listingExpired = true;
foreach (var residentialListing in residentialListings)
{
if (residentialListing.MlsNumber.ToString() == homeDetailsSectionChild.Name)
{
listingExpired = false;
}
}
if (listingExpired)
{
contentService.Delete(homeDetailsSectionChild);
pagesDeleted++;
}
}
}
}
<p>Created: @pagesCreated</p>
<p>Updated: @pagesUpdated</p>
<p>Deleted: @pagesDeleted</p>
@functions
{
private IContent UpdateContentValues(IContent page, ResidentialListing listing)
{
string address = string.Format("{0} {1}", listing.AddressNumber, listing.AddressStreet);
page.SetValue("pageTitle", string.Format("{0} {1}, {2}", address, listing.City, listing.State));
page.SetValue("streetAddress", address);
page.SetValue("city", listing.City);
page.SetValue("state", listing.State);
page.SetValue("zipCode", listing.ZipCode);
page.SetValue("community", listing.Subdivision);
page.SetValue("price", string.Format("{0:C0}", listing.Subdivision));
page.SetValue("description", listing.Remarks);
page.SetValue("dateUpdated", string.Format("{0:MM/dd/yyyy}", DateTime.Now));
return page;
}
}
Javier, thanks for your help - exactly what I was looking for. It is taking me a while to get used to the Umbraco way, but fundamentally I love the fact that you can do all the MVC goodness as well.
Glad I could help! Just an FYI, here's a site that I found a few weeks ago http://24days.in/umbraco/2013/. Its the "24 Days in Umbraco" blog for 2013. It has some great tips if you want to embrace the MVC goodness.
Pushing external data into Umbraco Content Types
I am new to Umbraco and in the process of evaluating the platform with a view to standardising on it as our main CMS. I love the separation of content from presentation and the fact that you have complete control over the presentation output through templates. I am also familiar with the ASP.NET MVC way of developing web apps.
My question is about Umbraco best practice in surfacing content from an external source. I would like to create a Content Type which matches the data format of the external source (web service or database) and then programmatically create content to be displayed in a site page from this source. The most obvious analogy would be a news service which I could display news items in a panel on pages in the Umbraco site. Now I am aware I could throw a bunch of code into a Razor view template, but I'd like to keep that out of my views. From a plain MVC perspective I would probably create some data access code and inject components into a MVC controller which would create some model for display in a MVC view.
What is the best way of achieving this with Umbraco? Does Umbraco use MVC controllers in the same way - with action methods, model binding etc?
If someone could point me in the right direction I would be very grateful - and any documentation resources would help too.
I guess one just needs to poke around the documentation - RTFM as they say. It would appear that SurfaceControllers are the way to achieve what I need. If anyone has any comments or additional information I would still be delighted to hear.
We pull external data to Umbraco at regular intervals using Umbraco's built in task scheduler: http://our.umbraco.org/wiki/install-and-setup/scheduled-tasks.
You're correct about using SurfaceControllers to accomplish this. We also used the Content Service in v6 (https://umbraco.com/follow-us/blog-archive/2013/1/22/introducing-contentservice-aka-the-v6-api.aspx) to programatically add/update/remove documents so that our internal Umbraco data matched our external data.
In our case, we connected to a RESTFul web service, got our JSON data, and interated through that to see what matched our content in Umbraco. We performed our add/update/remove logic from there.
Here's a quick and dirty CSHTML web page that we created, which could easily be turned into a SurfaceController. These are kinda nice since they are easy to create and debug. However, it should probably be put into a controller so that we can create a unit test for it.
Javier, thanks for your help - exactly what I was looking for. It is taking me a while to get used to the Umbraco way, but fundamentally I love the fact that you can do all the MVC goodness as well.
Glad I could help! Just an FYI, here's a site that I found a few weeks ago http://24days.in/umbraco/2013/. Its the "24 Days in Umbraco" blog for 2013. It has some great tips if you want to embrace the MVC goodness.
is working on a reply...