2011-08-18 55 views

回答

1

您可以通過代碼將內容類型與列表相關聯。我總是這樣做用這種方法:

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(); 
     } 
    } 

您可以在一個功能接收器使用此功能,例如:

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