Updating field works but Projection Query results are not as expected

Topics: Customizing Orchard, Troubleshooting, Writing modules
Oct 19, 2013 at 12:53 AM
Edited Oct 19, 2013 at 12:54 AM
I'm programmatically updating a Boolean field on a content item. The value of the Data field in the ContentItemVersionRecord table is updated correctly, which is great! However, I have a Query which filters on this Boolean field, and the associated content item is still included in the query's results, even though it should not be. Indeed, when previewing the query (in the dashboard), I can see that the field is False, indicating that the content item has been updated. Yet the query itself should exclude this result because the filter is configured to only include items where this field is True.

This is how I update the field value:
var contentItem = _orchardServices.ContentManager.Get(myContentItemId);
var myPart = contentItem.Parts.First(p => p.PartDefinition.Name == "MyContentType");
var availableField = myPart.Fields.First(f => f.Name == "Available");
((dynamic)availableField).Value = false;    
_orchardServices.ContentManager.Publish(contentItem); // this line does not appear to be necessary to update the database
My Query is configured to only include results where the "Available" field is True.

Now, when I use the built-in content editor to edit the content item and set the Available field to False, the item does not come back in the query result, which is correct behavior.

But when I use the code snippet above to update the Available field to False, the item comes back in the query result, which is incorrect. And yet the item in the results does indeed have Available set to False.

So for some reason the result is not being excluded from the Query when I use the programmatic approach above. Surely I'm missing something simple. Help!
Oct 21, 2013 at 7:29 PM
Verifying, this is still an issue. No new version record is created, although the latest version record is updated. Thus the projection query returns the wrong results, as if they are cached.
Oct 21, 2013 at 9:05 PM
I have it working now, although this may not be correct usage. The fix: Unpublish the content item, change the value of the field, and the Publish the content item again.
var contentItem = _orchardServices.ContentManager.Get(myContentItemId);

var myPart = contentItem.Parts.Single(p => p.PartDefinition.Name == "MyContentType");
var availableField = myPart.Fields.Single(f => f.Name == string.Concat(stock, "Available"));
((dynamic)availableField).Value = available;

Marked as answer by ayitey on 10/21/2013 at 2:05 PM