Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > How to update widget instance's properties programmatically. View modes: 
User avatar
Certified Developer 8
Certified Developer 8
Nate - 8/12/2011 3:52:54 PM
   
How to update widget instance's properties programmatically.
I am working on an editable webpart that needs to change the value of one of the widget properties when the page is saved.

I know how to override the SaveContent method of the EditableWebPartControl, but I don't know how to save the changed properties of the widget instance.

How do I do this?

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/15/2011 3:24:41 AM
   
RE:How to update widget instance's properties programmatically.
Hi,

you could use following code to change value of some property. Following example changes ContentBefore property of ScrollingNews widget inserted in the zoneLogo (editor widget zone):

PageInfo pi = PageInfoProvider.GetPageInfo("CorporateSite", "/", "en-us", "", false,ConnectionHelper.ContextConnection);
PageTemplateInfo pt = pi.PageTemplateInfo;
WebPartZoneInstance wpzi = pt.GetZone("zoneLogo");
WebPartInstance wpi = wpzi.GetWebPart("ScrollingNews");
wpi.SetValue("ContentBefore", "myValue");
//Save info
PageTemplateInfoProvider.SetPageTemplateInfo(pt);


Best regards,
Ivana Tomaniickova

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/15/2011 10:28:15 AM
   
RE:How to update widget instance's properties programmatically.
Thanks for your reply.

The code that you provided me did not work. It seems to me that your code will save the WebPart properties in the Template object and not the Widget Properties in the Document object.

Also, remember that I am doing this from within the SaveContent method of an EditableWebpart control, so the PageInfo is probably being modified by all of the EditableWebparts on the page before it is saved.

Can this be done?

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/15/2011 3:14:23 PM
   
RE:How to update widget instance's properties programmatically.
It doesn't look like it can be done because there is no way to edit the properties of the Node stored in CMSPortalManager's mCurrentNode so any changes you make are overwritten when that instance gets saved.

Here is the code from CMSPortalManager.SaveContent


User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/16/2011 4:49:22 AM
   
RE:How to update widget instance's properties programmatically.
Hi,

the code I provided works with editor's widgets inserted in the Design tab.

Could you please specify where exactly have you inserted the widget (Page or Design tab?) and in which zone (User personalization, Customization by group administrator, Customization by page editor?).

I removed the code from your post - as it is a part of full source code and sharing it without permission is illegal (mainly if you have not bought it).

Best regards,
Ivana Tomanickova

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/16/2011 10:54:59 AM
   
RE:How to update widget instance's properties programmatically.
The widgets I am wanting to work with are the ones that are customizable by the page editor.

The reason it won't work is because the widget properties are stored in DocumentWebParts while the content is stored in DocumentContent.

The CMSPortalManager SaveContent method gets a reference to the private TreeNode for the page, but does not pass it to the SaveContent methods of the webpart controls, it only passes the PageInfo instance. Then when it is returned, it simply copies the DocumentContent from the PageInfo instance to the TreeNode instance and then saves the tree node.

Since I cannot gain reference to the private TreeNode object from within the SaveContent method, I cannot change it's properties. Also, since the instance of the TreeNode Object is obtained before the SaveContent method is executed on all the webparts and then saved afterwards, any changes I may make to the TreeNode during SaveContent are overwritten with the values stored in the private TreeNode instance of the CMSPortalManager.

Since I can't modify this source code, I can't do anything about it.

The main reason I want to be able to do this is to find a way to allow a user to edit the content inside an EditableContent webpart both on the page and in a dialog window if they choose to do so. This has proven to be quite a challenge.

Also, since they actually prefer to edit content in a dialog window, I am currently using a modified Static Html widget with the HTML Toolbar. Since widget properties are stored in DocumentWebParts, the content does not get saved to DocumentContent and therefore does not show up as the content field of search results. DocumentWebParts cannot be configured as the search result content field of the CMS.MenuItem document type.

So, what I am really after is a way to get an editor in a dialog window for content that is stored in DocumentContent that can be used by the editors.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/16/2011 12:42:12 PM
   
RE:How to update widget instance's properties programmatically.
Ok, so I figured out a way to do it, but it required me to edit the WidgetProperties.ascx.cs file.

I added a dummy field to my editable content control called "WidgetContent" then I changed WidgetProperties.ascx.cs to load content for this field from the PageContent and to save it back to PageContent.

Now I can edit the content in the on the page or in the widget properties dialog window. It works beautifully.

User avatar
Member
Member
srikanth - 1/24/2012 8:14:50 AM
   
RE:How to update widget instance's properties programmatically.
can you please provide me the steps to do this? I am also working on same task as explained by you.



User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 1/24/2012 12:54:34 PM
   
