10

I am trying to check if a filename exists in the 'Name' column of Shared Docs Library. I think only CAML could help here. So I made this query

"<Where><Eq><FieldRef Name='Name' /><Value Type='Text'>" + docname + "</Value></Eq></Where>"

But I get this error One or more field types are not installed properly. Go to the list settings page to delete these fields.

 string docname = "Disabled Service";  
        StringBuilder sb = new StringBuilder();  
        sb.Append("<Where><Eq><FieldRef Name='Name' /><Value Type='Text'>"+docname+"</Value></Eq></Where>");  
        SPQuery spq = new SPQuery();  
        spq.Query = sb.ToString();  
        spq.RowLimit = 1;  
SPList list = web.Lists["Shared Documents"];  
                    DataTable dt = list.GetItems(spq).GetDataTable();  

Kindly help..

sanju
  • 101
  • 1
  • 1
  • 3

2 Answers2

21

The internal name of Name is FileLeafRef so your query should be:

"<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>" + docname + "</Value></Eq></Where>"

If you don't like building the query by hand then a couple of good tools are:
- U2U CAML Query Builder which gives you an UI for building the query.
- CAML.NET which is a library for safely building CAML queries

Per Jakobsen
  • 32,409
  • 1
  • 33
  • 62
2

I posted this as a comment but perhaps it is a part of the answer: You need to use the field internal name in your query, not the display name.

The easiest ways to find internal name of columns: 1. If you see the field in list settings, click it. the internal name will be in the query string of the settings page. 2. if you see the field in the list view, sort or filter by it. Again the internal name will be in the query string.

If these fail, in SP2013 you can use the REST API: http://[your site]/_api/web/lists/getByTitle('[list name]')/Fields?$select=Title,InternalName This will get you a list of all fields in that list with their title and internal name.

Shai Petel
  • 279
  • 3
  • 6