This project is read-only.

Creating Widgets in Recipes

Topics: General, Troubleshooting
Apr 5, 2011 at 8:59 AM
Edited Apr 5, 2011 at 9:25 AM

Hi guys,

I've been trying to create Widgets in a Recipe. I can create the actual Widget e.g. for BingMaps:


widget create /Type:BingMapWidget /Zone:Content /Title:"Location" /Layer:LocationPage


, but I can't see how to set the default values for that particular Widget e.g. Longitude & Latitude. Where should I be looking?

Thanks for your help :)

Apr 5, 2011 at 9:15 AM
Edited Apr 5, 2011 at 9:18 AM

I have tried to create a custom command in Bing.Maps.Commands.BingMapCommands.cs to replace the above and create the widget through the BingMaps module rather than the Widgets module... but found my class wouldn't accept:


using Orchard.Widgets;


..saying "The type or namespace 'Widgets' does not exist in the namesparce 'Orchard'??!. This then meant I couldn't do the following to edit the Widget fields:


var location= _content.New("BingMapWidget");

location.As<WidgetPart>().LayerPart = "LocationPage"



So then I wondered whether I should edit Orchard.Widgets.Commands.WidgetCommands.cs to add in custom Switches... but this feels wrong as it's mucking around with the Orchard source.


Any advice much appreciated :)

Apr 5, 2011 at 10:01 AM

Sorry, being dumb. Added Orchard.Widgets as a Reference to the project and now have access to WidgetPart and IWidgetService. 

Now stumbling over selecting the layer. Can see a method on the widget service so I can do: location.As<WidgetPart>().LayerPart = _widgets.GetLayer(1).

Be nice if there was a GetLayerByName(string layer)... the coding continues...

Apr 5, 2011 at 10:25 AM

Ok, well end result is that it worked. 

To create a custom widget from a recipe one needs to (it appears) create a custom command and then simply set the WidgetPart attributes on the custom widget :)

At the moment I've hardcoded the value in GetLayer(), but going to write a GetLayerByName(string layer) - unless this can already be easily achieved.

Apr 5, 2011 at 10:58 AM

Ahhh this is what's needed:

var layers = _widgets.GetLayers();            

var layer = layers.FirstOrDefault(layerPart => String.Equals(layerPart.Name, "LocationPage", StringComparison.OrdinalIgnoreCase));