Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

it means your SPField field = new ... returned null, as that's what you're trying to pass it into the new SPFieldLink.

If you're trying to create new fields, I strongly recommend using SPFieldCollection.Add() (so, in your example, contentType.Fields.Add()) this will return the internal name of the field just added, so you need to retrieve it again field contentType.Fields and then add it to the FieldLink Collection.

By the way, can you make sure you're not executing this code every time an item is added (you said you're building an event receiver) - as this will start to throw all sorts of errors as soon as you upload your second item.

share|improve this answer
    
So, instead of the line SPFieldLink fieldLink = new SpFieldLink(field), I do: contentType.Fields.Add(field); and then something like SPFieldLink fieldLink = contentType.Fields[contentType.Fields.Count-1]; ? –  Music Magi Jun 9 '11 at 15:46
1  
If you're doing this on either, you'll be trying to create the same fields and add them to the same content type each time you try to add files to the library. Are you deleting the library every time you try this? –  James Love Jun 9 '11 at 17:34
1  
Exactly, create the fields before the content type. But only create the field if it doesn't exist. Also, I think we both got confused - create the field at Site Collection Level first (SPSite.RootWeb.ContentTypes), it's easier to add to the content type that way. –  James Love Jun 9 '11 at 17:44
1  
Did you mean SPSite.RootWeb.Fields? –  Music Magi Jun 9 '11 at 17:46
1  
The field needs to be persisted to the database (with Update()) before you can add it to the content type, otherwise the "Add" method for the content type will try to load details for an SPField that doesn't yet exist in the content database, hence your SPException. –  James Love Jun 9 '11 at 17:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.