How to obtain control id guid using API

Posted by Community Admin on 04-Aug-2018 19:04

How to obtain control id guid using API

All Replies

Posted by Community Admin on 21-Mar-2012 00:00

Sitefinity 5.0
I am installing a custom module and part of the install adds a control to the ToolboxConfig.config.
I can access this ToolboxItem using Config.Manager.
I create a new Backend page to hold this control and my code creates the page ok but I am having trouble finding API code to perform 2 final steps I am having to do manually.
Q1.   I wish to add the control using PageNode.Page.Controls.Add(myControl) but I do not know how to create an instance of myControl (needs a guid).
Q2.  I wish to Publish my Page and I can only find PublishPageDraft which in my case, DraftPage is null.
I am looking for tips on how to accomplish the final 2 steps of this module installation.

using (var manager = PageManager.GetManager())
                
                    PageData pageData = null;
                    manager.Provider.SuppressSecurityChecks = true;
                    if (nodeType == NodeType.Standard)
                    
                        // Create the page for the manager itself
                        pageData = manager.CreatePageData(Guid.NewGuid());
                        pageData.HtmlTitle = pageName;
                        pageData.Title = pageName;
                        pageData.Description = pageName;
                        pageData.Culture = Thread.CurrentThread.CurrentCulture.ToString();
                        pageData.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
                         
                    
 
                    PageNode parentPageNode = manager.GetPageNode(parentPageNodeId);
 
                    pageNode = manager.CreatePage(parentPageNode, newPageId, nodeType);
                    pageNode.Page = pageData;
                    pageNode.Name = pageName;
                    pageNode.Description = pageName;
                    pageNode.Title = pageName;
                    pageNode.UrlName = Regex.Replace(pageName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                    pageNode.ShowInNavigation = true;
                    pageNode.LastModified = pageNode.DateCreated = DateTime.UtcNow;
 
                    //if set to false it will appear as Grouping page in the Backend Menu
                    pageNode.RenderAsLink = renderAsLink;
////////////////
 
                     
                    //const string sectionName = "PNI";
                    //var configManager = Config.GetManager();
                    //configManager.Provider.SuppressSecurityChecks = true;
                    //var toolboxConfig = configManager.GetSection<ToolboxesConfig>();
                    //var pageControls = toolboxConfig.Toolboxes["PageControls"];
                    //var section = pageControls.Sections.Where<ToolboxSection>(s => s.Name == sectionName).FirstOrDefault();
 
                    //if (section != null)
                    //
                    //    const string toolboxItemName = "Promo Manager";
                    //    if (section.Tools.Any<ToolboxItem>(ti => ti.Name == toolboxItemName))
                    //   
                    //        ToolboxItem tool = section.Tools.Elements.Where(t => t.Name == toolboxItemName).FirstOrDefault();
                             
                    //   
                    //
 
                    //PageControl mycontrol = new PageControl();
                    //mycontrol.ApplicationName = "Sitefinity";
                    //pageNode.Page.Controls.Add(mycontrol);
/////////////////
 
                    manager.SaveChanges();

Posted by Community Admin on 23-Mar-2012 00:00

Hello,

I'm glad to hear that you've managed to get this far in implementing the desired functionality. I believe you can easily address the remaining issues as well. Please note that for our Sitefinity SDK samples,  we have several projects which are being built dynamically entirely through code (that is all the pages, controls, users, and content is generated upon project initialization in the application's Global.asax codebehind. The Education Starter Kit, for example is one of these projects, which I'd strongly recommend you to take a look at if you want to get a better idea of what's happening when constructing the page through code.
Just to give you some heads-up, please find below some samples on possible ways of adding controls to a page:

public static void AddControlToPage(Guid pageId, Control control, string placeHolder, string caption)
       
           var pageManager = PageManager.GetManager();
           var page = pageManager.GetPageNodes().Where(p => p.Id == pageId).SingleOrDefault();
 
           if (page != null)
           
               var master = pageManager.EditPage(page.Page.Id);
 
               if (master != null)
               
                   if (string.IsNullOrEmpty(control.ID))
                   
                       control.ID = GenerateUniqueControlIdForPage(master, null);
                   
 
                   var pageControl = pageManager.CreateControl<PageDraftControl>(control, placeHolder);
                   pageControl.Caption = caption;
                   pageControl.SiblingId = GetLastControlInPlaceHolderInPageId(master, placeHolder);
                   pageManager.SetControlDefaultPermissions(pageControl);
                   master.Controls.Add(pageControl);
                   master = pageManager.PagesLifecycle.CheckIn(master);
                   pageManager.SaveChanges();
 
                   WorkflowManager.MessageWorkflow(pageId, typeof(PageNode), null, "Publish", false, new Dictionary<string, string>());
               
           
       
from which you might need these as well:
private static Guid GetLastControlInPlaceHolderInPageId(PageDraft page, string placeHolder)
        
            var id = Guid.Empty;
            PageDraftControl control;
 
            var controls = new List<PageDraftControl>(page.Controls.Where(c => c.PlaceHolder == placeHolder));
 
            while (controls.Count > 0)
            
                control = controls.Where(c => c.SiblingId == id).SingleOrDefault();
                id = control.Id;
 
                controls.Remove(control);
            
 
            return id;
        
and
private static string GenerateUniqueControlIdForPage(PageDraft pageNode, string culture)
        
            int controlsCount = 0;
 
            if (pageNode != null)
            
                controlsCount = pageNode.Controls.Count;
            
 
            string cultureSufix = (string.IsNullOrEmpty(culture)) ? string.Empty : string.Format("_" + culture);
 
            return String.Format("C" + controlsCount.ToString().PadLeft(3, '0') + cultureSufix);
        

On issue 2 I'd like to turn your attention to the approach we're applying with messaging WorkFlowManager with command Publish in the above samples, you can follow the same manner in your code, like this:
public static bool CreatePage(Guid pageId, string pageName, bool isHomePage, Guid parentPageId)
       
           bool result = false;
           var pageDataId = Guid.NewGuid();
           var parentId = parentPageId;
 
           if (parentId == Guid.Empty)
           
               parentId = SiteInitializer.FrontendRootNodeId;
           
 
           var count = 0;
           using (var fluent = App.WorkWith())
           
               fluent.Pages().Where(p => p.Id == pageId).Count(out count);
 
               if (count == 0)
               
                   fluent.Page()
                   .CreateNewStandardPage(parentId, pageId, pageDataId)
                   .Do(p =>
                   
                       p.Title = pageName;
                       p.Name = pageName;
                       p.Description = pageName;
                       p.UrlName = Regex.Replace(pageName.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString);
                       p.ShowInNavigation = true;
                       p.DateCreated = DateTime.UtcNow;
                       p.LastModified = DateTime.UtcNow;
                       p.Page.HtmlTitle = pageName;
 
                       p.Page.HtmlTitle = pageName;
                       p.Page.Title = pageName;
                       p.Page.Description = pageName;
                       p.Page.Culture = Thread.CurrentThread.CurrentCulture.ToString();
                       p.Page.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
                   )
                   .SaveChanges();
 
                   if (isHomePage)
                       fluent.Page(pageId).SetAsHomePage().SaveChanges();
 
                   WorkflowManager.MessageWorkflow(pageId, typeof(PageNode), null, "Publish", false, new Dictionary<string, string>());
 
                   result = true;
               
           
 
           return result;
       
for which you might need:
public const string UrlNameCharsToReplace = @"[^\w\-\!\$\'\(\)\=\@\d_]+";
 
        public const string UrlNameReplaceString = "-";
For more information please refer to this article from our documentation, or the SampleUtilities class form our Sitefinity SDK. If you have any additional questions, please do not hesitate to let us know, we'll be glad to help.


All the best,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 02-Apr-2012 00:00

Hello there,

My colleague (Ted) was the person who started this forum post. I have been trying to use the code provided and it freezes (never gets passed) the line to publish anything (the control or the page). No matter what it always fails (and no logs recorded) on: WorkflowManager.MessageWorkflow(pageId, typeof(PageNode), null"Publish"falsenewDictionary<stringstring>());

I feel like it is permissions/security related but am not sure. The one difference I noticed is that the code supplied below in the response seems to be geared towards frontend pages whereas we are trying to create backend pages. Is there some extra security related code we have to add for backend pages? Is there another way to publish items other then the line provided that does not work for us? Any help is greatly appreciated. Thanks!

This thread is closed