Adding a control to a page

Posted by Community Admin on 04-Aug-2018 20:11

Adding a control to a page

All Replies

Posted by Community Admin on 04-Aug-2011 00:00

I am developing a module which creates a page and then adds a control to that page... it works fine and I see it when viewing the page while signed in as admin

But when viewing said page without being signed in I get this

----------------------------------------
Timestamp: 8/4/2011 4:36:03 PM

Message: HandlingInstanceID: c6c66829-9327-4e34-b11f-0bc8dacf51df
An exception of type 'System.ArgumentNullException' occurred and was caught.
----------------------------------------------------------------------------
08/04/2011 12:36:03
Type : System.ArgumentNullException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Value cannot be null.
Parameter name: source
Source : System.Core
Help link :
ParamName : source
Data : System.Collections.ListDictionaryInternal
TargetSite : System.Collections.Generic.IEnumerable`1[TSource] Where[TSource](System.Collections.Generic.IEnumerable`1[TSource], System.Func`2[TSource,System.Boolean])
Stack Trace :    at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
   at Telerik.Sitefinity.Security.SecuredObjectExtensions.GetActivePermissions(ISecuredObject securedObject)
   at Telerik.Sitefinity.Security.SecurityExtensions.IsGranted(ISecuredObject item, String permissionSet, Guid[] principals, Int32 actions)
   at Telerik.Sitefinity.Security.SecurityExtensions.IsGranted(ISecuredObject item, String permissionSet, String[] actions)
   at Telerik.Sitefinity.Security.SecuredObjectExtensions.IsGranted(ISecuredObject securedObject, SecurityActionTypes actionType)
   at Telerik.Sitefinity.Web.UI.SecuredControl.get_IsGranted()
   at Telerik.Sitefinity.Web.UI.SecuredControl.get_Visible()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Control.PreRenderRecursiveInternal()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


If I don't add the control to the page it's fine... any ideas?

Posted by Community Admin on 08-Aug-2011 00:00

Hello Dan,

Can you please let us know of the implementation you have for adding the control on a page. You have to make usre that you are giving your control a certain permissions set, as otherwise the mentioned exception will be thrown, since the control is lacking any permissions. Please take a look at the below sample which is adding a simple Content Block control to a page and is setting certain permissions for that control.

var pagemanager = PageManager.GetManager();
            var cntrl = pagemanager.CreateControl<PageControl>();
            cntrl.ObjectType = typeof(ContentBlock).FullName;
            cntrl.PlaceHolder = "Body";
            cntrl.Caption = "PermissionsTest";
            cntrl.Description = "test";
            string permSet =  SecurityConstants.Sets.Controls.SetName;
            Telerik.Sitefinity.Security.Model.Permission permissionForEveryone = pagemanager.CreatePermission(permSet, ((ISecuredObject)cntrl).Id, SecurityManager.ApplicationRoles["Everyone"].Id);
            string[] actionsAllowedForEveryone =  new string[] SecurityConstants.Sets.Controls.View ;
            permissionForEveryone.GrantActions(true, actionsAllowedForEveryone);
            ((ISecuredObject)cntrl).Permissions.Add(permissionForEveryone);
            var page = pagemanager.GetPageNodes().Where(p => p.Page != null && p.Page.Title == "MyPage1").First();
            page.Page.Controls.Add(cntrl);
            pagemanager.SaveChanges();


Best wishes,
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 10-Aug-2011 00:00

We actually just ran into a similar problem.  Thanks so much for the code snippet, it was very helpful.

A couple of detail points though.

First, you should add further permissions to your control if you wish for it to replicate the same permissions that "drag n drop" has from the widget toolbox.  Please see this snippet.

var newControl = mgr.CreateControl<PageControl>(myControl, "ContentPlaceHolderId");
            newControl.ObjectType = typeof(MyControlClass).FullName;
            newControl.Caption = "My Super Cool Custom Control";
            string perms = SecurityConstants.Sets.Controls.SetName;
             
// Give Everyone View
var permissionEveryone = mgr.CreatePermission(perms, ((ISecuredObject)newControl).Id, SecurityManager.ApplicationRoles["Everyone"].Id);
            var actionsEveryone = new string[] SecurityConstants.Sets.Controls.View ;
            permissionEveryone.GrantActions(true, actionsEveryone);
            ((ISecuredObject)newControl).Permissions.Add(permissionEveryone);
 
// Give BackendUsers Move,Edit,Delete
            var permissionsBackend = mgr.CreatePermission(perms, ((ISecuredObject)newControl).Id, SecurityManager.ApplicationRoles["BackendUsers"].Id);
            var actionsBackend = new string[] SecurityConstants.Sets.Controls.Move,
                SecurityConstants.Sets.Controls.EditProperties,
                SecurityConstants.Sets.Controls.Delete;
            permissionsBackend.GrantActions(true, actionsBackend);
            ((ISecuredObject)newControl).Permissions.Add(permissionsBackend);
 
// Give Owner Change Permissions
            var permissionsOwner = mgr.CreatePermission(perms, ((ISecuredObject)newControl).Id, SecurityManager.ApplicationRoles["Owner"].Id);
            var actionsOwner = new string[] SecurityConstants.Sets.Controls.ChangePermissions ;
            permissionsOwner.GrantActions(true, actionsOwner);
            ((ISecuredObject)newControl).Permissions.Add(permissionsOwner);

Second -- the documentation on adding controls to a page should be updated.  This permission piece is crucial and will cause the page to not load.
Sitefinity 4 Developers Guide Adding and Removing Controls

Posted by Community Admin on 11-Aug-2011 00:00

I was able to get it working as I had discovered I needed to add permissions to the control...Which I think is kind of odd... as why do you need to add permissions on the control level when page permissions should do?

But anyhow the problem now is how do I get controls to display in the order I add them onto the page... I got two contentblocks and when I add them they don't display in the same order...

any ideas?

Posted by Community Admin on 12-Aug-2011 00:00

Hi Dan,

Thank you for getting back to me. Since our Sitefinity 4.1 official release, we changed the way frontend pages are rendered - we use ASP.NET rendering engine for all release after 4.1 The behavior that you are describing has been logged as a bug (occurring in a scenario where nested templates are used and the content from the parent template is loaded before the contents of the child template). I have just verified the bug status and it's marked as done. The fix is included in our Sitefinity 4.2 release, and upgrading a copy of your project should fix the problematic behavior.
 If any problems persist, or you need some further information, please do not hesitate to contact me again. I'll be glad to help you further.

Regards,
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

This thread is closed