Domanda

I have a search form which has data in controls like checkboxlist (Multi Select), text box I need to search SharePoint list for the values and display the list records in grid view.

Please suggest me how to write the search query which is dynamic.. Ex: If there are no values selected in one text box.. I need not include it in the query.

È stato utile?

Soluzione 2

Using StringBuilder to form the query dynamically.

if (!(String.IsNullOrEmpty(implanter)))
{
      query[querycount] = "<In><FieldRef Name='Implanter' /><Values>" + implanter + "</Value></In>";
                            querycount++;
}

Altri suggerimenti

You can either run SPMetal to generate static objects to query your lists using LINQ

  1. http://msdn.microsoft.com/en-us/library/ee538255%28v=office.14%29.aspx
  2. http://zimmergren.net/technical/sp-2010-getting-started-with-linq-to-sharepoint-in-sharepoint-2010

Or just use an old and yet effective CAML Query

SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><Eq><FieldRef Name='YourField'/>" +
    "<Value Type='Text'>Insert value to search here</Value></Eq></Where>";
SPListItemCollection collListItems = oList.GetItems(oQuery);

In a straight forward linq query

var results = MySPList.Items.Cast<SPListItem>()
              .Where(SPItem => SPItem["YourField"] == "Your query");

However, running spmetal is the way to go if you plan to use linq queries frequently, because using straight linq queries on splists can result on serious performance issues.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top