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:

 ContentDefinitionManager.AlterTypeDefinition("GearSearchWidget",
              cfg => cfg
                  .WithPart("GearSearchWidgetPart")
                  .WithPart("CommonPart")
                  .WithPart("WidgetPart")
                  .WithSetting("Stereotype", "Widget")
              );

            _commerceSetupService.Setup();

 

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")
                .List().FirstOrDefault();

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

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

 

            //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";
            _contentManager.Publish(beforeContent_gearShoppingArea);

 

 

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))
	.List().FirstOrDefault();

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) {
	_contentManager.Remove(searchFormWidgets.First().ContentItem);
}

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.