views:

38

answers:

1

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

A: 

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

moontear
There must be something wrong... I have tried it once again, apparently without modifiyng anything, and it works (using regular "Lookup" as the Value Type). The strangest thing is that if i try to filter using a "Text" Value Type, i get this error when applying the view to the list: "One or more field types are not installed properly. Go to the list settings page to delete these fields." =O
Maik
About the field types error: Are you using the internal names of the columns? See this FAQ: http://www.sharepoint-tips.com/2007/04/one-of-more-field-types-are-not.html
moontear
Thanks it works now!
Maik