none
Using ProjectProperty in CAML where clause RRS feed

  • Question

  • My question is simple: is it possible to query an item including the ProjectProperty fields? I've tried and concluded that it most likely isn't possible, but I want to know for sure. Thanks.
    MCTS in Web Application Development in .NET 2.0
    Tuesday, October 5, 2010 11:56 AM

Answers

All replies

  • Here have example using joins and projected fields could answer your question.

    This example shows how to use joins and projections. It is a console application that uses two
    lists: Books and Authors. The Books list relates to Authors through a field called LeadAuthor. The Authors
    list derives from Contacts and has an additional field called FullName. 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using System.Xml;
    namespace Apress.SP2010.CAMLJoins
    {
     class Program
     {
      static void Main(string[] args)
      {
       using (SPSite site = new SPSite("http://Dev01"))
       {
        using (SPWeb web = site.OpenWeb())
        {
         SPList bookList = web.Lists["Books"];
         SPList authList = web.Lists["Authors"];
         SPField la = bookList.Fields["LeadAuthor"];
         SPField fa = authList.Fields["Full Name"];
         string join = @"<Join Type='LEFT' ListAlias='Authors'>
            <Eq>
            <FieldRef Name='" + la.InternalName
                 + @"' RefType='ID' />
            <FieldRef List='Authors' Name='ID' />
            </Eq>
            </Join>";
                 string projField = @"<Field Name='Fullname' Type='Lookup'
            List='Authors'
            ShowField='" + fa.InternalName + "' />";
         SPQuery query = new SPQuery();
         query.Query = "";
         query.Joins = join;
         query.ProjectedFields = projField;
         query.ViewFields = @"<FieldRef Name='Fullname' />
    <FieldRef Name='Title' />";
         SPListItemCollection items = bookList.GetItems(query);
         foreach (SPListItem item in items)
         {
          Console.WriteLine("{0} has these lead authors: ",
          item.Title);
          if (item["Fullname"] == null)
          {
           Console.WriteLine(" no authors assigned");
           continue;
          }
          SPFieldLookupValue sc =
          new SPFieldLookupValue(item["Fullname"].ToString());
          Console.WriteLine(" - {0}", sc.LookupValue);
         }
        }
       }
       Console.ReadLine();
      }
     }
    }
    
    The join is from the parent list Books to the child list Authors. This is defined by List='Authors' in the join’s second FieldRef element. The ListAlias attribute must echo this, as it is the only join. The first FieldRef refers to the parent list’s lookup field, using the internal name. It’s read from the SPField object that has a property InternalName.

    The variable pfld stores the projected fields. The declaration contains one field, FullName, from the Authors list. ShowField is also defined using the internal name. The name defined in the Name attribute is used in the ViewField definition. The Title field is also added to the result set. The XML snippets are assigned to the appropriate properties of SPQuery.

    The GetItems method executes the query. Because item["Fullname"] returns a lookup, the value it returns is in the form of “#1;Joerg Krause.” To process it, we create an  SPFieldLookupValue object,passing the data as a string into the constructor to re-create the lookup value. From this object the LookupValue returns the full name.
    • Edited by Michael-Zhang Thursday, October 21, 2010 6:54 AM font
    Thursday, October 21, 2010 6:52 AM
  • Answer: using these fields in the where query part isn't possible.
    MCTS in Web Application Development in .NET 2.0
    Thursday, October 28, 2010 3:21 PM