This project is read-only.

Removing Widget

Topics: General
Sep 5, 2011 at 11:04 AM

I have created the GearSearchWidgetPart as Given Below


In Migration I m doing This:

              cfg => cfg
                  .WithSetting("Stereotype", "Widget")



In setup folder I have GearsetupService.cs  in that i am doing somthing like  this


  public class CommerceSetupService : ICommerceSetupService
         private readonly IContentManager _contentManager;

        public CommerceSetupService(IContentManager contentManager)
            _contentManager = contentManager;
            T = NullLocalizer.Instance;
        public Localizer T { get; set; }

public void setup()

 //setting up Gear search Widget on HomePage
            var asideFirst = _contentManager.Create("GearSearchWidget");
            var homepageLayer = _contentManager.Query<LayerPart>()
                .Where<LayerPartRecord>(lp => lp.Name == "TheHomepage")

            asideFirst.As<WidgetPart>().LayerPart = homepageLayer.As<LayerPart>();
            asideFirst.As<WidgetPart>().Title = "";
            asideFirst.As<WidgetPart>().Zone = "AsideFirst";
            asideFirst.As<WidgetPart>().Position = "14";

            // addding a layer for the GearListPage
            var gearListPageLayer = _contentManager.Create("Layer");
            gearListPageLayer.As<LayerPart>().Name = "GearListPageLayer";
            gearListPageLayer.As<LayerPart>().LayerRule = "url \"~/GearDetails/Search\"";


            //adding shopping cart widget to the GearListPageLayer
            var beforeContent_gearShoppingArea = _contentManager.Create("ShoppingCartWidget");
            beforeContent_gearShoppingArea.As<WidgetPart>().LayerPart = gearListPageLayer.As<LayerPart>();
            beforeContent_gearShoppingArea.As<WidgetPart>().Title = "";
            beforeContent_gearShoppingArea.As<WidgetPart>().Zone = "BeforeContent";
            beforeContent_gearShoppingArea.As<WidgetPart>().Position = "1";



and in ICommerceSetupService  I m Doing this

 public interface ICommerceSetupService : IDependency
        void Setup();


 i want to remove it throw code What i need to do in Migration to remove this part....



Oct 13, 2011 at 12:21 AM

Hi Mourice,

thanks for posting your code, it really helped me get started on programmatically adding a widget.

I also found a way to remove a widget through code using an IWidgetsService like follows:

var defaultLayer = _contentManager.Query<LayerPart>()
	.Where<LayerPartRecord>(lpr => lpr.Name.Equals("default", StringComparison.OrdinalIgnoreCase))

var widgetsOnLayer = _widgetsService.GetWidgets(defaultLayer.Id);
var searchFormWidgets = widgetsOnLayer
	.Where(w => w.ContentItem.TypeDefinition.Name == "TeamatonSearchForm")
	.Where(w => w.Zone == "AsideSecond")
	.Where(w => w.Title == "")
	.Where(w => w.Position == "1");

if (searchFormWidgets.Count() == 1) {

Just replace "TeamatonSearchForm" by "GearSearchWidget" and you should be good to go.

In general, I've found that the AdminController classes of the respective modules are a good place to look for code to reuse to imitate the behavior that's available through the admin dashboard, e.g. Orchard.Widgets.Controllers.AdminController.