SharePoint Search multiple lists
Customers need to filter the list of all contracts under the site conditionally, and have paging capabilities.
I found spsitedataquery on the Internet. This object can search all the lists in the site.
A bit of content source: http://www.cnblogs.com/Kenr/archive/2009/06/02/1494260.html
Web s
There is only one property for this element: Scope. There are 3 values that can be given to it, although MSDN says only 2 ...
① "<webs scope= ' sitecollection '/>"containing all Web sites (web) and subsites (web) under the site collection where the current site is located (the site)
② "<webs scope= ' Recursive '/>", including the current Web site (web) and all subsites under it (web)
③ default , contains only the current Web site (web)
Lists
This element defines the scope of your search in the document Libraries or list (Lists).
There are 4 attributes: BaseType, Servertemplate, Hidden, Maxlistlimit
In addition, there are two child elements: List, Withindex. Use child elements to limit the search criteria to a relatively small range.
The following is an introduction to attributes.
① BaseType (example: "<lists basetype= ' 1 '/>")
Define the scope of the search, the previous example searches all the document libraries, the other can set the value also has
Value |
Search scope |
Description |
0 |
Generic List |
All lists except the document library (Lists) |
1 |
Document Library |
Querying the document library only |
3 |
Discussion Forum |
Forum |
4 |
Vote or Survey |
Questionnaire Survey |
5 |
Issues List |
Discuss |
(' 2 ' is a hidden attribute ... (-"-;) ?? )
It is important to note that the default value for this property is "<lists basetype= ' 0 '/>", which means that all lists outside the document library are queried by default or when the property setting is wrong.
② servertemplate (example: "<lists servertemplate= ' 850 '/>")
Searches for a list of the specified types (850 is the page library list type). The value of this property is queried with default values when an error occurs.
For the value of Servertemplate, refer to Appendix 1
③ Hidden (example: "<lists servertemplate= ' 850 ' hidden= ' TRUE '/> ')
Sets whether the hidden list can be searched. The default is to search all non-hidden lists.
④ maxlistlimit (example: "<lists basetype= ' 1 ' maxlistslimit= '/> ')
Defines the total number of lists that are searched, and when this number is exceeded, SPSiteDataQuery throws an Spexception exception. The default value for this property is 1000. When this property is set to 0 o'clock, the number of search lists will not be restricted.
(regarding this attribute, some people say the default value is 2000, also some people say that the SP1 time has already corrected this question, how specifically to be researched ...)
As for the child elements, individuals are not very familiar with it and are not used. A brief introduction to the good.
① child elements List
(Example:<lists>
<list id= "7a9fdbe6-0841-430a-8d9a-53355801b5d5"/>
<list id= "3d18f506-fca1-451e-b645-2d720dc84fd8"/>
</Lists>)
Searches only for the list specified within the id attribute. You can use it only if you know the GUID of the search list.
② child element Withindex
(Example:<lists>
<withindex fieldid= "d4819257-6b69-41f1-82c8-a91615bff500" type= "Text" value= "complete"/>
</Lists>)
Specifies an index column for the search. The value of the Type property must be "Text", and value is the values for that column.
The search will be restricted to the list with the specified field. (You can interpret a field as a column name for the list)
Withindex has 3 properties FieldID, Value, Type. And the value of the type attribute must be "Text". In the example above, SPSiteDataQuery will search for the list item whose value is "complete" for the specified column.
ViewFields
This element defines which fields (columns of the list) are returned, that is, which columns (column) will be included in the DataTable that is the result of the search. Like what:
<fieldref name= "Title" nullable= "TRUE"/>
① Name (example: <fieldref name= "Title"/>)
The "name" Here is not the column name that we can see in our list, but the inner life of the column.
② Nullable (example: <fieldref name= "Title" nullable= "TRUE"/>).
The Nullable property is optional. We know that not all lists in Moss contain the same columns. For example, when querying a list, SPSiteDataQuery found that a column specified in the ViewFields does not exist in the list, then this will be ignored. This can be avoided when "Nullable" is specified as "TRUE", and the table will be queried, regardless of whether or not the column is present, except that the column is rendered blank in the returned DataTable.
Que ry
In this property, you can set the search criteria and sort the results. The function of this property is like an SQL statement. It's going to be a half-day talk. Let's take a look at a few examples, and the basics of SQL should be quickly understood. (Syntax reference Appendix 2)
RowLimit
Defines the number of rows returned.
Example: Q.rowlimit = 10;
There are a few problems with SPSiteDataQuery object search;
1. If the name of the column to be searched is changed, it cannot be searched;
lists that are searched with SPSiteDataQuery objects must have public fields, such as the "Contract number" field.
If the contract number for one of the lists is modified by another field, the problem will occur and the list will not be found using SPSiteDataQuery.
For example: the "title" internal name is "title", if the requirement change "title" is changed to "contract number". His internal name does not change or "Title", while the internal name of the "Contract number" field for the other list is "_x5408__x540c__x7f16__x53f7_".
If you query by following this query, then the list with "title" changed to "contract number" will not be searched.
<where><eq><fieldref name= "x5408__x540c__x7f16__x53f7_"/><value Type= "Text" >2010_1_2_213 </Value></Eq></Where>
2. Spsitedataaquery object has no paging function. If you want to split the page, you must search all the data, and then do the paging operation. All the data is searched for in-memory space.
Finally, use the SPQuery method to traverse all lists for searching.
<param name= "WEBURL" > Contract list Site </param>//<param name= "ListNames" > All contract List Names </param> <returns></returns> Public list<searchresultmodel> Getdataeachlistbiangeng (string webUrl , dictionary<string, string> listnames) {int pageSize = 30;//page size int pageIndex = ASPN Etpager1.currentpageindex; Aspnetpager1.pagesize = PageSize; int PageCount = 0;//is used for aspnetpager paging int flagpages = 0;//in order to mark items to get list<searchresultmodel> P Agedata = new list<searchresultmodel> (); using (SPWeb Web = SPContext.Current.Site.OpenWeb (webUrl)) {foreach (var item in ListNames) {SPList list = web. Lists.trygetlist (item. Key); if (list = null) {SPQuery query = new SPQuery (); Query. RowLimit = 500; Query. Query = Getquery (list); SPListItemCollection items = null; do {items = list. GetItems (query); if (Pagedata.count <= pageSize) {foreach (SPListItem listite m in items) {flagpages + = 1; if (Flagpages > (pageSize * (pageIndex-1)) && Flagpages < (pageSize * pageIndex)) {Searchresultmodel SRM = new Searchresultmodel (); Srm. Contractid = (listitem["Current contract number"]?? ""). ToString (); Srm. Contractmoney = (listitem["After the contract price changes"]?? ""). ToString (); Srm. Contractname = (listitem["contract name"]?? ""). ToString (); Srm. Duifangcompany = (listitem["After the change of the unit name"]?? ""). ToString (); Srm. Tanpanren = (listitem["contract owner"]?? ""). ToString (); Srm. Titleurl = web. Url + "/_layouts/listform.aspx?" Pagetype=4&listid= "+ list.id +" &id= "+ listItem.ID.ToString (); Pagedata.add (SRM); }}} PageCount + = items. count;//calculates the total number of pages in query. listitemcollectionposition = items. Listitemcollectionposition; } while (items. Listitemcollectionposition = null); } else {SPDiagnosticsService.Local.WriteEvent (3, New Spdiagn Osticscategory ("MSDN", traceseverity.unexpected, Eventseverity.information), eventseverity.information, "list name:" + iteM.key + ", not Found", "asdfasdf"); }}} Aspnetpager1.recordcount = PageCount; return pagedata; }
SharePoint Search multiple lists