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

Setting Category to a dynamic property

Vote:
 
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
#12944
Feb 20, 2007 11:19
Vote:
 
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
#15129
Feb 21, 2007 15:53
Vote:
 
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
#15130
Feb 22, 2007 13:37
Vote:
 
What line throws the NullRef-exception? /johan
#15131
Feb 23, 2007 14:46
Vote:
 
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
#15132
Feb 23, 2007 15:07
Vote:
 
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
#15133
Feb 23, 2007 16:24
Vote:
 
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
#15134
Feb 26, 2007 13:47
Vote:
 
Glad to hear it worked out :) /johan
#15135
Feb 26, 2007 15:47
* 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.