COB-CSU-AM
COB-CSU-AM

Reputation: 61

Creating a list definition from two content types (SharePoint)

I'm currently using SharePoint 2010 and Visual Studio 2010. VS 2010 makes adding a list definition really easy through a dialog box, but only allows adding just one content type through the dialog box, however I need to add two, how can I do so?

Thanks in advance!

Cheers!

Upvotes: 1

Views: 575

Answers (1)

alf
alf

Reputation: 18540

You can associate the content types with the list through code. I always do this with this method:

    private void VerifyListContentTypeAssociation(SPList list, string contentType)
    {
        SPContentTypeId contentTypeId = new SPContentTypeId(contentType);
        list.ContentTypesEnabled = true;
        SPContentTypeId matchContentTypeId = list.ContentTypes.BestMatch(contentTypeId);

        if (matchContentTypeId.Parent.CompareTo(contentTypeId) != 0)
        {
            SPContentType ct = list.ParentWeb.AvailableContentTypes[contentTypeId];
            list.ContentTypes.Add(ct);
            list.Update();
        }
    }

You can use this function in a feature receiver for example:

string contentTypeID = "0x010056eb9d8ddb324c92865eceef8a97c811";
SPList myList = web.Lists["MyList"];
VerifyListContentTypeAssociation(myList, contentTypeID);

Upvotes: 1

Related Questions