November Happy Hour will be moved to Thursday December 5th.

Set custom property when creating page

Vote:
 

I have a custom property (dropdown list) that I want to set automaically when a page is created. The custom property have to be editable when the page is created.

I have tried to create a  DataFactory.Instance.CreatedPage += new PageEventHandler(Instance_CreatingPage); in global where I set my custom property and save. DataFactory.Instance.Save(clone, EPiServer.DataAccess.SaveAction.ForceCurrentVersion); but the property is not set after the page is published. Ideas anyone?

 

#44046
Sep 29, 2010 14:19
Vote:
 

Hi

I think CreatedPage is raised after the page has been saved, so any changes you make to the PageData object won't be persisted. Try the CreatingPage event instead, it's raised during the save process. Or perhaps LoadingDefaultPageData or LoadedDefaultPageData would be even better, if you want to set a default value.

Regards

Per Gunsarfs
EPiServer Development Team

#44047
Sep 29, 2010 15:13
Vote:
 
#44048
Sep 29, 2010 15:14
Vote:
 

void Instance_CreatingPage(object sender, EPiServer.PageEventArgs e)
        {
            if (e.Page.PageTypeID == PropertyHelper.GetPropertyFromSettingsPage<int>("InfoPagePageType"))
            {
                if (!e.Page.IsValue("PageResponsible"))
                {
                    string user = PrincipalInfo.CurrentPrincipal.Identity.Name;
                    MembershipUserCollection users = Membership.GetAllUsers();
                    foreach (MembershipUser u in users)
                    {
                        if (user == u.UserName)
                        {
                            PageData clone = e.Page.CreateWritableClone();
                            clone.Property["PageResponsible"].Value = user;
                            DataFactory.Instance.Save(clone, EPiServer.DataAccess.SaveAction.Publish);
                        }
                    }
                }
            }
        }Test

Tested the creatingpage event, no success. The loadingdefaultdata and loadingdefaultdata is raised before the page is created? If I use them I cant check which pagetype the page is, because is doesnt exist.

void Instance_CreatingPage(object sender, EPiServer.PageEventArgs e)      

 {
            if (e.Page.PageTypeID == PropertyHelper.GetPropertyFromSettingsPage<int>("InfoPageType"))          

 {              

  if (!e.Page.IsValue("PageResponsible"))              

  {                  

  string user = PrincipalInfo.CurrentPrincipal.Identity.Name;
                 MembershipUserCollection users = Membership.GetAllUsers();                  

 foreach (MembershipUser u in users)                  

 {                      

 if (user == u.UserName)                      

  {
                            PageData clone = e.Page.CreateWritableClone();                          

  clone.Property["PageResponsible"].Value = user;                          

 DataFactory.Instance.Save(clone, EPiServer.DataAccess.SaveAction.Publish);   }   }   }   }  }

#44051
Sep 29, 2010 16:19
Vote:
 

As Per says, LoadedDefaultPageData should do the trick, but not sure if they've changed the way it works.

http://antecknat.se/blog/2008/06/09/prepopulate-fields-when-creating-page/

 

#44059
Edited, Sep 29, 2010 17:16
Vote:
 

CreatingPage worked, but without the CreateWritableClone.

#44070
Sep 30, 2010 8:20
* 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.