THe following code throws me an exception, System.ArgumentNullException: Key cannot be null. Parameter name: key at System.Collections.Hashtable.get_Item(Object key) at Microsoft.SharePoint.SPFieldLinkCollection.Exists(String name, SPFieldLink& fldFound)
also here: but nobody answered. http://social.technet.microsoft.com/Forums/sk/sharepoint2010programming/thread/5618e800-c4b5-4308-a5d8-c509aabb120e
My code is as follows:
using (SPSite spsite = new SPSite(strurl))
{
using (SPWeb currentWeb = spsite.OpenWeb())
{
try
{
currentWeb.AllowUnsafeUpdates = true;
SPContentType agendaPointsProposedCT = currentWeb.ContentTypes[Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME];
SPField reasonReturnedField = CreateHtmlField(currentWeb, Meetings.Common.Constants.FIELDS_AGENDAPOINTSREASONRETURNED_NAME);
//SPField reasonReturnedField = currentWeb.Fields.GetFieldByInternalName(reasonReturnedFieldName);
reasonReturnedField.Group = "$Resources:SPNLXXX,Field_XXXColumns_Group";
string schemaXmlWithResourceTokens = reasonReturnedField.SchemaXmlWithResourceTokens;
string displaynamelocalized = "$Resources:SPNLXXX,Field_ReasonReturned_Name";
string returnValue;
int indexOfAttributeName = schemaXmlWithResourceTokens.IndexOf("DisplayName", StringComparison.InvariantCultureIgnoreCase);
int indexOfAttibuteValueBegin = schemaXmlWithResourceTokens.IndexOf('"', indexOfAttributeName);
int indexOfAttributeValueEnd = schemaXmlWithResourceTokens.IndexOf('"', indexOfAttibuteValueBegin + 1);
returnValue = schemaXmlWithResourceTokens.Substring(0, indexOfAttibuteValueBegin + 1) + displaynamelocalized + schemaXmlWithResourceTokens.Substring(indexOfAttributeValueEnd);
reasonReturnedField.SchemaXml = returnValue;
//reasonReturnedField.Update();
SPFieldLink fieldLink = new SPFieldLink(currentWeb.AvailableFields.GetField(reasonReturnedField.InternalName));
//Check if the Field reference exists
if (!agendaPointsProposedCT.Fields.ContainsField(reasonReturnedField.Title))
{
agendaPointsProposedCT.FieldLinks.Add(fieldLink);
agendaPointsProposedCT.Update(true);
}
MoveFieldInColumnOrderToLastPosition(currentWeb, Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME, reasonReturnedField.InternalName);
}
catch (Exception ex)
{
Logger.LogError("XXX", "AddReasonReturnedFieldsToContentTypeAgendaPointProposed(SPListItem item, SPWeb elevatedTargetWeb)", ex);
throw;
}
finally
{
currentWeb.AllowUnsafeUpdates = false;
}
}
}
private static HtmlField CreateHtmlField(SPWeb currentWeb, string fieldName)
{
HtmlField fieldHtml;
fieldName = fieldName.Trim();
SPFieldCollection spFieldCollection = currentWeb.Fields;
if (currentWeb.AvailableFields.ContainsFieldWithStaticName(fieldName))
{
fieldHtml = (HtmlField)spFieldCollection[fieldName];
}
else
{
fieldHtml = new HtmlField(spFieldCollection, "HTML", fieldName)
{
Title = fieldName,
Group = "$XXX,Field_XXXColumns_Group",
ShowInEditForm = true,
StaticName = fieldName,
RichText = true,
RichTextMode = SPRichTextMode.FullHtml,
AllowHyperlink = true,
AllowDeletion = true
};
spFieldCollection.Add(fieldHtml);
}
return fieldHtml;
}