none
CAML query returns no result

    Question

  • Hello,

    I have this peace of code in a WCF service, but it always return an empty result.
    I am sure that there are files in my list that should be found. There is one file with the title el-standard.

    public bool GetProjectKeys(string projectNumber)
    {
        bool hasKeys = false;
    
        try
        {
            string url = String.Empty;
    
            using (SPWeb current = SPContext.Current.Web)
            {
                url = String.Format(@"{0}/{1}/{2}/", current.Url, "projekt", projectNumber);
            }
    
            if (!SPSite.Exists(new Uri(url)))
            {
                Exception ex = new Exception(String.Format("Cant find url: {0}", url));
                LoggerUtility.WriteError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex, LoggerUtility.LogType.Error);
                return hasKeys;
            }
    
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite elevatedSite = new SPSite(url))
                {
                    using (SPWeb elevatedWeb = elevatedSite.OpenWeb())
                    {
                        SPList infoList = elevatedWeb.Lists.TryGetList("Handlingar");
    
                        if (infoList != null)
                        {
                            string caml = "<Where><BeginsWith><FieldRef Name=\"Title\" /><Value Type=\"Text\">el-</Value></BeginsWith></Where>";
                            SPQuery elQuery = new SPQuery();
                            elQuery.Query = caml;
    
                            SPListItemCollection listItems = infoList.GetItems(elQuery);
    
                            if (listItems.Count > 0)
                            {
                                hasKeys = true;
                            }
    
                            LoggerUtility.WriteInfo(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, "Count: " + listItems.Count.ToString() + " ItemCount: " + infoList.ItemCount.ToString());
                            // log result
                            // Count 0 <--- why?
                            // ItemCount 30                            
                        }
                    }
                }
            });
            return hasKeys;
        }
        catch (Exception ex)
        {
            LoggerUtility.WriteError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex, LoggerUtility.LogType.Error);
            return hasKeys;
        }
                
    }

    When checking the ULS logs I found this unexpected error:

    "Detected use of SPRequest for previously closed SPWeb object. "

    This is strange, because I have a similar code like the one above where I get some other information from another list. And that one is working without any problem.

    Grateful for all the answers so I can solve this.
    Thanks.



    • Edited by Phrone Tuesday, November 25, 2014 10:05 AM
    Tuesday, November 25, 2014 9:30 AM

Answers

All replies