3

In an SPQuery, which fields are required to update a list item?

SPQuery query = new SPQuery();
query.Query = @"
    <OrderBy>
        <FieldRef Name='ID' Ascending='true' />
    </OrderBy>";
query.ViewFields = string.Concat(
    "<FieldRef Name='ID' />",
    "<FieldRef Name='Title' />");
query.ViewFieldsOnly = true;
query.RowLimit = 2000; // Effective batch size.
query.QueryThrottleMode = SPQueryThrottleOption.Override;

If I run listItem.Update() without changing any fields, the update fails with the error 'value does not fall within the expected range'. But if I change ViewFieldsOnly to false, the update succeeds. My question is which fields are required to update a list item successfully (excluding any fields that may have been changed)?

1
  • How are you getting the specific list item?
    – BlueBird
    Commented Dec 19, 2013 at 21:31

2 Answers 2

4

AFAIK it's not documented anywhere which fields should be included to be able to do an Update after querying with ViewFieldOnly set to true, but according to SPQuery.ViewFieldsOnly and Updating an SPListItem it's amongst other ID, UniqueID, object type, ...

BUT my recommendation is definitely don't even try, just set ViewFieldsOnly to false, then you're sure you get what's needed even in future versions of SharePoint.

1

I have also had this issue, and have managed to get round it. I included the ListItem ID in the ViewFields then have created a new SPListItem variable using List.GetItemByID. You can then do a normal List Item update using this new variable.

        SPQuery q = new SPQuery();
        q.Query = "Query here";
        q.ViewFields = "...Fields Here...<FieldRef Name='ID' />";
        q.ViewFieldsOnly = true;

        foreach (SPListItem item in list.GetItems(q))
        {
            SPListItem i = list.GetItemById(int.Parse(item["ID"].ToString()));
            //code to update item here
            i.Update();
        }

It may not be the best solution ever but it got me up and running.

1
  • Sorry, but this makes me facepalm. SPList.GetItemById() creates an SPQuery object with all viewfields and rowlimit 1 and queries a list.
    – Rae23
    Commented Aug 25, 2014 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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