user3308656
user3308656

Reputation: 63

set value in the droplink field

How do I assign values in a droplink through coding?

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item PriceBookHome = master.GetItem("/sitecore/content/Administration/Price Books/Clarisonic-us-retail");
string currency = "INR";
string currenySource = PriceBookHome.Fields["Currency"].Source;
Sitecore.Data.Items.Item currenyDictSource = master.GetItem(currenySource);

foreach (Item im in currenyDictSource.GetChildren())
{

    if (im.Fields["Key"].Value == currency)
    {

        PriceBookHome.Editing.BeginEdit();
        PriceBookHome.Fields["Currency"].SetValue(im.DisplayName, true);
        PriceBookHome.Editing.EndEdit();
    }

}

I am getting the following error on the droplink after insertion: "This field contains a value that is not in the selection list"

Error I am getting as follows:

enter image description here

droplink source[Currency path as given source in droplink] enter image description here

Upvotes: 0

Views: 3691

Answers (2)

Trayek
Trayek

Reputation: 4410

Most likely it's because you're storing a string (the displayname) in the field.
As Ruud says, you can put the ID in, or do something like this:

PriceBookHome.Editing.BeginEdit();
var field = (Sitecore.Data.Fields.ReferenceField)PriceBookHome.Fields["Currency"];
field.TargetItem = im;
PriceBookHome.Editing.EndEdit();

Upvotes: 0

Ruud van Falier
Ruud van Falier

Reputation: 8877

You need to set the ID of the currency item as value, not the name.

PriceBookHome.Fields["Currency"] = im.ID.ToString();

Upvotes: 4

Related Questions