Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to retrieve a list of items where a specific field matches a specific value. There is likely going to be multiple items returned when the query is successful.

Here's what my CAML query looks like:

//init the query object             
itemQuery = new SP.CamlQuery();

//build the query string
//Equiv to SELECT * FROM list WHERE CollectiveNum = strCollectiveNum (parameter)
itemQueryString = '<Where>';
itemQueryString +='<Eq><FieldRef Name=\'Collective #\'/><Value Type=\'Lookup\'>' + strCollectiveNum + '</Value></Eq>';
itemQueryString +='</Where>';

itemQuery.getitems(itemQuery);

No matter what this query returns all items! I'm confused. It's really unclear to me why this would be so.

Next, If I follow the example in the section "Retrieving Items from a List Using Javascript" on this page by adding

<View><Query>

and

</Query></View>

around my query string it throws an alert with a message that says "Request failed. One or more field types are not installed properly. Go to the list settings to delete these fields. undefined"

Where am I going wrong? I'd be glad to post any other pieces of my script if needed.

share|improve this question

2 Answers 2

up vote 7 down vote accepted

CAML uses the internal name of the column in question. Spaces and symbols are "encoded".

Space: _x0020_

Hash: _x0023_

<Where>
    <Eq>
        <FieldRef Name='Collective_x0020__x0023_' LookupId='TRUE' />
        <Value Type='Lookup'>1</Value>
    </Eq>
</Where>
share|improve this answer
1  
This, although I tend to take things a step further and interrogate the list via Powershell to determine what the internalname of the field is. You never know when a user might have originally created a field under the name you're using, then deleted it, then re-added it, for instance. –  NotVonKaiser Jun 19 '13 at 15:39
    
Actually, the site was made by myself - an amateur, who only learned about the naming/renaming issue after the site went into use :( It's not realistic for me to recreate the content with proper names. Thanks for this help. It seems like a weird convention to return all items when a query isn't proper.. The end result in my test server is that all items were deleted when I expected just particular items tagged with that value to be deleted. Changing the field name worked just fine and I'm seeing the results I've expected. Thanks! –  adnauseam Jun 19 '13 at 15:50
    
We were all amateurs. When I create a list via the GUI I like to first set my field names as a single string with no special characters. That sets the Internal Name, then I go back and change the field title so that it looks ok in the GUI. –  Robert Kaucher Jun 19 '13 at 16:10
    
Another way to find the internal name of a field is in IE go to the list/library settings page, under columns right click the field and select 'properties'. At the end of the Address (URL) you will see Field=InternalName where InternalName is the internal name of the field you are looking at. –  Robert Kaucher Jun 19 '13 at 16:13

When all items return it is almost always caused by an invalid CAML query. For the CAML attribute 'FieldRef' you need to use the internal name which cannot contain a whitespace.

Replace

 FieldRef Name="Collective #"

With

 FieldRef Name="Collective_x0020_#"

If still incorrect use powershell to find the field name.


Personally I would do (C#)

 SPQuery myQuery = new SPQuery();
 myQuery.Query ="<Where><Eq><FieldRef Name='Collective_x0020_#'/><Value Type='Lookup'>"+yourValue"+</Value></Eq></Where>";
 MyCollection = MyList.GetItems(myQuery);
share|improve this answer
3  
It seems the "#" character isn't accepted as valid. You just replace it with "<underscore>0023<underscore>" as suggested below. I don't have access to PowerShell but I can view the raw HTML of any page using this list and find that the internal name is indeed "Collective_0020__0023_". –  adnauseam Jun 19 '13 at 15:57
    
I was woundering about that. Thanks! –  user13186 Jun 20 '13 at 0:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.