I have an event handler that catches an ItemAdding event and changes the content type on the fly. This is my code to build the content type:
SPContentType baseContentType = web.AvailableContentTypes["Folder"];
SPContentType type = new SPContentType(baseContentType, web.ContentTypes, "Custom Folder");
// Add fields to new content type
AddField(ref type, "User", "Owner");
AddField(ref type, "DateTime", "Date");
AddField(ref type, "Text", "Description");
AddField(ref type, "Text", "Justification");
AddField(ref type, "User", "Users");
list.ContentTypes.Add(type);
list.Update();
.. snip ..
void AddField(ref SPContentType contentType, string fieldType, string fieldName)
{
SPField field = new SPField(contentType.Fields, fieldType, fieldName);
if (fieldName.Equals("Users"))
((SPFieldUser)field).AllowMultipleValues = true;
SPFieldLink fieldLink = new SPFieldLink(field);
contentType.FieldLinks.Add(fieldLink); // Exception thrown here
contentType.Update();
}
On the line: contentType.FieldLinks.Add(fieldLink);,
I'm getting an ArgumentNullException: value cannot be null. Parameter name: g
Does anyone know what this means or can see what I'm doing wrong? Thanks in advance!