none
CAML query and CDATA bug?

    Question

  • Hi masters,

    I have a problem with CAML query when I use CDATA blocks in <Where> clause. For example:

    1. <Where><Eq><FieldRef Name='...' /><Value Type='Text'>something]</Value></Eq></Where>
    - works ok

    2. <Where><Eq><FieldRef Name='...' /><Value Type='Text'>something]more</Value></Eq></Where>
    - works ok

    3. <Where><Eq><FieldRef Name='...' /><Value Type='Text'><![CDATA[something]]]></Value></Eq></Where>
    - doesn't work!!!

    4. <Where><Eq><FieldRef Name='...' /><Value Type='Text'><![CDATA[something]more]]></Value></Eq></Where>
    - works ok

    It looks like a content of the CDATA block cannot end with a right square bracket ']'. Otherwise SPQuery does not return the item in a result set.
    Can someone confirm this?

    Thanks
    Tuesday, December 16, 2008 12:38 PM

All replies

  • You don't actually need to use a CDATA construct, just do this:

    <Value Type='Text'>something</Value>

    If this is not possible, then perhaps explain why you need the CDATA constrct in there.

    Stu
    Starznet SharePoint Blog : http://www.starznet.co.uk/sharepoint/blog
    Please mark replies as answered if you find it has helped you
    Contact : http://www.starznet.co.uk/contactstarznet.htm
    Tuesday, December 16, 2008 12:46 PM
  • Stu,
     
    How can I search with CAML for items which may contain these characters !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~€ ? I believe CDATA is a best approach, otherwise you need to encode the text to distinct it from CAML itself. Can you still answer my question in the first post?

    Thanks,
    Sergio
    Wednesday, December 17, 2008 8:53 AM
  • Hiya,

    How about something like this?

    <Where> 
      <Contains> 
        <FieldRef Name='Title'>  
        <Value Type='Text'>Something&#93;</Value> 
      </Contains> 
    </Where> 

    You can find the Unicode references for all the special characters here:
    http://www.alanwood.net/demos/ansi.html

    Mike

    Wednesday, December 17, 2008 10:07 AM
  •  Mike,

    Yes,  I use this method to encode value before inserting into CAML query:
            string EncodeForCAML(string s)  
            {  
                //return "<![CDATA[" + s.Replace("]]>", "]]]]><![CDATA[>") + "]]>";  
     
                StringBuilder sb = new StringBuilder();  
                for (int i = 0; i < s.Length; ++i)  
                {  
                    char ch = s[i];  
                    if (char.IsLetterOrDigit(ch))  
                        sb.Append(ch);  
                    else 
                        sb.AppendFormat("&#{0};", Convert.ToInt32(ch));  
                }  
                return sb.ToString();  
            }  
     

    But my question in the first post was: why CDATA construction is not working for texts, which end with ']' ???  Is it a bug in CAML parser or something wrong at my sole site?

    Can anybody confirm that issue (look at the first post)?

    Sergio
    Thursday, December 18, 2008 10:00 AM
  • It seems, the SharePoint CAML parser is confused in parsing the text. Take the below scenario.
    <![CDATA[ something] ]] >

    When it reads the data, it assumes that the data is ending with the text "something" because when it finds the ']]' characters then it thinks end of text reached and querying it. But, when some character other than ']' after the first occurrence of the ']' then it thinks that the data is still there and proceeds until it finds ']]'. So, this is the reason it works for the cases 1,2,4 and 3rd failed. If data contains any special characters then yes, we need to use the CDATA except the case you have mentioned.

    Actually, I have written an article CAML Query few days back, and after read your question, seems it won't work for this scenario.
    I think, the only way is through coding.

    thanks

    ASP.NET and SharePoint developer
    Company: http://www.rampgroup.com/
    Blog: http://praveenbattula.blogspot.com
    Monday, February 08, 2010 12:12 PM