Converting SQL Query to XML Query for SharePoint List - sql

I'm using SQL Reporting 3.0 and I'm trying to query a SharePoint list. Since the SharePoint list can only be in XML inside of SQL reporting, I'm trying to figure out to to convert my SQL query into an XML query. The area I'm stuck at is inside of WHERE. I'm having difficulty with IN(#). Is there a way to convert this into an XML query? Thank you for your help its greatly appreciated.
SQL Query
SELECT Hours, Date, Project, ID, Created_By,Month
FROM TimeTracking
WHERE Project IN(#Project) AND Month IN(#Month)
XML Query
<RSSharePointList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ListName>Time Tracking</ListName>
<ViewFields>
<FieldRef Name="Hours" />
<FieldRef Name="Date" />
<FieldRef Name="Project" />
<FieldRef Name="ID" />
<FieldRef Name="Created_By" />
<FieldRef Name="Month" />
</ViewFields>
<Query>
<Where>
?
</Where>
</Query>
</RSSharePointList>

Here's an example on MSDN on using FOR XML PATH to return your query as XML. You can also add a root element by using ROOT.
Please refer to te following article:
http://msdn.microsoft.com/en-us/library/bb510462.aspx

Related

SharePoint CAML Query AND/OR operators

I would appreciate any help on getting my CAML query to work.
I have the following list:
| ID | Department | Status |
I am trying to get the query to return list items where ID=1, Department=Audit, and Status= "Stopped" OR "In Progress".
Can anyone please help?
Regards,
Query:
<Query>
<Where>
<And>
<Eq>
<FieldRef Name='ID' />
<Value Type='Counter'>1</Value>
</Eq>
<And>
<Eq>
<FieldRef Name='Department' />
<Value Type='Choice'>Audit</Value>
</Eq>
<Or>
<Eq>
<FieldRef Name='Status' />
<Value Type='Choice'>In Progress</Value>
</Eq>
<Eq>
<FieldRef Name='Status' />
<Value Type='Choice'>Not Started</Value>
</Eq>
</Or>
</And>
</And>
</Where>
</Query>
Note: it is assumed that Department is a Choice field
I would recommend you to utilize the tool that will help you build your CAML Queries, for example U2U Caml Query Builder

How to get detch data from List which is inside a folder?

I'm using SSRS for generating reports, have created the data source and dataset, and I'm able to retrieve the data. However, some items which are inside the folder are not getting displayed in report.
It's not fetching items inside a report.
Dont know how to mention scope here for query. Below is my query:
<RSSharePointList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ListName>Program</ListName>
<ViewFields>
<FieldRef Name="Title" />
<FieldRef Name="Requestor" />
</ViewFields>
</RSSharePointList>

SP 2010 Query CAML - How to sort by multivalue colum lookup field?

I'm trying to sort a list where a column is based on multiple value on a lookup field with the following statement:
<OrderBy>
<FieldRef Name='LookupFieldName' Ascending='TRUE' LookupValue='TRUE' />
</OrderBy>
If I uncheck "Allow multiple values" from column settings it works fine, but not with multiple values enabled.
Have you some suggestions?
Why SP can't sort on concatenation of ID#Value (eg. 1#Value1;2#Value2) ?
Thanks
You should be able to treat the addtional columns as any other column. In this example, Attorney1_x003a_EmpID is one of the multiple values:
<Query>
<OrderBy>
<FieldRef Name="Attorney1" Ascending="False" />
<FieldRef Name="Attorney1_x003a_EmpID" Ascending="False" />
</OrderBy>
</Query>

isnull and in statement in CAML

Is it possible to combine the sharepoint <In> statement with the <IsNull> statement? I only managed to get it working using an <Or> but would be great if you could place the <isNull> inside the <In> directly.
Thanks
The solution is as follows:
<Query>
<Where>
<In>
<FieldRef Name='MyLookupField' LookupId='TRUE'/>
<Values>
<Value Type='Lookup'></Value>
<Value Type='Lookup'>1</Value>
</Values>
</In>
</Where>
</Query>

SOAP call with query on result (SSRS, Sharepoint)

I created a report in VS using a shared data source which is connected to a sharepoint list. In the report I created a dataset with a SOAP call to the data source so I get the result from the sharepoint list in a table.
this is the soap call
<Query>
<SoapAction>http://schemas.microsoft.com/sharepoint/soap/GetListItems</SoapAction>
<Method Namespace="http://schemas.microsoft.com/sharepoint/soap/" Name="GetListItems">
<Parameters>
<Parameter Name="listName">
<DefaultValue>{BD8D39B7-FA0B-491D-AC6F-EC9B0978E0CE}</DefaultValue>
</Parameter>
<Parameter Name="viewName">
<DefaultValue>{E2168426-804F-4836-9BE4-DC5F8D08A54F}</DefaultValue>
</Parameter>
<Parameter Name="rowLimit">
<DefaultValue>9999</DefaultValue>
</Parameter>
</Parameters>
</Method>
<ElementPath IgnoreNamespaces="True">*</ElementPath>
</Query>
THis works fine, I have a result which I can show in a report, but I want to have the ability to select a parameter to filter the result on. I have created a parameter and when I preview the Report I see the dropdownbox which I can use to make a selection from the Title field, when I do this it still shows the first record, obviously it doens't work yet (DUH!) because I need to create a query somewhere, But! I have no idea where, I tried to include
<Where>
<Eq>
<FieldRef Name="ows_Title" />
<Value Type="Text">testValue</Value>
</Eq>
</Where>
in the the soap request but it didn't worked... I've searched teh intarwebz but couldn't find any simliar problems... kinda stuck now...any thoughts on this?
EDIT
Here's the query I used according to the blogpost Alex Angas linked.
<Query>
<SoapAction>http://schemas.microsoft.com/sharepoint/soap/GetListItems</SoapAction>
<Method Namespace="http://schemas.microsoft.com/sharepoint/soap/" Name="GetListItems">
<queryOptions></queryOptions>
<query><Query>
<Where>
<Eq>
<FieldRef Name="ows_Title"/>
<Value Type="Text">someValue</Value>
</Eq>
</Where>
</Query></query>
<Parameters>
<Parameter Name="listName">
<DefaultValue>{BD8D39B7-FA0B-491D-AC6F-EC9B0978E0CE}</DefaultValue>
</Parameter>
<Parameter Name="viewName">
<DefaultValue>{E2168426-804F-4836-9BE4-DC5F8D08A54F}</DefaultValue>
</Parameter>
<Parameter Name="rowLimit">
<DefaultValue>9999</DefaultValue>
</Parameter>
</Parameters>
</Method>
<ElementPath IgnoreNamespaces="True">*</ElementPath>
</Query>
I tried to put the new query statement in every possible way in the existing, but it doesn't work at all, I do not get an error though so the code is valid, but I still get an unfiltered list as return... pulling my hair out here!
A post at:
http://social.msdn.microsoft.com/forums/en-US/sqlreportingservices/thread/1562bc7c-8348-441d-8b59-245d70c3d967/
Suggested using this syntax for placement of the <Query> node (this example is to retrieve the item with an ID of 1):
<Query>
<SoapAction>http://schemas.microsoft.com/sharepoint/soap/GetListItems</SoapAction>
<Method Namespace="http://schemas.microsoft.com/sharepoint/soap/" Name="GetListItems">
<Parameters>
<Parameter Name="listName">
<DefaultValue>{CE7A4C2E-D03A-4AF3-BCA3-BA2A0ADCADC7}</DefaultValue>
</Parameter>
<Parameter Name="query" Type="xml">
<DefaultValue>
<Query>
<Where>
<Eq>
<FieldRef Name="ID"/>
<Value Type="Integer">1</Value>
</Eq>
</Where>
</Query>
</DefaultValue>
</Parameter>
</Parameters>
</Method>
<ElementPath IgnoreNamespaces="True">*</ElementPath>
</Query>
However this would give me the following error:
Failed to execute web request for the specified URL
With the following in the details:
Element <Query> of parameter query is missing or invalid
From looking at the SOAP message with Microsoft Network Monitor, it looks as though the <Query> node is getting escaped to <Query> etc, which is why it fails.
However, I was able to get this to work using the method described in Martin Kurek's response at:
http://www.sharepointblogs.com/dwise/archive/2007/11/28/connecting-sql-reporting-services-to-a-sharepoint-list-redux.aspx
So, I used this as my query:
<Query>
<SoapAction>http://schemas.microsoft.com/sharepoint/soap/GetListItems</SoapAction>
<Method Namespace="http://schemas.microsoft.com/sharepoint/soap/" Name="GetListItems">
<Parameters>
<Parameter Name="listName">
<DefaultValue>{CE7A4C2E-D03A-4AF3-BCA3-BA2A0ADCADC7}</DefaultValue>
</Parameter>
<Parameter Name="query" Type="xml">
</Parameter>
</Parameters>
</Method>
<ElementPath IgnoreNamespaces="True">*</ElementPath>
</Query>
And then defined a parameter on the dataset named query, with the following value:
<Query><Where><Eq><FieldRef Name="ID"/><Value Type="Integer">1</Value></Eq></Where></Query>
I was also able to make my query dependent on a report parameter, by setting the query dataset parameter to the following expression:
="<Query><Where><Eq><FieldRef Name=""ID""/><Value Type=""Integer"">" &
Parameters!TaskID.Value &
"</Value></Eq></Where></Query>"
See the question and answers for GetListItems Webservice ignores my query filter. This shows you how (and how not to) set up your SOAP call to include a query. You probably need to wrap your query with another <Query></Query>.
You have your FieldRef as
ows_Title
I believe it should just be Title.
When you get results from the SOAP request all your field name will begin with
ows_
Brilliant, thanks. This solution worked for queryOptions also.
In the Query:
<Parameter Name="queryOptions" Type="xml">
</Parameter>
And in the parameters list of the dataset:
Name: queryOptions
Value: <QueryOptions><Folder>Shared Documents/MyFolder</Folder></QueryOptions>