Field how-tos



Create a lookup field using list URL

To create a lookup field using a source list specified by its site relative URL 'Lists/MyLookupList':

    [SPGENField(
        ID = "{EC23CA6E-BA90-443F-9F3A-6D54889298F7}",
        DisplayName = "My lookup field",
        Type = SPFieldType.Lookup,
        LookupList = "Lists/MyLookupList",
        LookupField = "Title")]
    public class MyLookupField1 : SPGENField<MyLookupField1, SPFieldLookup, string>
    {
    }


To provision this field (create the field) on a site:

    MyLookupField1.Instance.Provision(spWeb); //spWeb is a reference of the web site to create this field at.



Create a lookup field using a managed list instance

To create a lookup field using a managed list instance declared as a type:

    [SPGENField(
        ID = "{EC23CA6E-BA90-443F-9F3A-6D54889298F7}",
        DisplayName = "My lookup field1",
        Type = SPFieldType.Lookup,
        LookupList = typeof(MyLookupList),
        LookupField = "Title")]
    public class MyLookupField1 : SPGENField<MyLookupField1, SPFieldLookup, SPFieldLookupValue>
    {
    }


Where the source list instance declaration is:

    [SPGENListInstance(
        WebRelURL = "Lists/MyLookupList", 
        Title = "My lookup list", 
        TemplateType = (int)SPListTemplateType.GenericList)]
    public class MyLookupList : SPGENListInstance<MyLookupList>
    {
    }


Note! You must ensure that the lookup list already exists or is provisioned before it can be used as a lookup list in the lookup field. If the lookup field is a web scoped field (site column), the lookup list instance must exist on the same site. If the field is a list field, the lookup list must exist on the same site as the list the field belongs to.

To provision this field (create the field) on a site:

    //Create the dependent lookup list first.
    MyLookupList.Instance.Provision(spWeb); //spWeb is a reference of the web site to create this field at.

    //Create the lookup field.
    MyLookupField1.Instance.Provision(spWeb);



Create a lookup field using a managed list instance with a managed field as lookup field

To create a lookup field using a lookup list declared as a managed list instance, and a show field which is also a managed field in the lookup list:

    [SPGENField(
        ID = "{EC23CA6E-BA90-443F-9F3A-6D54889298F7}",
        DisplayName = "My lookup field1",
        Type = SPFieldType.Lookup,
        LookupList = typeof(MyLookupList),
        LookupField = typeof(MyLookupList.MyShowField)]
    public class MyLookupField1 : SPGENField<MyLookupField1, SPFieldLookup, SPFieldLookupValue>
    {
    }


Where the source list instance declaration is:

    [SPGENListInstance(
        WebRelURL = "Lists/MyLookupList", 
        Title = "My lookup list", 
        TemplateType = (int)SPListTemplateType.GenericList)]
    public class MyLookupList : SPGENListInstance<MyLookupList>
    {
        protected override void InitializeDefinition(SPGENListInstanceProperties properties)
        {
            properties.Fields.Add<MyShowField>(true, true);
        }

        [SPGENField(
            ID = "{EC23CA6E-BA90-443F-9F3A-6D54889298F9}",
            DisplayName = "My show field",
            Type = SPFieldType.Text)]
        public class MyShowField : SPGENField<MyShowField, SPFieldText, string>
        {
        }
    }


Note! You must ensure that the lookup list already exists or is provisioned before it can be used as a lookup list in the lookup field. If the lookup field is a web scoped field (site column), the lookup list instance must exist on the same site. If the field is a list field, the lookup list must exist on the same site as the list the field belongs to.

To provision this field (create the field) on a site:

    //Create the dependent lookup list first.
    MyLookupList.Instance.Provision(spWeb); //spWeb is a reference of the web site to create this field at.

    //Create the lookup field.
    MyLookupField1.Instance.Provision(spWeb);

Last edited Mar 30, 2011 at 2:10 PM by tore7506, version 5

Comments

No comments yet.