Content Part that displays data from another data source

Dec 29, 2010 at 9:02 PM

I want to create a widget that pulls some statistics from a webservice and only displays in certain layers in my site. What I can't seem to figure out is how to get data into my part model for the widget. Do I need to add logic in the driver to populate the properties of my model, or should the model already have it's data at that point? Please point me in the right direction again ;) the help is really appreciated.

I have a model like this. I inherited from content part because I don't expect to have a record.


WebServicePart : ContentPart {
	public string Total { get;set; }
	public string Sets { get;set; }
	public string TopAverage { get;set; }

I then created a driver:
    public class WebServicePartDriver : ContentPartDriver<WebServicePart>
        protected override DriverResult Display(WebServicePart part, string displayType, dynamic shapeHelper) {
			//Code to hit retrieve data from web service
			var stats = Service.GetStats();
			part.Total = stats.Total;
			part.Sets = stats.Sets;
			part.TopAverage = stats.TopAverage;
            return ContentShape("Parts_Summary", () => shapeHelper.Parts_Summary(
                Total : part.Total,
		Sets : part.Sets,
		TopAverage : part.TopAverage

The view I expect to render is located at ~\Views\Parts\Summary.cshtml.
I can add my widget to the page, but I don't think the driver is called, and the view is not rendered. Thanks for the help

Dec 29, 2010 at 11:23 PM

Through some trial and error, I was able to get the functionality I wanted.

I added a handler to load the data from the webservice in the OnLoading event (is there a better place?):

public WebServicePartHandler() {
	OnLoading<WebServicePart>((ctx, wsp) => 
		var stats = MyWebService.GetStats();
		wsp.Total = stats.Total;
		wsp.Sets = stats.Total;
		wsp.TopAverage = stats.TopAverage;

In the driver, I just passed the part to the content shape without any modification:


protected override DriverResult Display(WebServicePart part, string displayType, dynamic shapeHelper)
	return ContentShape("Parts_Summary", () => shapeHelper.Parts_Summary(part));

After doing this, the data was rendered in my widget.