SharePoint - Join two SharePoint list in CAML Query

Asked By Sumit
28-Jul-11 07:35 AM
Hi

I have to join two lists  in CAML Query to get data from them. I am using MOSS 2007
Please suggest way to do this.

Thanks & Regards
Sumit Kour
  Reena Jain replied to Sumit
28-Jul-11 07:36 AM
Hi,

To do a CAML query in a SharePoint list you can use the class SPQuery. Here's an example.

Code:

try
 {
 SPSite site = new SPSite("serveur_url");
SPWeb web = new site.OpenWeb();
 SPList list = web.Lists["votre_liste"];
SPQuery query = new SPQuery(list.DefaultView);
 query.Query = " Une Valeur ";
SPListItemCollection items = list.GetItems(query);
 foreach (SPListItem item in items)
 {
Console.WriteLine(item["Title"]));
 }
 }

catch(SPException SpEx) { //Traitement d'erreur }

Here we use the operator "<Eq>, we could eg use <Contains a search corresponding to a SQL LIKE. You must clearly specify the internal name of the field list and not the title "Title". There are also graphical tools to generate CAML queries. A little research on CAML Query Builder on google guide you to this type of tool.

To specify which fields to include in a CAML query

Code:

 try
{
SPSite site = new SPSite("serveur_url");
SPWeb web = new site.OpenWeb();
 SPList list = web.Lists["votre_liste"];
SPQuery query = new SPQuery(list.DefaultView);
 query.Query = " Une Valeur ";
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items)
{
Console.WriteLine(item["Title"]));
}
 }
 
catch(SPException SpEx) { //Traitement d'erreur }

In the previous example, we specify the view "DefaultView". We could have used a view defined in the list. It is also possible to specify fields to include as follows:

Code:

 query.ViewFields = "<FieldRef Name='Champ1'/><FieldRef Name='Champ2'/>....";

Follow these libks-

http://rmanimaran.wordpress.com/2011/03/11/new-in-sharepoint-2010-caml-query/

http://praveenbattula.blogspot.com/2010/02/sharepoint-querying-caml-query-usage.html


Hope this will help you
  Vickey F replied to Sumit
28-Jul-11 07:40 AM
Follow this link-

http://www.eggheadcafe.com/community/aspnet/69/10331958/caml-query.aspx



CAML (Collaborative Application Markup Language) is an XML-based query language that helps you querying, building and customizing Web sites based on Windows SharePoint Services. The XML elements define various aspects of a WSS site.

here are link to download this

http://www.u2u.net/res/Tools/CamlQueryBuilder.aspx
http://software.informer.com/getfree-caml-query-builder-sharepoint/

to use this here are the links

http://www.zimmergren.net/archive/2008/05/05/how-to-easily-construct-your-caml-queries-with-spquery.aspx
http://www.codeproject.com/KB/sharepoint/SPQueryBuilderDialog.aspx

hope this will help you
  Riley K replied to Sumit
28-Jul-11 07:41 AM
You have two options for joining lists


1. You can use SPSiteDataQuery to fetch data from multiple list into a single datasource. You need to provide all the ListId and CAML query will return you a single datatable with the data from all list

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsitedataquery.lists.aspx

http://blog.thekid.me.uk/archive/2007/02/27/spsitedataquery-samples-for-wss-v3.aspx


2. Second, you can also use LINQ

http://msdn.microsoft.com/en-us/library/ee538250.aspx


Hope this info will help you

  James H replied to Sumit
28-Jul-11 08:25 AM

It's not possible to do a formal "Join" like you would using straight SQL, it just isn't supported by straight CAML.

You can use SPQuery.Joins: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.joins(office.14).aspx

  Radhika roy replied to Sumit
28-Jul-11 11:00 AM

You can Use CAML Query in sharepoint like this-

follow this example-

If you need to retrieve items from a list when developing web parts, application pages or custom field types you can best use theSPQueryobject from the SharePoint object model. This object is located in the Microsoft.SharePoint namespace of theMicrosoft.SharePoint.dll located in the Global Assembly Cache.

Instantiate the object as follows:

SPQuery qry = new SPQuery();

The most important property is the Query property, which needs to be set to your CAML query:

string
camlquery = "<OrderBy><FieldRef Name='Country'
/></OrderBy><Where>"
    + "<Eq><FieldRef Name='LastName'
/><Value
Type='Text'>Smith</Value></Eq>"
    + </Where>";
qry.Query = camlquery;

At this point you can execute the query on your list:

SPListItemCollection listItemsCollection =
list.GetItems(qry);

A small remark with the GetItems method of the SPList instance: this method returns a collection of type SPListItemCollection. It is possible that it is easier working with a DataTable. In that case you can execute the query as follows:

DataTable listItemsTable =
list.GetItems(qry).GetDataTable();



Follow this link also-



http://sharepointmagazine.net/articles/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list

 

Hope this will help you

Create New Account
help
Setup MS Office SharePoint 2007 Test / Dev Environment Authentication Problems Hello all, i like to setup a fast clonable test / development installation of MOSS2007 under Windows 2008 SP1 (not R2) (32bit). As starting point I have choosen the Blog post from has dsiplay some warning, regarding the installation of MSSQL on a Domain Controller. My single SharePoint admin account is named SPAdmin (domain admin, mssql: security, dbcreator), the local domain ist named OWSTIMER.EXE, onetnative.dll) 03 / 04 / 2010 13:53:04.95 OWSTIMER.EXE (0x0980) 0x0988 Windows SharePoint Services Topology 0 Medium Diagnostics settings: 32768 03 / 04 / 2010 13:53:05.38 OWSTIMER.EXE (0x0980) 0x0988 Windows SharePoint Services Topology 9e7d Medium Initializing the configuration database connection. 03 / 04 / 2010 13:53:11
How add Excel Pivot Table Reports in Windows Sharepoint Services SharePoint How to add Excel Pivot Table Reporting in Windows SharePoint Services. SharePoint Portal Server Discussions Windows SharePoint Services (1) SharePoint (1) Office 2003 (1) Excel (1) Shetakeprashant
installation of web parts Hello Everyone, We have a sharepoint server configured in the following way: Server1 has: Central Administration Office SharePoint Server Search Windows SharePoint Services Help Search Windows SharePoint Services Incoming E-Mail Windows SharePoint Services Web Application Server2 : Windows SharePoint Services Incoming E-Mail
Windows Sharepoint Services Administration service timeout SharePoint I'm noticing in the event log that this service is unable to start (Windows Sharepoint Services Administration or SPAdmin). I'm getting timeouts. 'Timeout (30000 milliseconds) waiting for the Windows SharePoint Services Administration service to connect' and 'The Windows SharePoint Services Administration service failed to
Windows SharePoint Services Adapter Windows Server hi I want: 1. BizTalk orchestration send message to a Windows SharePoint Services 2. user in SharePoint approve item 3. BizTalk orchestration receive message from a Windows SharePoint Services in a same