Welcome to DetlefGrohs.com Sign in | Join | Help

Adding a Lookup Field to a SharePoint 2007 List Code Snippet

This code snippet will add a lookup field to a list in SharePoint 2007. Change the false in the AddLookup call to true if you want to make it a required field. I will be posting a long post on how to add or modify any field type through the API in SharePoint.

        SPList list = spweb.Lists["MyList"];

        list.Fields.AddLookup("MyLookupField", spweb.Lists["MyLookupList"].ID, false);
        SPFieldLookup field = (SPFieldLookup)list.Fields["MyLookupField"];
        field.LookupField = "MyLookupListField";
        field.Update();
        list.Update(); 
Published Wednesday, September 26, 2007 9:27 AM by detlef
Filed Under:

Comments

No Comments
Anonymous comments are disabled