Sunday 17 August 2008

Adding Property to SPNavigationNode

[TR]
Birkaç gün önce yöneticim Sharepoint için 4. kırılıma kadar destekleyen ve node'larında extra değerler tutabileceğim bir menu manager yazmamı istedi. Bunu sharepoint standardında yapmam gerekiyordu. İlk önce SPNavigationNode sınıfını extend etmeyi düşündüm ancak bunun sealed bir class olduğunu farkettim. Daha sonra extra değerleri property olarak tutmaya karar verdim. Ancak bir sorun vardı. Bir türlü property ekleyemiyordum.
En sonunda node'a property ekleyebilmek için önce node'u oluşturup, siteye kaydedip, daha sonra geri çağırıp, property'i ekleyip, node'u update etmem gerektiğini öğrendim. Örnek kodu aşağıda veriyorum

[ENG]
My manager asked me to write a component to sharepoint which makes it possible to add 3th & 4th level link node to sharepoint navigation. It was like adding a new heading under a heading and then adding link to the second heading. That was easy. But the main problem was I needed to add some new properties to that link (like "is default page" value). I tried to extend SPNavigationNode class but I realised that it is a sealed class and sharepoint keeps each property as a different column in DB. Then I realed that adding my new values as a property vould solve my problem. But the problem was when I added a property I got compile time error.
Finally I learned that to add a property to a SPNavigationNode we must first create the object, add it to navigation, call the node back, add properties, update node.
Here is a sample code;

SPNavigationNodeCollection nodes = PublishingWeb.GetPublishingWeb(spweb);
SPNavigationNode newNode = new SPNavigationNode("DeveloperKoala",
"http://developerkoala.blogspot.com");
nodes.AddAsLast(newNode);
node = nodes[nodes.Count() -1];
node.Properties.Add("ExtendedProperty", "This is a cool site");
node.Update();

No comments: