More in this section

Forums / Developing with Sitefinity / Edit Control Property Programattically

Edit Control Property Programattically

3 posts, 0 answered
  1. Jeff
    Jeff avatar
    11 posts
    Registered:
    23 Aug 2011
    23 Aug 2011
    Link to this post

    I am looking at the article from the Sitefinity 4 documentation "Adding or Removing Controls" (http://www.sitefinity.com/40/help/developers-guide/sitefinity-essentials-pages-adding-and-removing-controls.html).


    In general, how do I edit an existing control?  Is it basically the same except after calling
    PageManager.GetManager().EditPage
    you simply find the draft control in the .Controls collection of the page draft and set the properties you want to set, then publish the page?  This didn't work for me when I tried it.

    Is there an example for editing just like for adding?

  2. Jeff
    Jeff avatar
    11 posts
    Registered:
    23 Aug 2011
    26 Aug 2011
    Link to this post
    If the general question above isn't clear, here are the specifics of my situation.

    In my case I already have the GUID of the control I want to edit.

    This is what I tried:
    - I get a copy of the control by iterating every page in PageManager.GetManager().GetPageDataList(), and every control on those pages looking for the one with the GUID I already know.
    - I get the GUID of the page that contains the control from the control's .Page property
    - I follow the example in the article to get a draft of the page that contains my control
    - I then search the controls on the draft page for one with the same control ID (not GUID, since the draft GUID is different) as the one I want to edit
    - I then edit the properties of the draft control
    - finally, I publish the draft page

    var control = this.FindControlByGuid(controlID);
    if (control != null)
    {
    var page = control.Page;
    var manager = PageManager.GetManager();
    if (page.Status != ContentLifecycleStatus.Temp)
    {
    try
    {
    var draftPage = manager.EditPage(page.Id, false);
    PageDraftControl draftControl = null;
    foreach (var currentControl in draftPage.Controls)
    {
    if( this.getControlPropertyValue(currentControl, "Id") == this.getControlProperty(control, "Id"))
    {
    draftControl = currentControl;
    break;
    }
    }
    if (draftControl != null)
    {
    this.setControlPropertyValue(draftControl, propertyName, newValue);
    manager.PublishPageDraft(draftPage.Id, true);
    }
    }
    catch (Exception ex)
    {
    throw;
    }
    }
    }

    Assume that "this.getControlPropertyValue" and "this.FindControlById" work correctly, as in practice I found that they do. There are no exceptions. I am testing success by querying the table [sf_control_properties] to see if my changes appear either in the existing control property record, or as a new control property record. I see no changes.

  3. Boyko Karadzhov
    Boyko Karadzhov avatar
    56 posts
    Registered:
    05 Dec 2016
    26 Aug 2011
    Link to this post
    Hello Jeff,

     You need to call manager.SaveChanges() at the end, after PublishPageDraft(...), to apply the changes. Considering all other methods are working properly this should do the trick.

    P.S.: If you know the Guid of your control you can get it using manager.GetControl<PageControl>(Guid) instead of iterating through the pages.

    Regards,
    Boyko Karadzhov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

3 posts, 0 answered