problem with SPQuery for whole sitecollection? - sharepoint

HI, Can we use SPQuery to query whole site collection. Means I have a list which is in different site so is it possible to use SPQuery for that or I need to use SPSiteDataQuery.
I know SPSiteDataQuery would work in this case but I don't wanna search all the lists as I know the name of the list and in SPSiteDataquery i can't mention the list name and in my case list name is unique in whole site collection. If not SPQuery whats the best method to query my list . I don't wana use Guid over here....
Any suggestions?
Thanks,

If you know the exact position of the list (i.e. the (sub)web it is in), you can just use
using(SPWeb otherWeb = SPContext.Current.Site.OpenWeb("urlofweblistisin"))
{
SPList yourList = otherWeb.Lists["YourListName"];
SPListItemCollection = yourList.GetItems(yourSPQuery);
}

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

Data returned if GetItems if query did not match

This is really a simple question. What does GetItems method return if SPQueryobject did not find any match? if i call the update method, if it did find anything. Will it add it?
I'm at the point of investigating a bug and I still don't have a environment for me to test and I'm new to SharePoint development so guys, please be gentle :D
The item will be added anyway. It doesn't matter if there are any items in the SPListItemCollection.
SPList.AddItem() uses this behavior to avoid loading all of the items in the list. One could write:
SPList list = ...
list.Items.Add();
This loads all the items in the list, which might be slow for large item sets.
SPList.AddItem() retrieves the SPListItemCollection by executing a CAML query that returns no items (ID == -1) and calls then the Add method.

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
}

How to get all items in a search scope in SharePoint 2010?

I thought I could just do something like this:
SPQuery oQuery = new SPQuery();
oQuery.ViewAttributes = "Scope='MyScope'";
SPListItemCollection collListItems = oList.GetItems(oQuery);
but it is pulling back a bunch of stuff that is not in the scope. Is there a different way I should be doing this?
You are confusing SPQuery, which a list querying class, based on CAML, and the search API. Take a look into FullTextSqlQuery, which is the class you really should work with.

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