Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonathan Caddy 3 posts 23 karma points
    Apr 22, 2014 @ 01:18
    Jonathan Caddy
    0

    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.

  • Jonathan Caddy 3 posts 23 karma points
    Apr 22, 2014 @ 01:57
    Jonathan Caddy
    0

    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.

  • Javier Barrera 34 posts 85 karma points
    Apr 28, 2014 @ 19:28
    Javier Barrera
    0

    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.

  • Javier Barrera 34 posts 85 karma points
    Apr 28, 2014 @ 19:41
    Javier Barrera
    0

    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;
        }
    }
  • Jonathan Caddy 3 posts 23 karma points
    Apr 29, 2014 @ 14:45
    Jonathan Caddy
    0

    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.

  • Javier Barrera 34 posts 85 karma points
    Apr 29, 2014 @ 16:39
    Javier Barrera
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft