More in this section
Forums / General Discussions / Programmatically changing user control content

Programmatically changing user control content

The forums are in read-only mode. In case that you want to directly contact the Progress Sitefinity team use the support center. In our Google Plus group you can find more than one thousand Sitefinity developers discussing different topics. For the Stack Overflow threads don’t forget to use the “Sitefinity” tag.
4 posts, 0 answered
  1. Jason
    Jason avatar
    2 posts
    Registered:
    03 Dec 2010
    09 Feb 2011
    Link to this post
    I'm trying to write a method that will look into the custom controls we have in our pages and replace certain pieces of text.  With the code below, I seem to be able to get to the container but not the control.

    Can you please let me know where I'm missing it?
    CmsManager cmsManager = new CmsManager();
     
    foreach (ICmsWebControl control in page.Controls)
    {
        if (control.ControlType == typeof(Telerik.Cms.Engine.WebControls.GenericContent))
        {
            Telerik.Cms.Engine.WebControls.GenericContent goControl = control.LoadControl(this) as Telerik.Cms.Engine.WebControls.GenericContent;
     
            if (goControl.Content.Contains("ReplaceMe"))
            {
                goControl.Content = goControl.Content.Replace("ReplaceMe", "newText");
     
                cmsManager.SaveControl(control);
            }
        }
    }

    Thanks,
    -Jason
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    22 Feb 2011
    Link to this post
    Hi Jason,

    This looks like as a code for Sitefinity 3.x edition. Please check this article . You should work with staged version of the page and you should better specify a container. If you do not make an automated tool you can query the following tables of the database

    For public controls like GenericContent you have to update [sf_CmsControlProperty] table.
    For backend - modules you have to update -[sf_CmsTextContent]




    Greetings,
    Ivan Dimitrov
    the Telerik team
  3. Jason
    Jason avatar
    2 posts
    Registered:
    03 Dec 2010
    25 Feb 2011
    Link to this post
    Ivan,

    Thank you for your reply.  I tried using that code earlier and the trouble with it and with my code is that this line :

    Telerik.Cms.Engine.WebControls.GenericContent gcCtrl = webCtrl.LoadControl() as Telerik.Cms.Engine.WebControls.GenericContent;

    returns nothing - and I don't know why.  I tried using the example you referenced but I ran into the same result.
    Here is the code as it stands, I'm not using the currentNode as in the example, I allow the user to select a parent page and then I change everything in that pagegroup.  Also I'm not calling page.publish() because I read that SavePage(page) was more efficient.
    01.protected void FindAndReplace(Guid parentPage)
    02.{
    03.    CmsManager cmsManager = new CmsManager();
    04.    // Gets all child pages of page
    05.    IList childPages = cmsManager.GetPages(parentPage);
    06.    string pageName = string.Empty;
    07.    foreach (ICmsPage cmsPage in childPages)
    08.    {
    09.        ICmsPage page = (ICmsPage)cmsManager.GetPage(cmsPage.ID, true);
    10.        // If group page get child pages
    11.        if (cmsPage.Pages.Count > 0 || cmsPage.PageType == CmsPageType.Group)
    12.        {
    13.            this.FindAndReplace(page.ID);
    14.        }
    15.        else {
    16.            if (this.ReplacePhraseInPageProperties(cmsPage))
    17.            {
    18.                cmsManager.SavePage(cmsPage);
    19.            }
    20.        }
    21.    }
    22.}
    23.private bool ReplacePhraseInPageProperties(ICmsPage page)
    24.{
    25.    bool modified = false;
    26.    if (page.Title.IndexOf("find") > -1)
    27.    {
    28.        page.Title = page.Title.Replace("find", "replace");
    29.        modified = true;
    30.    }
    31.    if (page.MenuName.IndexOf("find") > -1)
    32.    {
    33.        page.MenuName = page.MenuName.Replace("find", "replace");
    34.        modified = true;
    35.    }
    36.    if (page.Description.IndexOf("find") > -1)
    37.    {
    38.        page.Description = page.Description.Replace("find", "replace");
    39.        modified = true;
    40.    }
    41.    if (page.Name.IndexOf("find") > -1)
    42.    {
    43.        page.Name = page.Name.Replace("find", "replace");
    44.        modified = true;
    45.    }
    46.    // create a new instance of CmsManager
    47.    Telerik.Cms.CmsManager cmsManager = new Telerik.Cms.CmsManager();
    48.    // we are going to use Controls collection of a staged version of a page to
    49.    // access all controls of current page
    50.    IList<Telerik.Cms.ICmsWebControl> pageControls = page.Staged.Controls;
    51.    // to each Generic Content control in SideBarContent we are going to append "[Obsolete]" text
    52.    foreach (Telerik.Cms.ICmsWebControl webCtrl in pageControls)
    53.    {
    54.        if (webCtrl.ControlType == typeof(Telerik.Cms.Engine.WebControls.GenericContent))
    55.        {
    56.            // the following line clearly demonstrates the difference between ICmsWebControl (any control that exists on the Sitefinity page)
    57.            // and the actual control this ICmsWebControl carries. We have checked if ControlType of webCtrl is GenericContent, but to get
    58.            // the actual instance of GenericContent control, we need to call the LoadControl() function of ICmsWebControl. You can think
    59.            // of ICmsWebControl objects as Sitefinity wrapper for the actual control
    60.            Telerik.Cms.Engine.WebControls.GenericContent gcCtrl = webCtrl.LoadControl() as Telerik.Cms.Engine.WebControls.GenericContent;
    61.            if (gcCtrl != null)
    62.            {
    63.                if (gcCtrl.Content.Contains("find"))
    64.                {
    65.                    gcCtrl.Content = gcCtrl.Content.Replace("find", "replace");
    66.                    cmsManager.SaveControl(webCtrl);
    67.                    modified = true;
    68.                }
    69.            }
    70.        }
    71.    }
    72.    // finally we need to publish the current page and refresh it to see the results
    73.    return modified;
    74.}

    Looking at this, can you see why it doesn't work?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    25 Feb 2011
    Link to this post
    Hi Jason,

    Could you verify that the control of type GenericContent is found on the page? I checked the code on a sample page and it works properly at my end.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
4 posts, 0 answered