SPQuery : Difference between Query and ViewXml properties? - sharepoint

Hello SharePoint developpers !
I can't deeply understand the difference between Query and ViewXml properties in the SPQuery object. In the msdn documentation, it's written :
Query : Gets or sets the inner XML
used in the query.
ViewXml : Gets or sets the XML schema that defines the
view.
it seems to me that ViewXml is appropriate to filter the fields you want to retrieve... I'm not sure.
So what's the difference ? in which situations should we choose the first over the second ?
How SharePoint is treating those queries ..
Mystery remains for me so if someone could throw some light on it ?
thank you...

ViewXml completly describes query. It can contain Query, ViewFields, RowLimit elements and many more. For SPQuery you should better use corresponding properties (Query, ViewFields, RowLimit etc), and ViewXml will be generated automatically. You can test it by setting this properties for SPQuery object and then look to ViewXml. You should set ViewXml manually if you need to set some specific properties (but as I remember they all can be set using SPQuery properties).

Related

SharePoint 2010, CAML query to see if a new item has been added to a list (Announcements)

all. I am trying to build a CAML query for a SP2010 list that will check the Announcements to see if anything new has been added (or a CAML query + some functionality). I do not truly understand CAML and SP yet and would be grateful for any help - thank you.
Have a look around an SPQuery object if you want to go down the CAML route.
You will want to get all the items for a given date/time range unless you have a field that flags if an item is new..... the link below should help you build a query
http://social.msdn.microsoft.com/Forums/sharepoint/en-US/fed59f8e-72e2-46e2-9329-460fd65d7536/caml-query-datetime
Once you have a SPQuery object pass it to the SPList GetItems() method
You will then have a collection of items what you do with them then is all up to you :)
Hope this helps

SharePoint returns extra system columns

I do query to SherePoint. I have created query, viewquery and query options.
Web services returns me great results, but it include some other system columns such as:
ows_Modified , ows_DocIcon, ows_Editor. I don't want them. How do I return only those which is in ViewQuery string?
My queryoptions is:
#"<QueryOptions>
<IncludeMandatoryColumns>False</IncludeMandatoryColumns><ViewAttributes Scope='Recursive' />
</QueryOptions>";
Thanks!
In order to return only selected columns (and not all of them) use ViewFields property of SPQuery object. You can find some more information about it and a sample code here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.viewfields.aspx.
In order to do it from javascript, you can try code as written here (that post is on another topic, but it still shows how to specify fields to select): https://sharepoint.stackexchange.com/questions/33683/spservices-today-not-returning-correct-results.

Accessing sharepoint from caml

How can I add javascript to my CAML code?
For example, I want to calculate some rates/dates according to local field on Sharepoint list.
I want to set value of field according to the javascript result.
Any Idea ?
Poli .
You can add JavaScript to your page, not to CAML.
CAML is used to query Sharepoint lists.
The results will be rendered as HTML
Have a look at the rendered HTML and go from there.
You can't really "add" javascript code to a CAML query because CAML queries run on the server where as javascript runs client side.
Say you have a query like this :
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='FieldName' /><Value Type='Text'>TestValue</Value></Eq></Where>";
When you run your query :
SPListItemCollection items = list.GetItems(query);
You will end up with your items. This is where you can modify them and run your logic code (in your backend code).
For example :
foreach(SPListItem item in SPListItemCollection)
{
int rate = item["SomeField"].ToString() + item["SomeOtherField"].ToString();
//Do whatever you want with the result
}

SharePoint CAML OrderBy Modified does not work

I have made a query against a list. I want to get the last modified item which meets a certain condition, and my query looks like this:
<Query><OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy><Where><Eq><FieldRef Name='kortnummer'/><Value Type='String'>kv11</Value></Eq></Where></Query>
I get the listitems i need, just not in the right order. Changing Ascending to true does nothing, so obviously there is something with the OrderBy clause that is not right..
According to MSDN it should be possible to order by Modified.
Any idea why my OrderBy does not work?
I always put the OrderBy after the Where.
Otherwise you could make sure that you are using the internal name. I usually use SharePoint Manager 2007 to get the internal field names. Codeplex SharePoint manager
I can see that on a list in my SharePoint, the modified column internal name is "Last_x0020_Modified".
If you're doing this in a C# string to get an SPListItemCollection, you don't need "<Query></Query>" tags.

CAML GroupBy usage when querying a list using the SPQuery or SPSiteDataQuery object

I'm not sure how to use the GroupBy clause when querying a list. The SPListItemCollection or datatable looks exactly the same regardless of the groupby clause.
SPQuery query = new SPQuery();
query.Query = "<GroupBy><FieldRef Name=\"Area\"/></GroupBy>";
DataTable result = list.GetItems(query).GetDataTable();
// result.Rows.Count = Same as ungrouped query
Is GroupBy only supported by Lists.asmx webservice?
Found a reference on MS Social which suggests it's not supported on the SPSiteDataQuery object http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/ab8df6f5-35a0-401e-88cb-3eb31362bf0c/
I believe that clause is more related to how the grouping will be displayed in the UI. As far as the data returned goes, you might/should get some sorting but that's about it.
Regarding the UI, there is the GroupLimit element which limits how many groups are returned. There is also the Collapse element which has no meaning when used with SPQuery.
How were you hoping the data would be different?

Resources