•  

    Hello,

    I am trying to write a custom search query using CAML on a list which contains a URL field.

    The CAML query contains a WHERE clause on the URL field. What I have noticed is that when the URL field is used in the WHERE clause it does not return any results back. Maybe my CAML query is wrong??

    Any help would be appreciated.

    Here is the CAML query snippet:

     <Where>

        <BeginsWith>

                <FieldRef Name='URL'/>

                <Value Type='Text'>http://moss12/client1</Value>

         </BeginsWith>

    </Where>

     Now, I have also tried changing the Value Type to URL...

     <Where>

        <BeginsWith>

                <FieldRef Name='URL'/>

                <Value Type='URL'>http://moss12/client1</Value>

         </BeginsWith>

    </Where>

     I have also tried using <Contains>, <Eq> conditions, but no results. Has anyone tried writing CAML queries on URL field?

Answers

  • Friday, January 04, 2008 10:58 PM
    Avatar of M. Blumenthal
    Magenic
    (Partner)
    30 Points
     
     Answered

    I recently needed to see if an URL was in an SPList, and what I ended up doing was this:

    1. Create a string variable to hold the tail end of the URL I am looking for.  So if I am looking forhttp://server/managedpath/spwebOrSitecollectionname, I would put "/managedpath/spwebOrSitecollectionname" in  the URLtail string variable.
    2. Do my SPQuery to match against the URLTail instead of the URL, My CAML would look like this:

    <Where>

        <BeginsWith>

                <FieldRef Name='URL'/>

                <Value Type='URL'>/managedpath/spwebOrSitecollectionname</Value>

         </BeginsWith>

    </Where>

     

    3.  When I get my results back, convert each one to a string and see if it contains the FULL url (http://server/managedpath/spwebOrSitecollectionname).

    Hope that helps!

    --M.Blumentha

All Replies

  • Wednesday, September 26, 2007 7:01 PM
    Avatar of Steve.Curran MVP
    KnowledgeLake
    (Partner, MVP)
    29,755 Points
     
     

    Change your Type to URL

     

    <Value Type='URL'>http://moss12/client1</Value>

     

     
  • Thursday, September 27, 2007 2:55 AM
    Avatar of Pritam Dahake
     
    90 Points
     
     

    In the 2nd CAML code snippet I have, I did try with Value Type of 'URL'....but it doesnt return any results..

    Can you confirm that you are able to use URL field in your CAML query?

     

    Thanks

     
  • Thursday, September 27, 2007 3:20 AM
    Avatar of Steve.Curran MVP
    KnowledgeLake
    (Partner, MVP)
    29,755 Points
     
     
    yes

     

     
  • Thursday, September 27, 2007 4:31 PM
    Avatar of Pritam Dahake
     
    90 Points
     
     

    smc750,

    Below is the code snippet I am actually using in my application and it is not returning any results back.

    The "Sites" list has a column of type Hyperlink called "URL" with internal name as "SiteURL". The list has right now two items in it, with URLs http://moss12/Client1 and http://moss12/Client2. Using this query I was expecting to get both the items back.

     

    Do you see anything wrong with the code here? I have tried using FieldRef Name='URL' as well as FieldRef name="SiteURL'....both dont return any results back!!!

     

     

    SPWeb spWeb = SPContext.Current.Web;

    string sQuery = "<Where><BeginsWith><FieldRef Name='URL'/><Value Type='URL'>http://moss12/Client</Value></BeginsWith></Where>";

    string sList = "Sites";

    SPList spList = spWeb.Lists[sList];

    SPQuery query = new SPQuery();

    query.Query = sQuery;

    SPListItemCollection items = spList.GetItems(query);

     
  • Thursday, September 27, 2007 6:07 PM
    Avatar of Steve.Curran MVP
    KnowledgeLake
    (Partner, MVP)
    29,755 Points
     
     
    Nothing wrong here. Could be security. Check to make sure the user executing query has read rights to the items in question.

     

     
  • Thursday, September 27, 2007 7:28 PM
    Avatar of Pritam Dahake
     
    90 Points
     
     

    The code is in development running on my machine...

    I am the machine admin....the sharepoint service is running under my account...so there is no security issue here...Sad

     

     
  • Friday, October 05, 2007 6:01 PM
    Avatar of Robert L. Bogue [MVP]
    Thor Projects LLC
    (Partner, MVP)
    25 Points
     
     

     

    The problem is likely that MOSS12 is the name of your SharePoint server.  SharePoint strips the server name for relative urls before storing them in the DB so when CAML does the query it needs to be just the relative path.
     
  • Tuesday, October 09, 2007 3:28 PM
    Avatar of Dan Keeling
    Slalom Consulting
    (Partner)
    1,530 Points
     
     

    Download and use the U2U tool, you can get test data back from an order by query, and then see in plain text what the values of the URL field look like.  Then change as needed.  Link is here

     
  • Monday, October 22, 2007 12:25 PM
    Avatar of Kjaps
    Bufdir
     
    20 Points
     
     

     

    Did you find a solution to this issue? I have the exact same problem. I have used the CAML Query Builder (U2U) to examine the URL field, and it looks like this

     

    http://server/mywebhttp://server/myweb

     

    This is driving me nuts.

     
  • Monday, October 22, 2007 1:31 PM
    Avatar of Steve.Curran MVP
    KnowledgeLake
    (Partner, MVP)
    29,755 Points
     
     

    This whole issue could be related to a microsoft bug in the spquery and spquerysitedataquery classes. You can find out more here. This bug is not due to be fixed until SP2. I have seen no results come back for this reason many times.

     

    http://www.sharepointblogs.com/smc750/archive/2007/07/24/spsitedataquery-limited-to-10-document-libraries-or-lists.aspx

     

     
  • Friday, January 04, 2008 10:58 PM
    Avatar of M. Blumenthal
    Magenic
    (Partner)
    30 Points
     
     Answered

    I recently needed to see if an URL was in an SPList, and what I ended up doing was this:

    1. Create a string variable to hold the tail end of the URL I am looking for.  So if I am looking forhttp://server/managedpath/spwebOrSitecollectionname, I would put "/managedpath/spwebOrSitecollectionname" in  the URLtail string variable.
    2. Do my SPQuery to match against the URLTail instead of the URL, My CAML would look like this:

    <Where>

        <BeginsWith>

                <FieldRef Name='URL'/>

                <Value Type='URL'>/managedpath/spwebOrSitecollectionname</Value>

         </BeginsWith>

    </Where>

     

    3.  When I get my results back, convert each one to a string and see if it contains the FULL url (http://server/managedpath/spwebOrSitecollectionname).

     

     

    Hope that helps!

    --M.Blumenthal

     

     
  • Wednesday, September 03, 2008 3:04 PM
    Avatar of nuclear
    (Partner)
    75 Points
     
     
    Hi 

    This didn't help me, so this is a column that you save in the list itself, or does this refer to the document's url itself?

    I am looking to write a CAML query to lookup the newly added file through it's fileURL. This is necessary because the library is to full of records (13000) and the File.Item or other methods to get to the listitem takes to long or throws an ThreadAbortException

    Any ideas?

    So basically I want the CAML query to get to the list item with the file's url as input parameter?

    Thanks
     
  • Wednesday, October 22, 2008 2:43 PM
    Avatar of Neuner
     
    0 Points
     
     
    Hi 

    I Tested with a similar CAML Query. I can Confirm that Behaviour. With the SPField.InternalName in the FieldRef i can find every Url except those starting with http://MyServername. Is there a Way to query for the Urldescription?
     
  • Tuesday, December 16, 2008 7:32 PM
    Avatar of PaulE
     
    185 Points
     
     Proposed Answer Has Code
    A lot of trial-and-error has yielded something that might be helpful to know: it appears that querying a URL field with the value type set to URL causes the match to be made only of the URL portion of the field. Thus, for a URL field with the value "/managedpath/spwebOrSitecollectionname/default.aspx, MyWebOrSiteCollection", the following CAML would work (possibly even more appropriately so):
     
    <Where> 
      <Eq> 
        <FieldRef Name='URL'/>  
        <Value Type='URL'>/managedpath/spwebOrSitecollectionname/default.aspx</Value>    
      </Eq>    
    </Where> 
    • Proposed As Answer by Neela Krishna B Tuesday, August 23, 2011 1:33 PM
    •  
     
  • Wednesday, April 01, 2009 11:28 AM
    Avatar of Joke E
     
    0 Points
     
     
    Thanks PaulIE, this turned out to be a very helpfull post for me.
    This is exactly what I was looking for.

     
     
  • Tuesday, April 28, 2009 2:23 PM
    Avatar of marlobello
    ExxonMobil
     
    5 Points
     
     
    The best way to get the URLTail that M. Blumenthal mentions is the SPWeb.ServerRelativeUrl property. This CAML should work. Notice that you CAN and probably should use <Eq></Eq>.

    C#

    string relativeurl = SPContext.Current.Web.ServerRelativeUrl;
    
    string query = @"<Where>
    
        <Eq>
    
                <FieldRef Name='URL'/>
    
                <Value Type='URL'>" + relativeurl + @"</Value>
    
         </Eq>
    
    </Where>";
    

    marlobello@msn.com
     
  • Thursday, July 30, 2009 4:22 PM
    Avatar of Cawood
    DoxTree
    (Partner, MVP)
    2,466 Points
     
     
    It's not pretty, but you can get the information from the list web service.

    ows_URL="http://www.test.com, testdescription"

    With some string slinging, you can parse out what you need.
    - cawood
     
  • Thursday, October 29, 2009 11:58 AM
    Avatar of Ashwin Bhagwat. _
    3DPLM Software Solutions
     
    775 Points
     
     Proposed Answer Has Code
    Hi,
    
    
    Replace <Eq> by <Contains> and </Eq> by </Contains>
    
    
    Query should look something like:
    
    
    <Where>< Contains ><FieldRef Name='URL'/><Value Type='URL'>" <relative-url>"</Value></Contains ></Where>
    
    
    It works for you.
    
    
    Thanks,
    Ashwin
    • Proposed As Answer by Mohit9900 Tuesday, November 03, 2009 11:14 AM
    •  
     
  • Tuesday, November 03, 2009 11:15 AM
    Avatar of Mohit9900
     
    0 Points
     
     
    Thanks Ashwin.. I was avoiding to use SPQuery.. But finally got succeed by making small change..
     
  • Wednesday, June 16, 2010 5:59 AM
    Avatar of Artur MALCAN Malinowski
    Malcan
    (Partner)
    0 Points
     
     
    The real problem is that Sharepoint is looking for URL field differently... depending if you want to find URL on the same box or URl from different box. Checkhttp://malcan.com/EN/Lists/Tips%20and%20tricks/DispForm.aspx?ID=23 for solution, please.
     

    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 nothttp://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.

     

    Should this CAML be working. (I tried it with u2u and it does not display the URL column.) I get this error. {System.ApplicationException} = {"One or more field types are not installed properly. Go to the list settings page to delete these fields."}

    <Where><Eq><FieldRefName='URL'/><ValueType='URL'>/path/HR Policy.docx</Value></Eq></Where>
    share|improve this question
     
     
    Now using <Contains> instead of <where> and working. – Mina Kumari Mar 14 '11 at 17:21
    2  
    The URL field will have the display text also stored in it, so this is why you need the Contains node. – James Love Mar 14 '11 at 17:23
    feedback

    Querying for a Document in a Document Library

    Looks like you are trying to find a document in a document library using CAML. You can do this by using the hidden column named "FileRef":

    <Where><Eq><FieldRefName="FileRef"/><ValueType="Url">sites/SiteCollection/SubSite/Site Documents/Excel Report.xls</Value></Eq></Where>

    Note: Do not include the server name or beginning /.

    Querying for a document or list item with a URL field

    The following examples assuming you have a list or library setup with a URL column named "My Document".

    The link is to a document that is hosted on the sharepoint server (do not need server name):

    <Where><Eq><FieldRefName="My_x0020_Document"/><ValueType="URL">/sites/subsite/Site%20Documents/Excel%20Report.xls</Value></Eq></Where>

    The link is an absolute URL to something not on the server... for example http://www.google.com:

    <Where><Eq><FieldRefName="My_x0020_Document"/><ValueType="URL">http://www.google.com</Value></Eq></Where>

     
     
    @Kit Menke: I tried this as well (including yours) but same field not installed error <Where><Contains><FieldRef Name='URL'/><Value Type='URL'>/sites/SiteCol/subsite/Shared Documents/March Excel Report.xls</Value></Contains></Where> – Mina Kumari Mar 24 '11 at 12:28
     
    @Mina Kumari: I've updated the answer to include a query for a document in a document library which I think is what you're after. – Kit Menke Mar 24 '11 at 18:07
     
    @Kit Menke: I am not getting that error with this code. I should see one file but no record is returned with the query. <Where><Eq><FieldRef Name="FileRef"/><Value Type="Url">sites/SiteCollection/SubSite/Site Documents/Excel Report.xls</Value></Eq></Where> – Mina Kumari Mar 24 '11 at 19:51
     
    @Mina Kumari: What's the path of your document and did you put that into the query? Can you show the exact query you are trying? – Kit Menke Mar 24 '11 at 20:06
     
    <Where><Eq><FieldRef Name='FileRef'/><Value Type='Url'>sites/gatorade/Shared Documents 5/Sales Folder/Team_lists.txt</Value></Eq></Where> I put this in u2u tool. havnt tried in the winform app. – Mina Kumari Mar 24 '11 at 20:18
    show 3 more comments
    Was this post useful to you?   
posted on 2012-11-28 23:06  ilawrence  阅读(1960)  评论(0编辑  收藏  举报