Sharepoint lookup field Association list Query
The fuzzy search for the CAML Of The lookup field is used as an example.
1. Suppose I have two lists: the Salary list (Salary) and the Employee list (Employee ). The salary list must reference the employee ID, that is, lookup. 2. The Salary list includes Employee ID (Employee) and Salary (Salary ).
3. The Employee list contains the Employee ID (Employee), Employee name (Employee fullname), and department name (Employee Department)
4. now I need to use the CAML statement to query the Salary list and include the employee name and department. Here I need to use the list association query, as shown below:. first, I use the caml statement to query the Salary list.
SPQuery query = new SPQuery(); query.Query = @"
2000
";
Note: The statement returns the salary information with a salary greater than 2000.
B. associate it with the Employee list.
query.Joins = @"
";
Note: Specify the salary list lookup field name. RefType is always Id; If the association list is specified, the Name is always Id.
C. Associate fields referenced in the Salary list
query.ProjectedFields = @"
";
Note: ShowField specifies the employee Name field. Name is the reference Name of this field, so that FullName can be used in the returned results. D. display the required field information.
Query. ViewFields = @" ";
5. The complete code is as follows:
SPQuery query = new SPQuery(); query.Query = @"
2000
"; query.Joins = @"
"; query.ProjectedFields = @"
"; query.ViewFields = @"
"; string str = ""; using(SPSite site = new SPSite("http://dev-sp")){ SPWeb web = site.RootWeb; SPList list = web.Lists.TryGetList("Salary"); SPListItemCollection items = list.GetItems(query); foreach(SPListItem item in items){ str += item["Employee"].ToString() + ":" + item["Salary"] + "-" + item["FullName"].ToString() + "\n"; } } Console.WriteLine(str); Console.ReadLine();
6. query results:
Note: because the employee is of the lookup field type, there will be characters. Here I have not done any processing, and you can change it as needed.