CAML is a robust filtering language that is commonly used with OneView web parts. Unfortunately, there are times when the structure needs to be a little more flexible. With this trick, and OneView parameters, you can create a semi-dynamic CAML query to get around some of those structural limitations. The trick fakes out the web part by using OneView parameters and XML comments.
First, create your CAML as you would normally. Include all the fields you need to filter on.
<OrderBy>
<FieldRef Name="Created" />
</OrderBy>
<Where>
<And>
<And>
<Eq><FieldRef Name="Location"/><ValueType="Text">My Location</Value></Eq>
<Eq><FieldRef Name="ContentType"/><Value Type="Text">My Content Type</Value></Eq>
</And>
<Eq><FieldRef Name="FirmOffice"/><Value Type="Text">[MyOfficeParameter]</Value></Eq>
</And>
</Where>
In this example, you might not always want to filter on [MyOfficeParameter]. You’d like to either see all of the items or just the ones filtered by a certain office. Since you are using a OneView parameter, you can take advantage of this. You can set the entire filter for [MyOfficeParameter] to be conditional by surrounding it with {curly braces}. You can also make other parts of the query conditional as long as it includes the parameter and is surrounded by {curly braces}. If you insert our parameter into a comment you can now wrap the starting <And> along with the comment in {curly braces} to make that part conditional as well. This will keep your XML structure intact while allowing for some flexibility.
Here’s a modified version of the same CAML that will make the entire filter on FirmOffice conditional.
<OrderBy>
<FieldRef Name="Created" />
</OrderBy>
<Where>
{<And><!-- [MyOfficeParameter] -->}
<And>
<Eq><FieldRef Name="Location"/><ValueType="Text">My Location</Value></Eq>
<Eq><FieldRef Name="ContentType"/><Value Type="Text">My Content Type</Value></Eq>
</And>
{<Eq><FieldRef Name="FirmOffice"/><Value Type="Text">[MyOfficeParameter]</Value></Eq>
</And>}
</Where>
Now the query will check if [MyOfficeParameter] has a value. If it does then it will include the starting <And> as well as the conditional filter and the closing </And>.
This can be expanded upon if you have more conditions that you may need to filter on. A good example would be for a filterable news item list. You could have all your different filters like Office, Department, Practice, etc. and make each one conditional so you can filter by some, all, or none of the fields.