Content types how-tos



Create a managed content type with field links

To create a managed content type with two field links PercentageComplete and TaskStatus (build-in fields in SP), start by declaring the content type definition:

    [SPGENContentType(
        ID = "0x0100574F3398A6A141B4A6E0F56CF5B58C1B",
        Name = "My content type 1",
        Group = "SP Genesis Demo")]
    public class MyContentType1 : SPGENContentType<MyContentType1>
    {
        //Here we declare our field links for our content type.
        protected override void InitializeDefinition(SPGENContentTypeProperties properties)
        {
            //This property tells the framework to remove all undeclared field links for this content type. It will keep inherited field links only, 
            //in this case, title field from 0x01 Item content type.
            properties.FieldLinks.ProvisioningMode = SPGENProvisioningMode.ExclusiveAdd;

            properties.FieldLinks.Add(SPBuiltInFieldId.PercentComplete);
            properties.FieldLinks.Add(SPBuiltInFieldId.TaskStatus);
        }
    }


To provision this content type to a site, that means create or update the content type:

    MyContentType1.Instance.Provision(spWeb); //spWeb is a reference to a site



Create a managed content type with managed field links

To create a content type with two managed field links MyTextField and MyNumberField, start by declaring the managed field definitions:

    [SPGENField(
        ID = "{5DAE37A4-67D1-4D83-95C4-2833373E3B27}",
        DisplayName = "My text field",
        Type = SPFieldType.Text,
        Group = "SP Genesis Demo")]
    public class MyTextField : SPGENField<MyTextField, SPFieldText, string>
    {
    }

    [SPGENField(
        ID = "{9F6AAA82-166E-49A0-83A6-AC3212ACF666}",
        DisplayName = "My number field",
        Type = SPFieldType.Number,
        Group = "SP Genesis Demo")]
    public class MyNumberField : SPGENField<MyNumberField, SPFieldNumber, double?>
    {
    }


Declare the managed content type and add the two fields to the definition:

    [SPGENContentType(
        ID = "0x0100574F3398A6A141B4A6E0F56CF5B58C1C",
        Name = "My content type 2",
        Group = "SP Genesis Demo")]
    public class MyContentType2 : SPGENContentType<MyContentType2>
    {
        //Here we declare our field links for our content type.
        protected override void InitializeDefinition(SPGENContentTypeProperties properties)
        {
            //This property tells the framework to remove all undeclared field links for this content type. It will keep inherited field links only, 
            //in this case, title field from 0x01 Item content type.
            properties.FieldLinks.ProvisioningMode = SPGENProvisioningMode.ExclusiveAdd;

            properties.FieldLinks.Add<MyTextField>();
            properties.FieldLinks.Add<MyNumberField>();
        }
    }


To provision this content type to a site, that means create or update the content type:

    //We must provision the fields that will be used as field links inside our content type first.
    MyTextField.Instance.Provision(spWeb); //spWeb is a reference to a site
    MyNumberField.Instance.Provision(spWeb)

    //Now we can go ahead and provision our content type.
    MyContentType2.Instance.Provision(spWeb)



Create a managed content type with managed field links and add it to a managed list instance

To create a managed content type with managed field links and add it to a managed list instance, start by repeating the Create a managed content type with managed field links how-to.

To add it to a list, start by declaring our managed list instance and add the MyContentType2 content type to the list definition:

    [SPGENListInstance(
                WebRelURL = "Lists/MyList1",
                Title = "My list 1",
                TemplateType = (int)SPListTemplateType.GenericList,
                OnQuickLaunch = true,
                ContentTypesEnabled = true)]
    public class MyList1: SPGENListInstance<MyList1>
    {
        //Here we declare that this list instance will use our content type
        protected override void InitializeDefinition(SPGENListInstanceProperties properties)
        {
            //This property tells the framework to remove all undeclared field links for this content type. It will keep inherited field links only, 
            //in this case, title field from 0x01 Item content type.
            properties.ContentTypes.ProvisioningMode = SPGENProvisioningMode.ExclusiveAdd;

            properties.ContentTypes.Add<MyContentType2>();
        }
    }


To provision the list instance to a site, that means create or update the list instance, do this:
(You must have completed all the steps in the previous how-to for this to work.)

    MyList1.Instance.Provision(web);



Create a managed content type with field links added only to a list content type

If you want to add the managed field links to the list content type only instead of adding them to the site content type, you can declare it inside the list instance.
To acomplish this, start by looking back at the managed content type we declared in the previous how-to and remove the field links.

    [SPGENContentType(
        ID = "0x0100574F3398A6A141B4A6E0F56CF5B58C1C",
        Name = "My content type 2",
        Group = "SP Genesis Demo")]
    public class MyContentType2 : SPGENContentType<MyContentType2>
    {
        //Here we declare our field links for our content type.
        protected override void InitializeDefinition(SPGENContentTypeProperties properties)
        {
            //We will add our field links directly inside the list content type in this demo.
            //properties.FieldLinks.ProvisioningMode = SPGENProvisioningMode.ExclusiveAdd;

            //properties.FieldLinks.Add<MyTextField>();
            //properties.FieldLinks.Add<MyNumberField>();
        }
    }


Ensure that you have the same field declarations as the previous how-to:

    [SPGENField(
        ID = "{5DAE37A4-67D1-4D83-95C4-2833373E3B27}",
        DisplayName = "My text field",
        Type = SPFieldType.Text,
        Group = "SP Genesis Demo")]
    public class MyTextField : SPGENField<MyTextField, SPFieldText, string>
    {
    }

    [SPGENField(
        ID = "{9F6AAA82-166E-49A0-83A6-AC3212ACF666}",
        DisplayName = "My number field",
        Type = SPFieldType.Number,
        Group = "SP Genesis Demo")]
    public class MyNumberField : SPGENField<MyNumberField, SPFieldNumber, double?>
    {
    }


Then modify the managed list instance:

    [SPGENListInstance(
                WebRelURL = "Lists/MyList1",
                Title = "My list 1",
                TemplateType = (int)SPListTemplateType.GenericList,
                OnQuickLaunch = true,
                ContentTypesEnabled = true)]
    public class MyList1: SPGENListInstance<MyList1>
    {
        //Here we declare that this list instance will use our content type
        protected override void InitializeDefinition(SPGENListInstanceProperties properties)
        {
            //This property tells the framework to remove all undeclared field links for this content type. It will keep inherited field links only, 
            //in this case, title field from 0x01 Item content type.
            properties.ContentTypes.ProvisioningMode = SPGENProvisioningMode.ExclusiveAdd;

            //Add our managed content type to the list content types collection and get the reference to it.
            var ct = properties.ContentTypes.Add<MyContentType2>();

            //Add the field links like the previous how-to:s
            ct.FieldLinks.ProvisioningMode = SPGENProvisioningMode.ExclusiveAdd;
            ct.FieldLinks.Add<MyTextField>();
            ct.FieldLinks.Add<MyNumberField>();
        }
    }


To provision the list instance to a site, that means create or update the list instance, do this:

    //We must provision the fields that will be used as field links inside our content type first.
    MyTextField.Instance.Provision(spWeb); //spWeb is a reference to a site
    MyNumberField.Instance.Provision(spWeb)

    //Now we can go ahead and provision our content type.
    MyContentType2.Instance.Provision(spWeb)

    //Finally, provision the list instance.
    MyList1.Instance.Provision(web);

Last edited Apr 1, 2011 at 8:45 AM by tore7506, version 12

Comments

No comments yet.