VarbanVarbanov

Element with the same id already exists

Discussion created by VarbanVarbanov on Sep 25, 2013
Latest reply on Oct 23, 2013 by jstarnes

Hi, when I'm using AFSDK to Update some elements in AF sometimes i get strange exception "Element XXX with unique Id (xxxGuidxxx) cannot be added because element with the same unique Id already exists".
Please note that it is happening when I'm updating element, not even adding it. Also I cannot assign id to an element so it is allways generated by the SDK and i shouldnt face sucgh problem. My code is as follows:

 

 

 
 public List<Guid> AddOrUpdateAFElements(List<AssetElement> elements, string parentElementPathOrId, bool updateSubElements)
        {
            var result = new List<Guid>();
            foreach (var element in elements)
            {
                result.Add(AddOrUpdateSingleElement(element, parentElementPathOrId, updateSubElements));
            }
            CheckIn();
            return result;
        }

 

 
     

private Guid AddOrUpdateSingleElement(AssetElement element, string parentElementPathOrId, bool updateSubElements)
        {
            AFElement foundElement;
            AFElement parentElement = null;

            Guid result;

            if (!String.IsNullOrEmpty(parentElementPathOrId))
            {
                parentElement = GetAFElement(parentElementPathOrId);
                if ((parentElement == null))
                {
                    throw new ArgumentException("Parent element does not exist. Element cannot be added");
                }
                foundElement = parentElement.Elements[element.Name];
            }
            else
            {
                foundElement = _afDatabase.Elements[element.Name];
            }

            if (foundElement == null)
            {
                result = AddAFElementFromAssetElement(parentElement, element);
            }
            else
            {
                result = UpdateAFElementFromAssetElement(foundElement, element, updateSubElements);
            }
            return result;
        }


    private Guid UpdateAFElementFromAssetElement(AFElement elementToUpdate, AssetElement asElement, bool updateSubElements = true)
        {
           
            elementToUpdate.Name = asElement.Name;
            elementToUpdate.Attributes.Clear();
            SetAFElementProperties(elementToUpdate, asElement);
            if (updateSubElements && asElement.SubElements != null)
            {
                foreach (var childAsElement in asElement.SubElements)
                {
                
                    var childAfElement = elementToUpdate.Elements[childAsElement.Name];
                    if (childAfElement != null)
                    {
                        UpdateAFElementFromAssetElement(childAfElement, childAsElement);
                    }
                    else
                    {
                        AddAFElementFromAssetElement(elementToUpdate, childAsElement);
                    }
                  
                }
            }
           
            return elementToUpdate.ID;
        }


 private Guid AddAFElementFromAssetElement(AFElement parentElement, AssetElement asElement)
        {
            // Note! if particular element is not already added in the Af DataBase, it is not possible to add any subelements to this element
            var afElement = CreateOneLevelAFElementFromAssetElement(asElement);
            if (parentElement == null)
            {              
                _afDatabase.Elements.Add(afElement);
            }
            else
            {
                parentElement.Elements.Add(afElement);
            }
         
            RecursiveAddChildElementsToAFElement(afElement, asElement.SubElements);
    
            return afElement.ID;
        }

        private void RecursiveAddChildElementsToAFElement(AFElement element, IEnumerable<AssetElement> childElements)
        {
            if (childElements == null) return;
            foreach (var asElement in childElements)
            {
                var afElement = CreateOneLevelAFElementFromAssetElement(asElement);
                element.Elements.Add(afElement);
                RecursiveAddChildElementsToAFElement(afElement, asElement.SubElements);
            }
        }

 

 

Asset Element is Data Contract of my service as follows:

 

 

 
[DataContract]
    public class AssetElement
    {
        [DataMember]
        public Guid ID { get; set; }

        [DataMember]
        public Guid ParentID { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string Description { get; set; }

        [DataMember]
        public List<AssetAttribute> Attributes { get; set; }

        [DataMember]
        public List<AssetElement> SubElements { get; set; }

        [DataMember]
        public Guid ElementTemplateId { get; set; }

    }

 

 

The exception occures during Checkin() operation. Is anybody has an idea why this exception occures and how to avoid it, I'll highly appreciate that

Best Regards
 

Outcomes