We recently setup some ribbon functionality to add custom popups into buttons in the Sitecore content editor ribbon. The code to launch the popup was via a custom command:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public override void Execute(CommandContext context) { Assert.IsTrue(context.Items.Length == 1, "Exactly one item should be selected"); ... CreateWindow(url.ToString(), "Network/32x32/earth_new.png", "Copy Language Content", "400px", "310px"); } private void CreateWindow(string uri, string icon, string header, string width, string height) { MethodInfo method = typeof(Windows).GetMethod("CreateWindow", BindingFlags.NonPublic | BindingFlags.Static); method.Invoke(null, new object[] { UIUtil.GetUri(uri), string.Empty, header, icon, width, height, true, true, true, true }); } |
In the popup window we wanted to be diligent and check that only certain users could see the form. This was done via:
1 2 3 4 5 6 7 8 9 |
protected void Page_Load(object sender, EventArgs e) { if (!(SitecoreContext.User.IsAdministrator || SitecoreContext.User.IsInRole("sitecore\RoleName"))) { ContentContainer.Visible = false; return; } } |
The problem we found was sporadically the ‘SitecoreContext.User.IsAdministrator’ check would fail and the popup would be blank.
After a lot of debugging we found the cookies in the HttpRequest object would sometimes be different hence causing the differences in the users attributes. When Sitecore invokes preview mode, it sets the user to be ‘sitecore\anonymous’ via ‘PreviewManager.StoreShellUser(true);’. This is what was catching us out. If the user had been using the cms and then visited preview the popup window would inherit the anonymous user.
Once we found the solution it was a very easy change to integrate (the WebEdit command -> Run(ClientPipelineArgs args) gave us the answer).
During our command before we open the popup we simply needed to add:
1 |
PreviewManager.RestoreUser(); |