This project has moved. For the latest updates, please go here.

Camlex not working in WebMethod asp.net 3.5 Sharepoint 2010

Oct 13, 2016 at 3:45 PM
Here is my web method that I call with help of Ajax JQuery. When sending a request I receive 500 error.
Commenting string
string caml =Query().Where(x => (string)x["Title"] != "").ToString();
and Web method works get a 200 response.
In the console application it works but in web method CAMLEX does not work. Please tell me why?
[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static int CountPromotions(string listName, string configTags)
        {
            using (SPWeb web = new SPSite(SPContext.Current.Web.Url).OpenWeb())
            {
                SPList list = web.Lists[listName];
                var query = new SPQuery();
                string caml =
                    Camlex.Query()
                        .Where(x => (string)x["Title"] != "").ToString();

                query.Query = caml;
                SPListItemCollection listcol = list.GetItems(query);

                return listcol.Count;
            }

        }
Coordinator
Oct 14, 2016 at 8:15 AM
hello enbeznos,
could you add try/catch around Camlex call and log exact exception message and stack trace e.g. to text file:
string caml = string.Empty;
try
{
    caml = Camlex.Query().Where(x => (string)x["Title"] != "").ToString()
}
catch (Exception x)
{
    log("Error: {0}\n{1}", x.Message, x.StackTrace);
}

private static void log(string msg, params object[] args)
{
    try
    {
        msg = string.Format(msg, args);
        msg = string.Format("{0} (thread id: {1})\t{2}\n", DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"), Thread.CurrentThread.ManagedThreadId, msg);
        File.AppendAllText("c://temp/log.txt", msg);
    }
    catch
    {
    }
}
It may be related with app pool rights because if I remember correctly for working with reflection full trust assembly is needed (it is considered as full trust when you install it to the GAC for example). Also if you need to get items with empty title instead of x["Title"] != "" you may need to use comparison with null x["Title"] != null. In 1st case it will produce the following CAML:
<Where>
  <Neq>
    <FieldRef Name="Title" />
    <Value Type="Text"></Value>
  </Neq>
</Where>
while in second another one:
<Where>
  <IsNotNull>
    <FieldRef Name="Title" />
  </IsNotNull>
</Where>