I have the following code and I havent been able to find what the problem is. The exception is raised SOMETIMES in the last line of enable keywords, in the Update.
The thing is we create a site manually from code: then from code we also add a feature, and the problem is happening somewhere in the feature activated or some of its methods.
/// <param name="list"></param>
public static void EnableEnterpriseKeywords(this SPList list)
{
Logger.LogDebug("SPListExtensions", "EnableEnterpriseKeywords(this SPList list)", "BEGIN");
try
{
Assembly taxonomyAssembly = Assembly.LoadWithPartialName("Microsoft.SharePoint.Taxonomy");
Type listFieldSettings = taxonomyAssembly.GetType("Microsoft.SharePoint.Taxonomy.MetadataListFieldSettings");
object listSettings = listFieldSettings.GetConstructor(new Type[] { typeof(SPList) }).Invoke(new object[] { list });
listFieldSettings.GetProperty("EnableKeywordsField", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(listSettings, true, null);
listFieldSettings.GetProperty("EnableMetadataPromotion", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(listSettings, true, null);
listFieldSettings.GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(listSettings, null);
}
catch (Exception ex)
{
Logger.LogError("SPListExtensions", string.Format("EnableEnterpriseKeywords(this SPList list) - Error activating on list {0}", list.Title), ex);
throw;
}
Logger.LogDebug("SPListExtensions", "EnableEnterpriseKeywords(this SPList list)", "END");
}