29 October 2011

value does not fall within the expected range

Did you have the following situation? Access to the sharepoint list. Operations in any action, then try to contact list again, but this time, and receive the following message

value does not fall within the expected range.


What to do?

You need to refrash the web and the list!

Use this code:

/// <summary>
/// Refresh a List
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static SPList RefreshList(SPList list)
{
     return RefreshWeb(list.ParentWeb).Lists[list.ID];
}

/// <summary>
/// Return the web
/// </summary>
/// <param name="web"></param>
/// <returns></returns>
public static SPWeb RefreshWeb(SPWeb web)
{
     web = new SPSite(web.Site.ID).OpenWeb(web.ID);
     web.AllowUnsafeUpdates = true;
     web.Lists.IncludeRootFolder = true;
     return web;
}

Here is an example of using the code - I had a problem in this case without using Refresh List.

using (SPSite site = new SPSite(siteUrl))
{
  using (SPWeb web = site.OpenWeb(webId))
  {
    // Get a list.

    SPList list = web.Lists[listId];
    SPView view = list.Views[viewId];
    SPQuery query = new SPQuery(view);
    String FieldsXml =  "<FieldRef Name=… />";
    query.ViewFields = FieldsXml;
    SPListItemCollection items = list.GetItems(query);
    foreach (SPListItem item in items)
    {    
       
list = Lists.RefreshList(list);
        SPListItem item1 = list.GetItemById(item.ID);
        foreach (SPListItemVersion itemVer in item1.Versions)
        { 

           
        }

    }
  
 }// dispose web 
}// dispose site


Yours,
Roi

No comments:

Post a Comment