November Happy Hour will be moved to Thursday December 5th.

Unlinking MetaField from MetaClass programmatically

Vote:
 

I am not able to delete MetaField programmatically, because said MetaField are linked to MetaClass/MetaClasses and therefore cannot be deleted. Below code works after removing the MetaField link to MetaClass/MetaClasses manually within the Commerce Manager, but I want to automate that process.

I have read threads regarding the topic on this Forum. They all seem to be linking to another thread explaining this. That thread seems to be no longer available (https://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=142928). 

MetaDataContext metaDataContext = CatalogContext.MetaDataContext;
MetaClass metaClass = Mediachase.MetaDataPlus.Configurator.MetaClass.Load(metaDataContext, "metaclassname");
IEnumerable<MetaField> allMetaFields = metaClass.GetAllMetaFields();

foreach (MetaField metaField in allMetaFields)
{
  if (metaField.Name.Equals("metafieldname"))
    {
       MetaField.Delete(metaDataContext, metaField.Id);
    }
}

How would I go about checking for MetaField link/links to MetaClass/MetaClassses programmatically, and then delete the link, so that above code would work?

#229799
Oct 23, 2020 11:04
Vote:
 

metaClass.DeleteField(metaFieldName) should do the work for you. So basically 

MetaDataContext metaDataContext = CatalogContext.MetaDataContext;

MetaClass metaClass = Mediachase.MetaDataPlus.Configurator.MetaClass.Load(metaDataContext, "metaclassname"); 

metaClass.DeleteField("metafieldname");

#229804
Oct 23, 2020 14:22
Vote:
 

Thanks Quan Mai,

It works now.

Philip

#229903
Oct 26, 2020 15:44
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.