A very common error message that appears when developing in the SharePoint platform is “Value does not all within the expected range” when trying to update an SPListItem that is the result of a query to a SharePoint list. Let’s look at the following example:

class SearchListItem
{
    /// <summary>
    /// Search document by title
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    private static SPListItem SearchDocument(SPList list)
    {
        //search document by title
        StringBuilder query = new StringBuilder();
        query.Append("<Where>");
        query.Append("<Eq>");
        query.Append("<FieldRef Name='Title' />");
        query.Append("<Value Type='Text'>My document</Value>");
        query.Append("</Eq>");
        query.Append("</Where>");

        SPView queryView = list.Views["All Documents"];
        SPQuery spQuery = new SPQuery(queryView);
        spQuery.Query = query.ToString();
        spQuery.ViewAttributes = "Scope=\"RecursiveAll\"";

        SPListItemCollection items = list.GetItems(spQuery);
        if (items != null && items.Count > 0)
            return items[0];

        return null;
    }

    /// <summary>
    /// Update document.
    /// </summary>
    /// <param name="list"></param>
    private static void UpdateDocumentError(SPList list)
    {
        //search document by title
        SPListItem doc = SearchDocument(list);
        //update item in list
        doc["Title"] = "My document modified";
        doc.Update(); //error "Value does not fall 
        //within the expected range" is thrown
    }

    /// <summary>
    /// Update document.
    /// </summary>
    /// <param name="list"></param>
    private static void UpdateDocumentSucess(SPList list)
    {
        //search document by title
        SPListItem doc = SearchDocument(list);
        //get SPListItem directly from list
        SPListItem item = doc.ParentList.GetItemById(doc.ID);
        //update item in list
        item["Title"] = "My document modified";
        item.Update(); //item updated sucessfully
    }
}

In the previous example, the following actions are being performed:

  • A search is performed within a list for a document named “My Document”. As a result of that search, a SPListItem instance is returned containing the document information;
  • In the “UpdateDocumentError” method, after the search is performed, the returned item is modified and the list is updated. This action will cause the exception “Value does not fall within the expected range” to be raised;
  • In the “UpdateDocumentSucess” method, after the search is performed, we obtain the item directly from the list using the ID of the SPListItem returned from the search (the line “SPListItem item = doc.ParentList.GetItemById(doc.ID);” was added, returning a new SPListItem instance). The new instance is then modified and used to update the item in the list. This way, the item is sucessfully updated.

Related Articles

To learn why your business should migrate to SharePoint Online and Office 365, click here and here.

If you want to convert your tenant’s root classic site into a modern SharePoint site, click here.

If you are a SharePoint administrator or a SharePoint developer who wants to learn more about how to install a SharePoint farm in an automated way using PowerShell, I invite you to click here and here. The articles use AutoSPInstaller with a SharePoint 2016 farm but AutoSPInstaller support for SharePoint 2019 was already announced!

If you want to learn how to upgrade a SharePoint 2013 farm to SharePoint 2019, click here and here.

If you want to learn all the steps and precautions necessary to successfully keep your SharePoint farm updated and be ready to start your move to the cloud, click here.

If you learn how to greatly speed up your SharePoint farm update process to ensure your SharePoint farm keeps updated and you stay one step closer to start your move to the cloud, click here.

If you want to learn how to upgrade a SharePoint 2010 farm to SharePoint 2016, click here and here.

If you are new to SharePoint and Office 365 and want to learn all about it, take a look at these learning resources.

If you are work in a large organization who is using Office 365 or thinking to move to Office 365 and is considering between a single or multiple Office 365 tenants, I invite you to read this article.

If you or your customers are not ready to move entirely to the Cloud and Office 365, a hybrid scenario could be an interesting scenario and SharePoint 2019 RTM was recently announced with improved hybrid support! To learn all about SharePoint 2019 and all its features, click here.

If you want to know all about the latest SharePoint and Office 365 announcements from SharePoint Conference 2019, click here and here.

Happy SharePointing!

LEAVE A REPLY

Please enter your comment!
Please enter your name here