SharePoint - How to customize Sharepoint search using CAML Query

Asked By Alok
05-Jul-11 07:46 AM
Hi

How to customize Sharepoint search using CAML Query .
in my project we want to use content level search ((inside the document).
waiting for helpful response.
  TSN ... replied to Alok
05-Jul-11 07:51 AM
hi..

Look at this links,...

http://abstractspaces.wordpress.com/2008/05/05/caml-query-lookup-field-by-id-not-by-value/

hope this helsp you/...
  Reena Jain replied to Alok
05-Jul-11 08:07 AM
hi,

SPQuery is the SharePoint object which is used to perform a query operation against SharePoint data.SPList.getItems(SPQuery) is the step, will return SPListItemCollection which satisfies the query.SPQuery has one data member ‘Query’, which need to set before passing SPQuery object to SPList.

try this
using (SPWeb web = SPContext.Current.Site.RootWeb)
{
 
  SPList mylist = web.Lists["Tasks"];
  SPQuery query = new SPQuery();
  query.Query = "<Where><Eq><FieldRef Name='Status'/>" +
       "<Value Type='Text'>Completed</Value></Eq></Where>";
  SPListItemCollection items = mylist.GetItems(query);
  foreach (SPListItem item in items)
  {
  Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
  }
}

hope this will help you
  Reena Jain replied to Alok
05-Jul-11 08:10 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'/>....";

Hope this will help you
  Vickey F replied to Alok
05-Jul-11 09:46 AM

If you need to retrieve items from a list when developing web parts, application pages or custom field types you can best use the SPQueryobject from the SharePoint object model. This object is located in the Microsoft.SharePoint namespace of the Microsoft.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();



Hope this will help you.
  Riley K replied to Alok
05-Jul-11 09:53 AM
You have asked this question almost thrice and you got good answers

check these links  where you posted the same question


http://www.eggheadcafe.com/community/aspnet/69/10320267/how-to-customize-sharepoint-search-using-caml-query-.aspx

http://www.eggheadcafe.com/community/aspnet/69/10323539/how-to-customize-sharepoint-search-content-level-using-caml-query.aspx

Did you try to implement what my pals suggested at these links

if you tried and still did not get post the error where you are getting

All the Best
Create New Account
help
Search In document Library SharePoint Dear All, I want to search contents in Document Library using CAML. I have tried UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.Utilities; public partial class SPGridExample : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs to display contents of one list SPList list = web.Lists["assignments"]; / / generateGrid(spGrid, list.Items.GetDataTable().DefaultView); #endregion #region Using SPGridView to display contents of several lists SPWeb rootWeb = SPContext.Current String.Format("<Where> <Gt> <FieldRef Name = 'StartDate' / > " + Gt> < / Where> ", SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.UtcNow.AddDays(-5))); DataTable dt = rootWeb.GetSiteData(query); DataView dv = new DataView(dt); generateGrid(spGridCrossWeb, dv); #endregion } protected void any other code to seach contents in Dcoument Library Regards, Kamlesh ( kamlesh.k73@gmail.com) SharePoint Windows Services Discussions SPUtility.CreateISO8601DateTimeFromSystemDateTime (1) SPContext.Current.Site.RootWeb (1) SPContext.Current.Web (1
any tasks for the current User in home page. I have a list in http: / / sharepoint:231 / software / documents / default.aspx , and I am adding users task webpart in home page; http: / / sharepoint:231 / , but the user task webpart does not shows any tasks for the current user When I am add the same webpart in http: / / sharepoint:software / , there the webpart is work fine. I need the same webpart in home page 0}:SPWebPart runat = server> < / {0}:SPWebPart> " ), XmlRoot (Namespace = "sa.WebParts.WebPartToDisplayGroupTasks" )] public class SPWebPart : Microsoft.SharePoint.WebPartPages. WebPart { private const EnumStatus _defaultStatus = EnumStatus .NotCompleted; private EnumStatus _enumStatus = _defaultStatus; GridView GridView1 = new value ; } } public SPWebPart() { this .Title = “Webpart to display Group Tasks” ; } protected override void CreateChildControls() { Microsoft.SharePoint. SPList listSiteSetup = SPContext .Current.Web.Lists[ "Tasks" ]; Microsoft.SharePoint. SPQuery query = new Microsoft.SharePoint. SPQuery ();Microsoft.SharePoint. SPListItemCollection items = listSiteSetup.Items; DataTable dtTasks = items.GetDataTable(); DataColumn dtLink = new DataColumn ( “link” ); dtTasks.Columns
Sharepoint Development I am not able to add an item to a list in a sharepoint site through programming. i am able to access the data in the list and i AllowUnsafeUpdates = true; SPList issue = web.Lists["Issues"]; SPListItemCollection ilistitems = issue.Items; DataSet ids = new DataSet(); DataTable idt = new DataTable("ilistdata"); idt = ilistitems.GetDataTable(); DataGrid2.DataSource = idt; DataGrid2.DataBind(); You could use this to create a sort of task
Join two SharePoint list in CAML Query Hi I have to join two lists in CAML Query to to do this. Thanks & Regards Sumit Kour Hi, To do a CAML query in a SharePoint list you can use the class SPQuery. Here's an example. Code: try { SPSite site 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 Follow this link- http: / / www 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 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 You have two options for joining lists 1 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