I am trying to make a sandboxed solution that adds a couple of itemeventreceivers to a list, that add the items to the choices of a choicefield. The result would be something like a lookup field, without the referential integrity.
The code below works perfectly in a regular solution - the choice gets added to the field as expected, but in a sandboxed solution I get no results. The event receiver does fire, no exceptions occur and and all code is reached when stepping through, but the item just isn't added. I think I'm doing nothing here that is impossible in a sandboxed solution. Is there something I need to do differently because this code runs sandboxed?
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
string itemTitle = properties.AfterProperties["Title"].ToString();
SPField _field = properties.Web.Fields["Klant"];
if (_field == null)
{
properties.Cancel = true;
return;
}
SPFieldChoice KlantField = (SPFieldChoice)_field;
foreach (string choice in KlantField.Choices)
{
if (itemTitle.ToLower() == choice.ToLower())
{
properties.Cancel = true;
return;
}
}
KlantField.Choices.Add(itemTitle);
KlantField.Update(true);
properties.Web.Update();
}
Background: I am running VS2010 to debug, I have a Sharepoint Foundation Server to test on, and I also tried the sandboxed solution on Office365, the environment where we want to use this solution. The sandboxed and non-sandboxed solutions I'm talking about are separate solutions, and not changed afterwards. They are identical except for the description and GUIDs and that one was made as a sandboxed solution and the other as a farm solution one.