Prash's Blog

CAML Query to Search for URL field in SharePoint June 19, 2009

Filed under: SharePoint — prazjain @ 12:04 pm
Tags: ,

As I recently got assigned to a SharePoint project, I had a scenario where I had to perform a search on a URL field. Checking loads on forums / blogs showed one or two solutions which did not resolve my problem. So I am posting my solution here just in case it helps any one else.

This is how I formed the CAML query :

SPQuery query = new SPQuery();        
query.Query = "<Where><Contains><FieldRef Name='URL'/><Value Type='URL'>"+ urlParameter  +"</Value></Contains></Where>";

//get the results
SPListItemCollection result = list.GetItems(query);

where urlParameter variable holds the relative url i.e. /mysite/subsite

Some blogs suggested using <Eq> but that did not work for me, rather its <Contains> that did the trick.

Advertisements
 

9 Responses to “CAML Query to Search for URL field in SharePoint”

  1. johnny Says:

    Hello. Thank you for this great info! Keep up the good job!

  2. prazjain Says:

    I am glad that the post helped you.

  3. Murli Says:

    Thanks , this has helped me a lot.

  4. mc choov Says:

    Thanks a lot 🙂

  5. Koen Zomers Says:

    The reason why a contains actually works is because URL fields are being stored in the “[url], [description]” format. If you do an Eq(uals) then it can’t find it because the title is behind it. If you do a Contains, it will find it because it does contain the url. A BeginsWith should theoretically also do the trick, but I’ve found the whole CAML queries with URL fields to be highly unreliable. Sometimes it does work, sometimes it doesn’t. I’m just retrieving all list items and using Linq to perform the search myself. Worse for performance, but much better for reliability of the results.

    • prazjain Says:

      Hi Koen,
      Thanks for the explanation, it makes things a bit clear. But having said that, the whole thing is a bit unreliable 🙂

      Cheers
      Prashant

    • Vijay Says:

      Hi ,

      If URL is “http://www.google.com” and description is “Yahoo” then if we search using description key the item is not displaying in results.. Please help me in this,

      “http://www.google.com, Yahoo”

      it is searching only the URL content.. not searching in description area.

  6. sree Says:

    it is not working in o365 site.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s