Archief

Dev

External data in Orchard

I have a webservice that gives me information to display on a detail page. This needs to be output in an orchard page with its masterpage and widgets surrounding it.

Colleagues of mine are trying the approach where they import the webservice data into Orchard, and let Orchard figure out the routing and displaying of the content. They have a fixed set of detail pages, none ever disappear and sometimes pages get added. (They are running into performance issues because they need an initial import of about 500.000 items)

My case is a little bit different: the data changes constantly and there can’t be a delay. The data has to come directly from the webservices, I can’t even cache it for a few minutes. So I fiddled around a bit and came up with this solution. In orchard I create a LocationDetails contenttype that’s not creatable and automatically gets the LocationDetailsPart:

ContentDefinitionManager.AlterPartDefinition(typeof(LocationDetailsPart).Name, part => part
.Attachable(false)
);
ContentDefinitionManager.AlterTypeDefinition("LocationDetails", cfg => cfg
  .WithPart(typeof(LocationDetailsPart).Name)
  .Creatable(false)
);

I then implemented a custom Controller that matches a url. If the appropriate content is found on the webservice, a temporary LocationDetails contentitem gets created (but doesn’t get inserted into the database!) and gets passed the details from the webservice.

var details = getStuffFromWebservice();
if (details == null) return new HttpNotFoundResult();

var contentItem = _contentManager.New("LocationDetails");
contentItem.As<LocationDetailsPart>().Details = details;

return new ShapeResult(this, _contentManager.BuildDisplay(contentItem));

After that it’s just using the default display logica of Orchard!

#orchard #webservices