1
votes

my question is basically the same as @Bob Black's Cannot access sitecore item field via API but I agree with @techphoria414 that the accepted solution is not necessary and in my case does not work.

In my own words, I have a template Departure that I have been using for about a year now creating and updating items programmatically. I have added a new field Ship to the template. When I create a new item the field comes up as null when I try to access it using departure.Fields["Ship"]. If I step over the line causing the exception then after calling departure.Editing.EndEdit() I can then see the Ship field if I call departure.Fields.ToList(). If I add the template to a content item via the Sitecore GUI I can see the field and use it, and if I look at a content item which is based on the template I can see the new field too. So it is only when I access the template/item programmatically that it is null.

I have sitecore running on my local machine with a local sqlserver, and publish to my local machine.

Here is my code

String ship = "MSDisaster";

foreach (Language language in SiteLanguages)
{
    departure = departure.Database.GetItem(departure.ID, language);
    departure.Editing.BeginEdit();

    try
    {
        departure.Fields["StartDate"].Value = GetSitecoreDateString(xDep, "StartDate");
        departure.Fields["EndDate"].Value = GetSitecoreDateString(xDep, "EndDate");
        departure.Fields["Guaranteed"].Value = xDep.SelectSingleNode("./Guaranteed").InnerText;
        departure.Fields["Status"].Value = xDep.SelectSingleNode("./Status").InnerText;
        departure.Fields["Currency"].Value = ConvertLanguageToCurrency(language);
        departure.Fields["Market"].Value = ConvertLanguageToMarket(language);
        departure.Fields["TwinSharePrice"].Value = GetPrice(xDep, "twn", language);
        departure.Fields["SinglePrice"].Value = GetPrice(xDep, "sgl", language);

        if (!String.IsNullOrEmpty(ship))
            departures.Fields["Ship"].Value = ship;
    }
    catch (Exception ex)
    {
        departure.Editing.CancelEdit();
        log.Error(ex);
        throw ex;
    }
    departure.Editing.EndEdit();
}

So, how do I get the field be picked up?

Thanks,

James.

2

2 Answers

2
votes

Firstly do you see the field in the web database in the sitecore administration. If you do the item has the fields, you then should check the template assigned on the item and double check that the field is actually called "ship" and check the case as ive seen this as an issue before.

Also check the security on the item and field just in case anyone changed anything. Next try and get the data from the item but instead of using the field name, use the field ID.

Let me know how you go?

Chris

0
votes

Sorry Chris, StackOverflow, and the others who looked at my questions. It was a stupid typo. It's even there in my question

    departure.Fields["SinglePrice"].Value = GetPrice(xDep, "sgl", language);

    if (!String.IsNullOrEmpty(ship))
       departures.Fields["Ship"].Value = ship;
}

departure is the item I am working on, departures is the collection it belongs to... doh.

So what is the protocol here? Do I delete my question now because it isn't really going to help anyone code better?

James.