2011-08-18 79 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); 
相关问题