Hi.
I am having trouble with setting a category that is created to a dynamic property.
When ever a specific page type is created, a new category should be created at the same time. I also have a dynamic property called "NewsCategory" and the new category that is created should be set to the "NewsCategory" property for the new page that is created.vIs this possible at all?
Here is my code. What is wrong if it is possible to do so?
public static void CreateCategory(EPiServer.Core.PageData pageData)
{
string pagename = pageData.PageName;
Category findCat = Category.Find(pagename);
if ((findCat == null) || (findCat.Name != pagename))
{
Category root = Category.GetRoot();
Category newCat = new Category(pagename, pagename);
root.Categories.Add(newCat);
newCat.Save();
Category createdCat = Category.Find(pagename);
DataFactory.DynPropTree.SetDynamicProperties(pageData);
string guid = createdCat.GUID.ToString();
pageData.Property["NewsCategory"].Value = guid;
EPiServer.Global.EPDataFactory.Save(pageData, EPiServer.DataAccess.SaveAction.Publish);
}
}
I have also tried this code, but the same problem occurs
public static void CreateCategory(EPiServer.Core.PageData pageData)
{
string pagename = pageData.PageName;
Category findCat = Category.Find(pagename);
if ((findCat == null) || (findCat.Name != pagename))
{
Category root = Category.GetRoot();
Category newCat = new Category(pagename, pagename);
root.Categories.Add(newCat);
newCat.Save();
Category createdCat = Category.Find(pagename);
DynamicPropertyCollection dynprops = DynamicProperty.ListForPage(pageData.PageLink);
DynamicProperty dynprop = dynprops["NewsCategory"];
dynprop.PropertyValue.Value = createdCat.GUID;
DynamicProperty.SaveCollection(pageData.PageLink, dynprops);
}
}
BR,
Tore
Hi Tore!
Could you please give a little information on what the actual problem is?
Compile time/runtime, errormessages, crashes or logical errors?
Thanks,
Johan Olofsson
Hi Johan.
Thank you for your answer!
I have tried to set the property value to the newly created Category and I get a NullReferenceException. I have also tried to add the newly created Category to a CategoryList, and then set the property value to the CategoryList, but I also get a NullReferenceException when I do this.
Any ideas what value I can use?
BR,
Tore
Hi Johan.
When I try to set the value of the property to the created category, it throws a NullRef-exception.
Is the dynamic property read-only when the property is set by code?
BR,
Tore
hi Tore!
I hacked up a little sample that sets a dynamicproperty of type Category
through code:
Category cat = Category.Find(CurrentPage.PageName);
if (null == cat)
{
cat =new Category(CurrentPage.PageName,CurrentPage.PageName);
Category.GetRoot().Categories.Add(cat);
cat.Save();
cat = Category.Find(CurrentPage.PageName);
}
DynamicProperty dynProp = DynamicProperty.Load(CurrentPage.PageLink, "NewsCategory");
PropertyCategory propCat = (PropertyCategory)dynProp.PropertyValue;
propCat.Category = new CategoryList(new int []{cat.ID});
dynProp.Save();
Hope this helps.
Regards,
Johan Olofsson
Hi Johan.
Thank you very much for your help. It works now.
I think the problem was that I tried to set the property on the CreatingPage EventHandler and not the CreatedPage EventHandler. The dynamic property for the page could not be set because the page was not saved yet. My bad!:)
BR,
Tore