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

1 Answer 1

0

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

2
  • 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
    Oct 15, 2010 at 10:31
  • About the field types error: Are you using the internal names of the columns? See this FAQ: sharepoint-tips.com/2007/04/…
    – Dennis G
    Oct 16, 2010 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.