1
votes

As the title says: in sharepoint 2010 i need to programmatically create a view which lets me filter on the items on a list (a list of person). In this person list i have a lookup field which refers to another list (projects): i need to show only the people that work on a determinated project (passed as a string)

I have created an example view using this code:

class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://dev_seventeen:999"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList books = web.Lists["Books"];
                    StringCollection fields = new StringCollection();
                    fields.Add("Title");
                    fields.Add("Publisher");
                    fields.Add("Autore");
                    var query = new XElement("Where",
                                    new XElement("Eq",
                                        new XElement("FieldRef", new XAttribute("Name", "Publisher")),
                                        new XElement("Value", new XAttribute("Type", "Choice"), "Alpha")
                                        )
                                    ).ToString(SaveOptions.DisableFormatting);

                    SPView view = books.Views.Add("TestView",
                        fields,
                        query,
                        100,
                        false,
                        false,
                        Microsoft.SharePoint.SPViewCollection.SPViewType.Html,
                        false
                        );
                    Console.WriteLine(query);
                    Console.ReadLine();
                }
            }
        }
    }

It filters a list named "Books" on a choice type field named "Publisher", looking for all books published by "Alpha"

What i need to know is how to filter on a lookup field instead of a chioce one, because if i just put "Lookup" instead of "Choice" in the query it doesn't work =(

Thanks

1

1 Answers

0
votes

Can you use LookupID as type? That would make sure you only have one match. Check the following pages for Lookup fields, CAML & LINQ: