The article provides one example of how to retrieve a Unique ID Context attribute value and then set an ID value if none is already set. In this example, the first context is retrieved, but this can, of course, be adjusted to meet your use case. In addition, this example does not include a check  the item type SID of the items that receive the ID value.


Example


       private static void SetAUIDIfNotSet(IswItem contextItem)
        {
            foreach(IswPart part in contextItem.GetAllParts())
            {
                if (part.DefObj is IswItem item)
                {
                    IswAttribute attribute = item.FindAttributeWithSID("AUID");
                    if (attribute == null)
                        SetAUIDAttribute(item, contextItem);
                }
            }
        }

        private static void SetAUIDAttribute(IswItem attributeItem, IswItem contextItem)
        {
            //Reading system variable names from contextItem attribute AUIC
            IswAttributeType attributeTypeAUIC = contextItem.Broker.FindAttributeTypeWithSID("AUIC");
            IswAttribute attributeAUIC = contextItem.GetAttributeOfType(attributeTypeAUIC);
            //Getting the first system variable name
            string systemVariableName = GetFirstValueInStringArrayAttribute(attributeAUIC.ValueAsXML);

            //Getting next value from system variable
            string counterValue = contextItem.Broker.IncreaseSystemVariable(systemVariableName);
            //Name convention for Unique IDs in SystemWeaver is counterName-counterValue
            string attributeAUIDValue = $"{systemVariableName}-{counterValue}";

            //Setting new value for AUID attribute
            attributeItem.SetAttributeWithSID("AUID", attributeAUIDValue);
        }

        private static string GetFirstValueInStringArrayAttribute(string xmlString)
        {
            var xmlDocument = XDocument.Parse(xmlString);
            return xmlDocument.Root.Elements("VALUE").First().Value;
        }