Updating widget with Kentico API

Tomasz Czado asked on January 27, 2015 17:26

Hi,

I have spent a lot of time to searching a solution for my problem. I'm looking to a way to update a widget property added to document. Widget is added on page tab of document. If you want me to add additional informations, let me know.

Of course I want to do it with Kentico API.

I'll be highly appreciated for any help.

Recent Answers


Brenden Kehren answered on January 27, 2015 18:08

On the Edit tab of the page, you should be able to click the widget's gear icon to configure it.

If you need to configure the default property value for all future instances of a widget, you will need to go to Site Manager>Development>Widgets and find the widget you are working with and edit the field/property default value. Again, it will only make modifications to new instances added to a page, not existing ones.

0 votesVote for this answer Mark as a Correct answer

Tomasz Czado answered on January 27, 2015 18:22

Hi, thanks for reply, but I'm asking how to do it with Kentico API :)

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 28, 2015 01:16

Can't say I saw that in the original post.

I'm assuming you have read the short documentation on the Widget and the API? They will provide some info how how they are all related and a starting point on what to access. Unfortunately, other than that I won't be of much help.

Maybe we could step back a bit and find out why you want to edit those properties? Maybe there is a different solution for this.

0 votesVote for this answer Mark as a Correct answer

Tomasz Czado answered on January 28, 2015 10:01

I have some custom module allowing users to create new site from template - that template contains widget as header logo. The goal is that I want to update that header logo "Image" property using Kentico API. So I know code name of widget and zone, but I'm trying to figuring out how to update that property. As I observed, content for this widget are stored in CMS_VersionHistory table, but I didn't saw it in DocumentContent column in CMS_Document table. I had found the way to add new widget but I don't know, how to update already added widget.

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on January 28, 2015 16:02

Tomasz, don't you want to try to setup a macro (perhaps custom macro) to the widget property field, so it will be resolved during runtime, so no need to update it after site installation?

0 votesVote for this answer Mark as a Correct answer

Tomasz Czado answered on January 28, 2015 16:52

I'm sorry, but how macro will work? How this macro could looks like and how it can update image src property in widget after site instalation?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 29, 2015 02:47

I like Roman's idea of the macro. The macro could be as simple as retrieving a setting or a path property from the content tree.

0 votesVote for this answer Mark as a Correct answer

Tomasz Czado answered on February 5, 2015 12:03 (last edited on February 5, 2015 12:43)

Well, I think that I have found a solution, please to look at this code (objects have to be checked if they are null):

  string culture = "en-US";
  string aliasPath = "/";
  string widgetName = "Image";
  string zoneID = "Test";

  CMS.PortalEngine.WidgetZoneTypeEnum zoneType = CMS.PortalEngine.WidgetZoneTypeEnum.None;

  CMS.DocumentEngine.TreeProvider tree = new CMS.DocumentEngine.TreeProvider(CMSContext.CurrentUser);
  CMS.DocumentEngine.TreeNode staticNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, aliasPath, culture);

  CMS.DocumentEngine.PageInfo pi = CMSWebPartPropertiesPage.GetPageInfo(staticNode.NodeAliasPath, staticNode.DocumentPageTemplateID, culture);

  CMS.PortalEngine.PageTemplateInstance templateInstance = CMS.PortalControls.CMSPortalManager.GetTemplateInstanceForEditing(pi);

  CMS.PortalEngine.WebPartInstance widgetInstance =  = templateInstance.GetWebPart(templateInstance.GetWebPart("Image").InstanceGUID, widgetName);

  CMS.PortalEngine.WebPartZoneInstance zone = templateInstance.GetZone(zoneID);
  if ((zoneType == CMS.PortalEngine.WidgetZoneTypeEnum.None) && (zone != null))
  {
      zoneType = zone.WidgetZoneType;
  }

  widgetInstance.SetValue("Image", "image_url");
  CMS.PortalControls.CMSPortalManager.SaveTemplateChanges(pi, templateInstance, zoneType, CMSContext.ViewMode, tree);
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.