Sharepoint lookup field Association list Query

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.