Tag Archives: caml

Considerations When Using View in CAML Search

I recently worked on a web part which processes test scores for students. Test scores are stored in a list, organized by course and by user. The web part should retrieve the test scores for the logged in user, then the user’s score for each course. A previous implementation used a view, My Scores, which showed test scores for the logged in user by defining a filter, “Username is equal to [Me]”.

It occurred to me to retrieve a user’s test score for a course by performing a CAML search on the My Scores view. Typically, when searching a list using a CAML query, you can do something like this:

SPQuery query = new SPQuery();
query.Query = <query for course>;
results = list.GetItems(query);

There is an overloaded version of the SPQuery constructor that takes an SPView. Additionally, SPList.GetItems has an overloaded version that also takes an SPView. Given the availability of the SPView parameter, I was tempted to perform the CAML search on the My Scores view by doing the following: Continue reading Considerations When Using View in CAML Search

Searching for Files with CAML

I was interested in searching for a file in a Document Library. Using a CAML query is much more efficient than iterating through the Document Library and examining each file. There are a couple of scenarios.

Searching at the root level of the Document Library

Searching for files is a little different from searching for other list items. In SharePoint, a file is shown with the same attributes as any other list item. When creating the CAML query, it would seem natural to search a field such as Title for a given file name. However, for a document, the Title field by default doesn’t store the file name. Rather, it contains arbitrary content that can be edited in Word (under the document’s Properties).

Furthermore, when creating the CAML query, you must reference a field using its internal name. So you won’t be able to use a field such as “Name”, as it is not a valid internal name (source: List of internal names for SharePoint fields).

Incorrect internal name error
If you don’t use the correct internal name of a field in a query, you may get the following error when attempting to access the results of the query: One or more field types are not installed properly. Go to the list settings page to delete these fields.

The correct field to use when searching for files is FileLeafRef, which is the appropriate internal name to use for the Name field. The C# snippet to create the query is as follows: Continue reading Searching for Files with CAML