Create a page in code when not logged in

Vote:
 
I want to create a page under the current page to record comments about the page, so I have a simple form that contains name,email and comments text. When the user clicks the submit button I just want to create a new page belwo the current one. The problem is that the user is not necessarily logged in, or may not have sufficient access rights. So I 'switch' user as shown in the FAQ 'How do I save a page as anonymous user?', but I get an error -"Access was denied to page 25" when I call the GetDefaultPageData method. Page 25 is the current page when the button is clicked. Below is the code. I'm running EPiServer 4.62B on Windows XP SP2. protected void btnSubmit_Click(object sender, EventArgs e) { // Save the current user details UnifiedPrincipal currentUser = UnifiedPrincipal.Current; // Get our 'Auto create' user details from config string userName = SiteSettings.GetSettingString("AutomaticCreatorUserName"); string password = SiteSettings.GetSettingString("AutomaticCreatorPassword"); // Obtain the Iprincipal IPrincipal principal = AuthenticationProvider.Authenticate(this, userName, password); // Set the CurrentUSer to this one PageBase.CurrentUser = principal as UnifiedPrincipal; // Load the required page type PageType pageType = PageType.Load(Names.PageTypeNewsComment); // Create a new page instance (following line fails with "Access was denied to page 25") PageData newPage = Global.EPDataFactory.GetDefaultPageData(CurrentPage.PageLink, pageType.ID); // Set details for the page newPage.PageName = "Comment created on " + DateTime.Now.ToString("R"); newPage.VisibleInMenu = false; ((PropertyString)newPage.Property["CommentName"]).Value = txtCommentName.Text; ((PropertyString)newPage.Property["CommentEmail"]).Value = txtCommentEmail.Text; ((PropertyLongString)newPage.Property["CommentText"]).Value = HttpUtility.HtmlEncode(txtCommentText.Text); // Now save the page Global.EPDataFactory.Save(newPage, EPiServer.DataAccess.SaveAction.Save); // And 'restore' previous user PageBase.CurrentUser = currentUser; } Any ideas? Regards, Mark
#13323
Jan 15, 2008 20:46
Vote:
 
Specifying accesslevel NoAccess as a parameter should do the trick: protected void btnSubmit_Click(object sender, EventArgs e) { // Create a new page instance (following line fails with "Access was denied to page 25") PageData newPage = Global.EPDataFactory.GetDefaultPageData(CurrentPage.PageLink, pageType.ID, EPiServer.Security.AccessControlList.NoAccess); // Set details for the page newPage.PageName = "Comment created on " + DateTime.Now.ToString("R"); newPage.VisibleInMenu = false; ((PropertyString)newPage.Property["CommentName"]).Value = txtCommentName.Text; ((PropertyString)newPage.Property["CommentEmail"]).Value = txtCommentEmail.Text; ((PropertyLongString)newPage.Property["CommentText"]).Value = HttpUtility.HtmlEncode(txtCommentText.Text); // Now save the page Global.EPDataFactory.Save(newPage, EPiServer.DataAccess.SaveAction.Save, EPiServer.Security.AccessControlList.NoAccess); }
#15595
Jan 15, 2008 21:30
Vote:
 
Thanks Mari, That worked perfectly.
#15596
Jan 16, 2008 10:33
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.