0

I'm having some troubles with filling a TagTreelist field programmatically. I simply tried the same as for treelist fields as i know it works:

string tagID = Search.GetYearTagItem(year, "/sitecore/content/ORV/Websites/Global/Sanofi COM/Data/Tags", "Feature/Experience Accelerator/Taxonomy/Datasource/Tag").ToString(); // returns me the item ID to insert in the treelist 
Sitecore.Diagnostics.Log.Error("DebugA:"+tagID, this); // this returns me the right ID.
item.Fields["Tags"].Value = tagID;// string : ids separated by pipes

But it seems to work differently with TagTreelist because i got the following error :

Object reference not set to an instance of an object

Does anyone know what type my object is supposed to receive ?

Thank you for your help

EDIT 1: i confirm you if i remove this line :

item.Fields["Tags"].Value = // string : ids separated by pipes

the error disappears. The problem definitely comes from the line i guess.

  • Are you sure the type of information is the issue? Your error message suggest a null reference.. can you debug and verify that you don't have that issue? – Gatogordo Nov 21 '18 at 17:06
  • Yes, that's what i did and i'm 100% sure the variable i use contains the correct id. However i will keep investigating in case people confirm me it should work. – Slrg Nov 21 '18 at 17:13
  • Can you try 'item["Main Tags"] = // string' without '.Fields'? – Marek Musielak Nov 21 '18 at 18:38
  • 1
    @PeterProchazka i've added some more code and explanations. In case it helps... – Slrg Nov 21 '18 at 20:21
  • 1
    if item.Fields["Field Name"].Value throws exception and item["Field Name"] does not throw the exception, it means that the item doesn't have a field called Field Name. Check if you're editing the correct item and if the field name is correct for sure – Marek Musielak Nov 22 '18 at 7:39
2

If

item.Fields["Field Name"].Value = "some string";

throws null reference exception and

item["Field Name"] = "some string";

does not throw the exception, it means that the item doesn't have a field called Field Name.

Check if you're editing the correct item and if the field name is correct. Most probably the real field name is different (custom fields) or it should be __Field Name (for standard Sitecore built-in fields).

|improve this answer|||||
  • Hello @Marek Musielak i tried this solution however, the tree still appears empty in the sitecore. Any idea what i might be missing? – AmiraElEwady Feb 24 '19 at 11:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.