CAML Query for multiple branches of "And-Or-In" statements - sharepoint

I'm using CAML Query to pull items from a list.
I tried re-arranging the structure of my query, and still would not return anything. I try not to use CAMLQuery Builder (doing it manually).
<Query>
<Where>
<And>
<Contains>
<FieldRef Name="Field1"/><Value Type="Text">A</Value>
</Contains>
<And>
<In>
<FieldRef Name="Field2"/><Values><Value Type="Text">B</Value></Values>
</In>
<Or>
<In>
<FieldRef Name="Field3"/><Values><Value Type="Text">C</Value></Values>
</In>
<In>
<FieldRef Name="Field4"/><Values><Value Type="Text">D</Value></Values>
</In>
</Or>
</And>
</And>
</Where>
</Query>
Note: I have seen questions that may seem as a duplicate of this, but have never seen a question go as deep as this level of branching. CAML Query seems to be particular in where you put your closing tags.
Did I nest my "And" and "Or" incorrectly? What could be wrong/missing in this query?

While your CAML appears valid to me (without actual <FieldRef> and <Value> elements, I can't specifically test your field names, types & values), I tend to write my CAML in a more "balanced" fashion, as such:
<Query>
<Where>
<And>
<And>
<Contains>A</Contains>
<In>B</In>
</And>
<Or>
<In>C</In>
<In>D</In>
</Or>
</And>
</Where>
</Query>
According to the spec, both your posted version & my answer here would net the same result, but I would test to confirm. If the above doesn't work, brevity aside, I'd suggest posting your full CAML so that we can recreate your List with the same field names & types and test data with matching values.

Related

Properly Nesting Compound AND / OR Statements Using CAML

I am leveraging SPservices within a SharePoint 2013 environment to create filtered views unique to the current user. Effectively, when a user visits a page, they get a custom feed of documents that they have uploaded to a specific library.
I can successfully query the library and render the results they way I want, but when I attempt to create a compound CAML query, I keep getting errors. I have tried using the various resources here to manually build this query (and do to network restrictions I cannot use the U2U CAML builder tool), but I keep getting thrown errors. I started doing simple queries and it was working, but once I try to get to my third and forth "OR" statements (including a IsNull argument), I hit a wall. I think I am improperly nesting my statements and would love some help in identifying what I am doing wrong.
<Where>
<And>
<Contains>
<FieldRef Name='Author' />
<Value Type='User'>" + userName + "</Value>
</Contains>
<And>
<Eq>
<FieldRef Name='ContentType' />
<Value Type='Computed'>Document</Value>
</Eq>
</And>
<Or>
<Or>
<Or>
<Or>
<Eq>
<FieldRef Name='sensitivity' />
<Value Type='Choice'>Low</Value>
</Eq>
<Eq>
<FieldRef Name='sensitivity' />
<Value Type='Choice'>Medium</Value>
</Eq>
</Or>
<Eq>
<FieldRef Name='sensitivity' />
<Value Type='Choice'>High</Value>
</Eq>
</Or>
</Or>
<IsNull>
<FieldRef Name='sensitivity' />
</IsNull>
</Or>
</And>
</Where>
The desired result would be an output of items that the current user uploaded / created WHERE the Content Type is equal to "Document" AND the Sensitivity (choice) field is equal to Low, Medium, High or is empty.
I feel like I am really close, but I am making a silly mistake. For context, the "userName" reference in the statement is a javascript variable that is functioning properly.
The bracketing can be quite confusing without a tool like the U2U CAML builder tool. I feel your pain.
I think your issue may be the isnull at the end. I believe you would want is NotNull
<IsNotNull>
<FieldRef Name='sensitivity' />
</IsNotNull>
As a last ditch you might be able to just query all files by user whos documents co

SharePoint 2013 View Filter using CAML and User ID

To start off, I would like to thank anyone in advance for the help. I am using SharePoint 2013, SharePoint designer 2013, and attempting to create a new filtered view...
I have a list of entries that has a column which contains multiple username's.
Ex: Username1 Username2 Username3
What I'm looking to do is filter the view based upon whether or not the UserIDs column contains the currently logged in user. I've found a couple of walk throughs on this issue but none seem to work (I can't seem to make it do so). I believe the reason it does not work is because our SharePoint environment adds a 'salt' to the beginning of each username it pulls.
Ex: i:0#.w|DomainName\UserName
As you can tell in the column above, I'm substringing all of the Salt/Domain information out. Would anyone happen to know if this is possible?
If so, do I need to use Jscript to pull the username before I filter?
Below are three more examples of what I've tried....
<Where>
<Contains>
<FieldRef Name='UserIDs' />
<Value Type='User'>System Account</Value>
</Contains>
</Where>
<Where>
<Contains>
<FieldRef Name='UserIDs' LookupId='True' />
<Value Type='User'>1073741823</Value>
</Contains>
</Where>
<Where>
<Contains>
<FieldRef Name='UserIDs'/>
<Value Type='User'><UserID/></Value>
</Contains>
</Where>
I've used these two before and they worked:
<Where>
<Eq>
<FieldRef Name="UserIDs" LookupId="TRUE" />
<Value Type="User">1073741823</Value>
</Eq>
</Where>
<Where>
<Eq>
<FieldRef Name="UserIDs" LookupId="TRUE" />
<Value Type="Lookup">1073741823</Value>
</Eq>
</Where>

Sharepoint 2010 CAML Query Person Lookup

I have a CAML query where I am trying to retrieve records based on the logged in users id.
The following query works on my dev & qa environments, but not in my production environment. There is data that exists that matches the user. I have also tested inside SP CAML Query Helper and again there is 0 results returned.
I have tried:
<Where>
<Eq>
<FieldRef Name='SharepointUser' LookupId='TRUE'/><Value Type='Integer'>37</Value>
</Eq>
</Where>
<Where>
<Eq>
<FieldRef Name='SharepointUser' LookupId='TRUE'/><Value Type='User'>37</Value>
</Eq>
</Where>
<Where>
<Eq>
<FieldRef Name='SharepointUser' LookupId='TRUE'/><Value Type='Lookup'>37</Value>
</Eq>
</Where>
SharepointUser is of Type="User"
Is there some setting that I am missing that I need to set for Lookups to work appropriately?
If you are looking for the current user's ID, you should use the <UserID/> CAML element, like this:
<Where>
<Eq>
<FieldRef Name="SharepointUser" LookupId="TRUE"/><UserID/>
</Eq>
</Where>

SharePoint: Validating SPQuery

I am writing an internal API for my company which allows users to pass in SharePoint CAML query.
In side my function I take to user's query, and add some additional elements to it and then use the final query to retrieve required data from SharePoint.
Example:
User passes in:
<Query>
<Where>
<Eq>
<FieldRef Name='Category' />
<Value Type='Choice'>Bug</Value>
</Eq>
</Where>
</Query>
Internally, I modify the query to be:
<Query>
<Where>
<And>
<Eq>
<FieldRef Name='Category' />
<Value Type='Choice'>Bug</Value>
</Eq>
<Eq>
<FieldRef Name='AssignedTo' />
<Value Type='Integer'><UserID /></Value>
</Eq>
</And>
</Where>
</Query>
What do you think is the best way to validate queries sent by users?
If the queries that you're going to allow are fairly restricted, it might be a good approach to build a schema to represent what a valid query would be. Then you could just see if their xml is valid according to that schema. Also, I know that you can use the CAML Builder dll from code. I can't find an example of this right away, but there may be a way to use it's CAML building methods in a try/catch block to stop invalid queries from ever getting built.
Also, it occurs to me that you may need to watch out for the fact that the CAML query's FieldRef will need to be built using the internal name of the field which may differ from the display name.

How to query all lists that use a certain content type in SharePoint?

I have the following CAML query:
<Where>
<And>
<Eq>
<FieldRef Name='PublishToSM' />
<Value Type='Boolean'>True</Value>
</Eq>
<IsNull>
<FieldRef Name='SMUpdateDate' />
</IsNull>
</And>
</Where>
I have only one content type that uses these fields. When I run this query against a list that uses this content type everything works fine. When I run it against a List that does not it throws the error: One or more field types are not installed properly. Go to the list settings page to delete these fields.
I would like to be able to search all Lists on all websites in a site collection. Can this be done without erroring out?
Use SPSiteDataQuery, add a where clause to include the content type. i.e.:
<Where>
<And>
<Eq>
<FieldRef Name='ContentType' />
<Value Type='Text'>CONTENTTYPE NAME</Value>
</Eq>
<And>
<Eq>
<FieldRef Name='PublishToSM' />
<Value Type='Boolean'>True</Value>
</Eq>
<IsNull>
<FieldRef Name='SMUpdateDate' />
</IsNull>
</And>
</And>
</Where>
<BeginsWith>
<FieldRef Name='ContentTypeId' />
<Value Type='ContentTypeId'>CONTENTTYPE ID</Value>
</BeginsWith>
Set the SPSiteDataQuery's Scope property to SiteCollection. By setting the Lists property you can also limit the search to for instance document libraries etc. The ViewFields property can be set to limit the fields retrieved (i.e. instead of the equivalent of a select * on the items's fields)

Resources