Ecco come aggiungere, in
SharePoint 2007 (WSS3 - MOSS), un
Content Type (SPContentType) esistente sul sito ad una lista o document library.
//using Microsoft.SharePoint;
//add a content type to a list
string listUrl = "http://<servername>/Lists/Links";
using (SPSite site = new SPSite(listUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList(listUrl);
//get content type to add
SPContentType ctNew = web.AvailableContentTypes["Contact"];
//search content type in list
SPContentTypeId ctIdList = list.ContentTypes.BestMatch(ctNew.Id);
//add a content type if not exists
if (ctIdList == null || ctIdList.IsChildOf(ctNew.Id) == false)
{
list.ContentTypes.Add(ctNew);
//enable content type in list
list.ContentTypesEnabled = true;
//update list
list.Update();
}
}
}