/// <summary> /// Removes relation between DynamicModel item and relatedItem by field name /// </summary> /// <param name="item">Current Dynamic Content Object</param> /// <param name="relatedItem">Related Dynamic Content Object</param> /// <param name="fieldName">Name of the string field</param> public static void DeleteRelation(this DynamicModel item, DynamicModel relatedItem, string fieldName) { if (item == null) { throw new ArgumentNullException("item"); } else if (relatedItem == null) { throw new ArgumentException("relatedItem"); } if (fieldName != null) { var sfContent = item.ToSitefinityModel(); var sfRelatedContent = relatedItem.ToSitefinityModel(false); if (sfContent.DoesFieldExist(fieldName)) { DynamicModuleManager manager = DynamicModuleManager.GetManager(); sfContent.DeleteRelation(sfRelatedContent, fieldName); manager.Lifecycle.Publish(sfContent); manager.SaveChanges(); } } }
/// <summary> /// Gets related Dynamic Models for a given field name /// </summary> /// <typeparam name="T">The content type.</typeparam> /// <param name="item">Current Dynamic Content Object</param> /// <param name="fieldName">Name of the string field</param> public static List <T> GetRelated <T>(this DynamicModel item, string fieldName) where T : DynamicModel, new() { if (item == null) { throw new ArgumentNullException("item"); } if (fieldName != null) { var sfContent = item.ToSitefinityModel(); if (sfContent.DoesFieldExist(fieldName)) { var items = sfContent.GetRelatedItems <DynamicContent>(fieldName) .Select(i => (T)Activator.CreateInstance(typeof(T), i)) .ToList(); return(items); } } return(null); }