RE:How to update widget instance's properties programmatically.
I'll write up some steps for you when I get a moment. It's not very difficult. Do you need it for 5.5 or 6.0?

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 1/26/2012 12:57:13 PM
   
RE:How to update widget instance's properties programmatically.
Like I said in an earlier post, I just make a field in my webpart called "widgetcontent" and use the HtmlEditor. Then, to get it to save that field's data as page content, I had to edit the file stored in /CMSModules/Widgets/Controls/WidgetProperties.ascx.cs and change the SaveFormToWidget method like this

/// <summary>
/// Saves the given DataRow data to the web part properties.
/// </summary>
/// <param name="form">Form to save</param>
private void SaveFormToWidget(BasicForm form)
{
if (form.Visible && (widgetInstance != null))
{
// Keep the old ID to check the change of the ID
string oldId = widgetInstance.ControlID.ToLower();

DataRow dr = form.DataRow;

// Load default values for new widget
if (IsNewWidget)
{
form.FormInformation.LoadDefaultValues(dr, wi.WidgetDefaultValues);
}

foreach (DataColumn column in dr.Table.Columns)
{
//ADD THIS CODE HERE TO STORE THE WIDGET CONTENT INTO PAGE DATA
if (column.ColumnName.Equals("widgetcontent", StringComparison.InvariantCultureIgnoreCase))
{
try
{
string str2 = widgetInstance.ControlID.ToLower();
if (widgetInstance.InstanceGUID != Guid.Empty)
{
str2 = str2 + ";" + this.InstanceGUID.ToString().ToLower();
}

TreeNode node = DocumentHelper.GetDocument(pi.DocumentId, tree);

// Move the content in the page info
pi.EditableWebParts[str2] = dr[column];


// Update the document
node.SetValue("DocumentContent", pi.GetContentXml());
DocumentHelper.UpdateDocument(node, tree);

}
catch (Exception ex)
{

}
}
else
{
widgetInstance.MacroTable[column.ColumnName.ToLower()] = form.MacroTable[column.ColumnName.ToLower()];
widgetInstance.SetValue(column.ColumnName, dr[column]);
}


// If name changed, move the content
if (String.Compare(column.ColumnName, "widgetcontrolid", true) == 0)
{
try
{
string newId = ValidationHelper.GetString(dr[column], "").ToLower();

// Name changed
if (!String.IsNullOrEmpty(newId) && (String.Compare(newId, oldId, false) != 0))
{
mWidgetIdChanged = true;
WidgetId = newId;

// Move the document content if present
string currentContent = (string)(pi.EditableWebParts[oldId]);
if (currentContent != null)
{
TreeNode node = DocumentHelper.GetDocument(pi.DocumentId, tree);

// Move the content in the page info
pi.EditableWebParts[oldId] = null;
pi.EditableWebParts[newId] = currentContent;

// Update the document
node.SetValue("DocumentContent", pi.GetContentXml());
DocumentHelper.UpdateDocument(node, tree);
}

// Change the underlying zone names if layout web part
if ((wpi != null) && ((WebPartTypeEnum)wpi.WebPartType == WebPartTypeEnum.Layout))
{
string prefix = oldId + "_";

foreach (WebPartZoneInstance zone in pti.WebPartZones)
{
if (zone.ZoneID.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase))
{
// Change the zone prefix to the new one
zone.ZoneID = String.Format("{0}_{1}", newId, zone.ZoneID.Substring(prefix.Length));
}
}
}
}
}
catch (Exception ex)
{
EventLogProvider ev = new EventLogProvider();
ev.LogEvent("Content", "CHANGEWIDGET", ex);
}
}
}
}
}


And then also edit some code in the OnInit method right under where LoadDataRowFromWidget(dr); is called

// Load values from existing widget
LoadDataRowFromWidget(dr);
foreach (DataColumn dc in dr.Table.Columns)
{
if (dc.ColumnName.Equals("widgetcontent", StringComparison.InvariantCultureIgnoreCase))
{
try
{
string str2 = widgetInstance.ControlID.ToLower();
if (widgetInstance.InstanceGUID != Guid.Empty)
{
str2 = str2 + ";" + this.InstanceGUID.ToString().ToLower();
}

TreeNode node = DocumentHelper.GetDocument(pi.DocumentId, tree);

// Move the content in the page info

DataHelper.SetDataRowValue(dr, dc.ColumnName, pi.EditableWebParts[str2]);



}
catch (Exception ex)
{

}
}
}



This will work with any webpart where you want the data to be stored in PageData so that it is indexed in the search engine and you can have the html editor on the page as well as in the edit dialog if you use the Editable Text webpart as a base.

User avatar
Member
Member
srikanth - 1/27/2012 9:05:46 AM
   
RE:How to update widget instance's properties programmatically.
thanks..