views:

287

answers:

3

I have a sharepoint list which has several fields. It seems that when a field is left blank on one of the records - that attribute is missing on the field when I query the list using a CAML Query.

Is it possible to write a query to return the records which do not contain this attribue?

Example:

id Employee Title description
-------------------------
1  Jeff  Person1 
2  Bob  Person2
3  Charles Person3
4    Person4
5    Person5

Is there any way to query this to only return records with id 4 and 5 because they have left the name field blank?

I tried the following:

System.Text.StringBuilder xmlQuery = new StringBuilder();
xmlQuery.Append("<Query>");        
xmlQuery.Append("   <Where>");        
xmlQuery.Append("       <IsNull>");            
xmlQuery.Append("           <FieldRef Name=\"Employee Title\" />");           
xmlQuery.Append("       </IsNull>");        xmlQuery.Append("   </Where>");        
xmlQuery.Append("</Query>");        XmlNode query = new XmlDocument();          
query.InnerXml = xmlQuery.ToString();

But of course the attribute doesn't exist in those records, so nothing is returned

Thanks in advance!

A: 

There should not be any issue if a field value is missing one or more fields, it is just normal and should work. But one change you will have to do in the query is to remove the outer query tag please use the below query and you should be good to go.

<Where>       
<IsNull>           
<FieldRef Name="Name" />       
</IsNull>   
</Where>
Kusek
Thanks for affirming this is possible! I found my mistake, I guess I should have posted my actual code rather than a sample. The sample worked as my code did not. If I could vote, I would. Thanks!
Jeff
A: 

This is a longshot, but is your "Name" column actually named "Title" within the list?

FieldRefs expect the internal name of the field AFAIK

zincorp
A: 

My mistake was present in my code but not my example. I did not realize that spaces had to be replaced with _x0020_ .

Actual snippet:

        System.Text.StringBuilder xmlQuery = new StringBuilder();
        xmlQuery.Append("<Query>");
        xmlQuery.Append("   <Where>");
        xmlQuery.Append("       <IsNull>");
        xmlQuery.Append("           <FieldRef Name=\"Employee_x0020_Title\" />");
        xmlQuery.Append("       </IsNull>");
        xmlQuery.Append("   </Where>");
        xmlQuery.Append("</Query>");
        XmlNode query = new XmlDocument();
        query.InnerXml = xmlQuery.ToString();

Thanks again!

Jeff
You could have edited you Question.
Kusek