Adding and removing controls
Sitefinity allows you to add and remove controls from page through the Pages API.
When adding a control to page, you must perform the following:
-
Get the page node or data.
When adding control to page, first, you must get the page node or data. For more information about finding a specific page node and data, see Querying pages.
-
Get the temp draft of the page.
You must add the controls to the temp draft of the page.
-
Create draft control.
To create the draft control, you use the page manager. To duplicate the control in the UI, you must set the ID property of the original control. For more information, see Generate unique control Id.
When creating the draft control, it is recommended to set the following properties:
-
Set default permissions for the draft control.
You must set the default permissions for the draft control.
-
Add the control.
After you set the properties and permissions, you add the control to the temp draft of the page.
-
Get master version of the page.
After you add the control to the page, you must check in the page to get the master version.
-
Publish the page.
Finally, you must publish the page. To publish the page, you use the workflow manager.
This section contains the following:
Generate unique control Id
The following code generates unique control Id in a page:
private string GenerateUniqueControlIdForPage(PageDraft pageDraft)
{
int controlsCount = 0;
if (pageDraft != null)
{
controlsCount = pageDraft.Controls.Count;
}
return String.Format("C" + controlsCount.ToString().PadLeft(3, '0'));
}
First, you get the number of controls in the page draft. Then, you format a string, used for control identifier in the page.
Get last control in placeholder
The following code gets the last control Id in the specified placeholder:
private Guid GetLastControlInPlaceHolderInPageId(PageDraft pageDraft, string placeHolder)
{
var id = Guid.Empty;
PageDraftControl control;
var controls = new List<PageDraftControl>(pageDraft.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;
}
First, you get all control in the specified placeholder. Then, you iterate through them until the last control is reached.
Adding control to page
The following example adds the control to the specified page:
public void AddControlToPage(Guid pageNodeId, Control control, string placeHolder, string caption)
{
var pageManager = PageManager.GetManager();
var page = pageManager.GetPageNodes().Where(p => p.Id == pageNodeId).SingleOrDefault();
if (page != null)
{
var temp = pageManager.EditPage(page.Page.Id);
if (temp != null)
{
if (string.IsNullOrEmpty(control.ID))
{
control.ID = GenerateUniqueControlIdForPage(temp);
}
var pageControl = pageManager.CreateControl<PageDraftControl>(control, placeHolder);
pageControl.Caption = caption;
pageControl.SiblingId = GetLastControlInPlaceHolderInPageId(temp, placeHolder);
pageManager.SetControlDefaultPermissions(pageControl);
temp.Controls.Add(pageControl);
pageManager.PagesLifecycle.CheckIn(temp);
pageManager.SaveChanges();
// Publish
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(PageNode).FullName);
WorkflowManager.MessageWorkflow(pageNodeId, typeof(PageNode), null, "Publish", false, bag);
}
}
}
First, you initialize the PageManager. Then, you get the page node using GetPageNodes and filtering based on Id. You create the page draft by calling EditPage. Then, if not set, you use the GenerateUniqueControlIdForPage method to generate unique id for the control in the page. You create the control draft using the CreateControl<PageDraftControl> method and specifying the placeholder. You set the caption and find the sibling control Id by calling GetLastControlInPlaceHolderInPageId and specifying the temp draft and placeholder. You set the default permissions by calling SetControlDefaultPermissions. Then, you add the control to the temp draft and call CheckIn to get the master. Finally, to publish the news item in live state, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.
Adding user control to page
The following example adds the user control to the specified page:
public void AddControlToPage(Guid pageNodeId, string controlPath, string placeHolder, string caption, Dictionary<string, string> properties)
{
var pageManager = PageManager.GetManager();
var page = pageManager.GetPageNodes().Where(p => p.Id == pageNodeId).SingleOrDefault();
if (page != null)
{
var temp = pageManager.EditPage(page.Page.Id);
if (temp != null)
{
var pageControl = pageManager.CreateControl<PageDraftControl>(controlPath, placeHolder);
foreach (KeyValuePair<string, string> property in properties)
{
var prop = pageControl.Properties.FirstOrDefault(p => p.Name == property.Key);
if (prop == null)
{
prop = new ControlProperty();
prop.Id = Guid.NewGuid();
prop.Name = property.Key;
prop.Value = property.Value;
pageControl.Properties.Add(prop);
}
}
pageControl.Caption = caption;
pageControl.SiblingId = GetLastControlInPlaceHolderInPageId(temp, placeHolder);
pageManager.SetControlDefaultPermissions(pageControl);
temp.Controls.Add(pageControl);
pageManager.PagesLifecycle.CheckIn(temp);
pageManager.SaveChanges();
// Publish
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(PageNode).FullName);
WorkflowManager.MessageWorkflow(pageNodeId, typeof(PageNode), null, "Publish", false, bag);
}
}
}
First, you initialize the PageManager. Then, you get the page node using GetPageNodes and filtering based on Id. You create the page draft by calling EditPage. You create the control draft using the CreateControl<PageDraftControl> method and specifying the placeholder. You add all control properties from the dictionary by wrapping them in ControlProperty object. To duplicate the control in the UI, you must add the ID property. You set the caption and find the sibling control Id by calling GetLastControlInPlaceHolderInPageId and specifying the temp draft and placeholder. You set the default permissions by calling SetControlDefaultPermissions. Then, you add the control to the temp draft and call CheckIn to get the master. Finally, to publish the news item in live state, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.
Removing control from page
The following example removes the first control from the specified page:
public void RemoveControlFromPage(Guid pageNodeId)
{
var pageManager = PageManager.GetManager();
var page = pageManager.GetPageNodes().Where(p => p.Id == pageNodeId).SingleOrDefault();
if (page != null)
{
var temp = pageManager.EditPage(page.Page.Id);
if (temp != null)
{
// Remove the first child control in the controls collection
if (temp.Controls.Count > 0)
{
temp.Controls.RemoveAt(0);
}
pageManager.PagesLifecycle.CheckIn(temp);
pageManager.SaveChanges();
// Publish
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(PageNode).FullName);
WorkflowManager.MessageWorkflow(pageNodeId, typeof(PageNode), null, "Publish", false, bag);
}
}
}
First, you initialize the PageManager. Then, you get the page node using GetPageNodes and filtering based on Id. To remove the control, you must create the temp draft. To do this, you use the EditPage method. When calling the method you specify the ID of the page. To remove the first control, you use temp.Controls.RemoveAt(0). Then, you call CheckIn to get the master. Finally, to publish the news item in live state, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.