Displaying a WidgetPart programatically

Oct 27, 2010 at 10:41 AM

I created a widget and was able to successfully add it on the home page using Admin panel (Add Widget to a Zone).

How I can add widget to a page using code?

Oct 28, 2010 at 12:50 PM

Has any one done this? Adding Widget to a Zone. I am able to display a widget on a page after having placement.info settings plus adding widget to a zone in Admin panel.

How can I avoid setting in Admin panel? Is there any settings I can do in Layout or somewhere else?

 

Regards,

Ajay

Coordinator
Oct 28, 2010 at 5:33 PM

If you are looking to do something like that in code, a good place usually is the SetupService as it's the place where the default structures are being built. A good idea to reproduce some of that in your own modules usually is in a data migration method.

In this case, around line 250, you should see how the home page layer and its three default widgets get created:

// add a layer for the homepage
var homepageLayer = contentManager.Create("Layer");
homepageLayer.As<LayerPart>().Name = "TheHomepage";
homepageLayer.As<LayerPart>().LayerRule = "url \"~/\"";
contentManager.Publish(homepageLayer);

// and three more for the tripel...really need this elsewhere...
var tripelFirst = contentManager.Create("HtmlWidget");
tripelFirst.As<WidgetPart>().LayerPart = homepageLayer.As<LayerPart>();
tripelFirst.As<WidgetPart>().Title = T("First Leader Aside").Text;
tripelFirst.As<WidgetPart>().Zone = "TripelFirst";
tripelFirst.As<WidgetPart>().Position = "5";
tripelFirst.As<BodyPart>().Text = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a nibh ut tortor dapibus vestibulum. Aliquam vel sem nibh. Suspendisse vel condimentum tellus.</p>";
contentManager.Publish(tripelFirst);

var tripelSecond = contentManager.Create("HtmlWidget");
tripelSecond.As<WidgetPart>().LayerPart = homepageLayer.As<LayerPart>();
tripelSecond.As<WidgetPart>().Title = T("Second Leader Aside").Text;
tripelSecond.As<WidgetPart>().Zone = "TripelSecond";
tripelSecond.As<WidgetPart>().Position = "5";
tripelSecond.As<BodyPart>().Text = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a nibh ut tortor dapibus vestibulum. Aliquam vel sem nibh. Suspendisse vel condimentum tellus.</p>";
contentManager.Publish(tripelSecond);

var tripelThird = contentManager.Create("HtmlWidget");
tripelThird.As<WidgetPart>().LayerPart = homepageLayer.As<LayerPart>();
tripelThird.As<WidgetPart>().Title = T("Third Leader Aside").Text;
tripelThird.As<WidgetPart>().Zone = "TripelThird";
tripelThird.As<WidgetPart>().Position = "5";
tripelThird.As<BodyPart>().Text = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur a nibh ut tortor dapibus vestibulum. Aliquam vel sem nibh. Suspendisse vel condimentum tellus.</p>";
contentManager.Publish(tripelThird);

Nov 20, 2010 at 11:04 PM
Edited Nov 20, 2010 at 11:05 PM

Great, but I don't understand why the first time the site is compiled and run I see the tripel zones (First, Second, Third Leader Aside) but when I go to dashboard and come back to home page those zones are not displayed. Even after logging out they are not displayed until the next compile that tripel zones will show up again. Is this a known issue or by design?

 

Coordinator
Nov 21, 2010 at 4:05 AM

That might be a known issue. Can you check whether there is a difference in trailing slash?if the URL is different  even by a trailing slash, the home page widget rule may fail to match and widgets may disappear.

Nov 21, 2010 at 6:46 PM

Yes, just checked the difference is in the trailing slash.

Coordinator
Nov 22, 2010 at 3:41 AM

And? Does that coincide with what you observed before? In other words is it the same issue?

Nov 22, 2010 at 7:03 AM

Yes, it's the same issue. The problem is with matching the trailing slash.