I've created a View and did group by on a field and then did count on that field. It gives me all records and the total counts. I want to show only records which are greater than 1.... where to put this Greater than in the code ?
I created the view and I opened the view in SharePoint designer and my code looks like this now? I want to put somewhere count(Commitment_x0020_Reference) > 1
<XmlDefinition>
<View Name="{358474DF-DB87-423E-A795-6C361A33655F}" MobileView="TRUE" Type="HTML" DisplayName="Double SI" Url="/networks/SCP/Lists/Contracts and Studies/Double SI.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/15/images/generic.png?rev=23" >
<Query>
<GroupBy Collapse="TRUE" GroupLimit="500">
<FieldRef Name="Commitment_x0020_Reference"/>
</GroupBy>
<OrderBy>
<FieldRef Name="Date_x0020_of_x0020_Reception" Ascending="FALSE"/>
<FieldRef Name="Modified" Ascending="FALSE"/>
</OrderBy>
</Query>
<ViewFields>
<FieldRef Name="ID"/>
<FieldRef Name="Edit"/>
<FieldRef Name="Type_x0020_of_x0020_Procedures"/>
<FieldRef Name="Unit"/><FieldRef Name="Reference"/><FieldRef Name="Title1"/><FieldRef Name="_x0039_i_x002d_Com_x0020_L2_x002"/><FieldRef Name="Modified"/><FieldRef Name="Editor"/><FieldRef Name="_UIVersionString"/>
</ViewFields>
<RowLimit Paged="TRUE">50</RowLimit>
<Aggregations Value="On">
<FieldRef Name="Commitment_x0020_Reference" Type="COUNT"/>
</Aggregations>
<JSLink>clienttemplates.js</JSLink>
<XslLink Default="TRUE">main.xsl</XslLink>
<Toolbar Type="Standard"/>
</View>
The short answer is that you can't. Querying by the sum of grouped results is not possible with a single CAML query.
Alternate options would be to perform post-processing on the results, so the query returns all records, then you remove the unwanted records on the client side, such as with JavaScript; or you could perform multiple queries in sequence to retrieve every possible value for Commitment_x0020_Reference, then perform multiple queries using those values to retrieve and count the number of results for each Commitment_x0020_Reference value.
Neither of those options is very straightforward to implement from the SharePoint Designer GUI, and may warrant a separate Stack Overflow question depending on your approach.
Related
I'm using a CAML query to grab some items from a list. It pulls the items into a datatable which is then set as the datasource of a gridview control.
Everything worked fine until I realized it was using the ID field to sort the items. I wanted to sort by the field Target_x0020_Id, so I ordered the order by, however it doesn't change the behavior when I added this.
This is my query:
WhereEqFieldRefName='Target_x0020_Id' Value Type='Text'900/Value/EqWhereOrderByFieldRef Name='Target_x0020_Id'/FieldRef/OrderBy
The only thing I added was the orderby element. (Sorry I'm having some issues posting the code without it trying to render in the post)
I can only assume this is the CAML you're using
<Where>
<Eq>
<FieldRefName='Target_x0020_Id'>
<Value Type='Text'>900</Value>
</Eq>
<Where>
<OrderBy>
<FieldRef Name='Target_x0020_Id'></FieldRef>
</OrderBy>
I see three glaring errors.
There's a missing space between FieldRef and Name in the <Eq> portion of the caml.
The closing <where> is missing the forward slash.
The FieldRef tag in <OrderBy> is incorrect. It's self closing
This should be the right caml
<Where>
<Eq>
<FieldRef Name='Target_x0020_Id'>
<Value Type='Text'>900</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name='Target_x0020_Id' />
</OrderBy>
I do see that you're returning all items where Target_x0020_Id is equal to 900 and then sorting all those items by Target_x0020_Id. Since this field is always 900, your sort isn't going to work.
Also, is Target_x0020_Id really a text field? If it's numeric, you should chagne the type to 'Number' instead of 'Text'.
I'm creating an iOS app that queries a Sharepoint Webservice with GetListItems. I'm able to retrieve all of the records, but there are a lot of records > 4000. Is there a way I can retrieve 50 at a time?
I'm using SOAP for this by the way.
Assuming you're passing a CAML query to the webservice, you ca use a rowlimit tag
<Query>
<!-- your current query here -->
<RowLimit>100</RowLimit>
</Query>
EDIT
To retrieve a subset of values you could do something like this
<Query>
<Where>
<And>
<Geq>
<FieldRef Name='ID'/>
<Value Type='Counter'>0</Value>
</Geq>
<Lt>
<FieldRef Name='ID'/>
<Value Type='Counter'>100</Value>
</Lt>
</And>
</Where>
</Query>
I haven't tested this, but it should work. Of course, you're going to want to change the values in your code to retrieve the particular subset.
I am now working on sharepoint CAML query. to fetch out item by a given name from a huge resources library which full of images, documents, videos and so on.
I didn't know whether sharepoint index the Name column. I'm thinking to use nested query to make this query faster.
My question here is:
How does sharepoint query work? execute the outer layer first, or start from the inner layer?
I wanna to filter the ResourceType(Image,Document..) first. then filter the category and finally fetch out the items match the name.Is the query bellow work just like what i need?
<Where>
<And>
<Eq><FieldRef Name='IR_ResourcesType' />
<Value Type='Text'>Image</Value></Eq>
<And>
<Eq><FieldRef Name='IR_Category' />
<Value Type='Text'>All</Value>
</Eq>
<Eq>
<FieldRef Name='FileLeafRef' />
<Value Type='File'>aaa</Value>
</Eq>
</And>
</And>
</Where>
If you can, try indexing one of the unique columns in your list so you can use it later on for querying in your where clause.
As best practice, we should also just get the columns that we will be using so as to not have a data buffet. You can limit the columns you will be displaying by using viewFields.
Here is a link which shows some tips and tricks for CAML in conjunction with SPServices which I think you might find useful.
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.
I'd like to have a CAML query to get the events from a calendar list.
I want to get all the events including Today in the upcoming 30 or so days.
So far I have:
<Where>
<DateRangesOverlap>
<Geq>
<FieldRef Name=\"EventDate\" />
<Value Type=\"DateTime\">
<Today />
</Value>
</Geq>
<FieldRef Name=\"EndDate\" />
<FieldRef Name=\"RecurrenceID\" />
<Value Type=\"DateTime\">
<Month />
</Value>
</DateRangesOverlap>
</Where>
This does not work :( Any ideas?
As Nat has said you hint (but don't specify) that you are using recurring events in your calendar list so I am assuming that is the part that's not working?
You need to set the SPQuery.ExpandRecurrances and SPQuery.CalendarDate property prior to running the CAML query and you can only do this using the object model, not the web services or by setting an attribute in CAML.
Further - IIRC this will only expand out instances of recurring events that occur in the same calendar month as .CalendarDate so you have to run it multiple times to get a multi-month view.
This is by far the best reference covering recurring events I have found.
Understanding the SharePoint Calendar and how to export it to iCal format
Welcome to the nightmare of recurring events in SharePoint!
The recurrence does not actually add all the events into the list, but the CAML can only query from "actual" events stored in the list.