在SP 2007中构建内容类型时出错

时间:2011-06-09 15:32:49

标签: sharepoint events

我有一个事件处理程序,可以捕获ItemAdding事件并动态更改内容类型。这是我构建内容类型的代码:

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

.. ..剪断

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;
    field.Update();
    contentType.Fields.Add(field); // Exception thrown here
    SPFieldLink fieldLink = new SPFieldLink(contentType.Fields[fieldName]);
    contentType.FieldLinks.Add(fieldLink); 
    contentType.Update();
}

在线:contentType.FieldLinks.Add(fieldLink);,

我收到了ArgumentNullException:value不能为null。参数名称:g

有谁知道这意味着什么或者能看出我做错了什么?提前谢谢!

堆栈追踪:

at Microsoft.SharePoint.SPField.UpdateCore(Boolean bToggleSealed) 在Microsoft.SharePoint.SPField.Update() at ChangeContentTypeEventHandler.ChangeContentTypeEventHandler.AddField(SPContentType& contentType,String fieldType,String fieldName)    at ChangeContentTypeEventHandler.ChangeContentTypeEventHandler.ItemAdding(SPItemEventProperties properties)

1 个答案:

答案 0 :(得分:0)

您的错误不在您当前的代码中。它位于堆栈跟踪的下方。你可以发一个完整的堆栈跟踪吗?

编辑:

好的完整跟踪堆栈意味着内容类型当前正在使用中。您最需要做的是指定级联选项。 http://msdn.microsoft.com/en-us/library/ms474878.aspx

等等     contentType.Update(真,真)