Skip to main content
Malcan Logo

Polski  English  
  
Malcan > Tips and tricks > Cannot find anything with SPQuery CAML and GetItems when searching against URL field  

Tips and tricks: Cannot find anything with SPQuery CAML and GetItems when searching against URL field

See also Malcan Workflow for Sharepoint

Technology

SharePoint 2007 

Title

Cannot find anything with SPQuery CAML and GetItems when searching against URL field 

Hint

When you want to look for items on the list or library searching with CAML by URL field you find zero items... although the URL is there.
 
The typical CAML string is below:
<Where>
   <BeginsWith>
      <FieldRef Name='URL'/>
      <Value Type='Text'>http://server/file.txt</Value>;
   </BeginsWith>
</Where>
 
Typical C# code to use SPQuery is:
SPSite oSite = new SPSite("http://moss");
SPWeb oWeb = oSite.OpenWeb();
SPList oList = oWeb.GetListFromUrl("Lists/Links/AllItems.aspx");
SPQuery linksQuery = new SPQuery();
linksQuery.Query = this.txtCAML.Text;
MessageBox.Show(oList.GetItems(linksQuery).Count.ToString());
 
There are few things you may wonder:
1) Should I use <Eq> or <BeginsWith> or <Contains> tag => in my case it turned out BeginsWith works fine (as URL is encoded as "URL, Title" in the field).
2) Shoold the value type be 'Text' or 'Url' => in my case it turned out both are equal.
3) Why the hell I cannot find my items (returns zero)? => it turns out the rules are as follows:
   a) IF you look for link that points to the same Sharepoint box THEN you should search with relative URL (so the caml value is just /file.txt and not http://server/file.txt.
   b) IF you look for link that points to any other box or even the same box with different port number (e.g. http://server:81/file.txt) THEN you should use full URL as in the CAML example above.
Attachments
Created at 6/15/2010 13:16  by Artur Malinowski 
Last modified at 6/15/2010 13:25  by Artur Malinowski