Access Arguments/Variables annotations

Hello guys, for those who have created custom rules, is there a way in which I can access the arguments/variables annotations? (those which you can find right-clicking the argument/variable and within the Design Properties option in Studio)

You access them by right-clicking the variable/argument in Studio. Is there somewhere else you’re trying to see them?

oh, i forgot to specify. I want to access that property them from within Visual Studio using the API (Uipath.activities.api). I need to create a custom rule which asks for an annotation in every argument. When I debug I can access the Arguments/Variables using the API, but I can not find the “annotation property”

I do not believe they are accessible.
Here is how I solved it

        // This is the function that executes for each activity in all the files. Might impact performance.
        // The rule instance is the rule provided above which also contains the user-configured data.
        private static InspectionResult Inspect(IWorkflowModel workflowModel, Rule ruleInstance)
        {
            var messageList = new List<string>();

            if (workflowModel.Project.ProjectOutputType == "Library" && !(workflowModel.Project.TestCases.Contains(workflowModel.RelativePath)))
            {
                string workflowFullPath = $@"{workflowModel.Project.Directory}\{workflowModel.RelativePath}";
                string workflowXamlText = File.ReadAllText(workflowFullPath);

                XmlDocument workflowXML = new XmlDocument();
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(workflowXML.NameTable);
                workflowXML.LoadXml(workflowXamlText);
                nsmgr.AddNamespace("ns1", "http://schemas.microsoft.com/netfx/2009/xaml/activities");
                nsmgr.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");

                XmlNodeList annotationsNodeList = workflowXML.SelectNodes("/ns1:Activity/x:Members/x:Property", nsmgr);

                foreach (XmlNode annotationNode in annotationsNodeList)
                {
                    if (annotationNode.Attributes["sap2010:Annotation.AnnotationText"] is null)
                    {
                        messageList.Add($"The annotation {annotationNode.Attributes["Name"].Value} requires an annotation.");
                    }
                }
            }

            if (messageList.Count > 0)
            {
                return new InspectionResult()
                {
                    ErrorLevel = ruleInstance.ErrorLevel,
                    HasErrors = true,
                    RecommendationMessage = ruleInstance.RecommendationMessage,
                    // When inspecting a model, a rule can generate more than one message.
                    Messages = messageList
                };
            }
            else
            {
                return new InspectionResult() { HasErrors = false };
            }
        }

It is worth noting, there are two ways annotations can be stored in the xaml file, so this only checks for one of them.

2 Likes

ohhh, that is an interesting approach, I also inspected the .xaml file and I saw that the annotations actually existed but I could not find a way to access them using the API.
Thanks you so much Jon! I do not understand few things from your code but I will take a deeper look haha

Currently there is no annotation property for argument/variable model in Studio.Analyzer.Models. I have rule which check annotation for main workflow sequence (there is AnnotationText property for activity model), but annotation for argument is inaccessible directly from library. Of course we can find it by scrapping xaml file (as @Jon_Smith mentioned), but this requires more effort.

Ok, I’ll explain it a little more, but its been a while since I messed around trying out making rules.

If I remember correctly you can inspect different objects and variables / arguments are one of them but they lack key details such as annotations.
So instead I focused on inspecting each workflow.

The rule I had was that arguments in libraries must have an annotation so the first thing is to check the project type is a library and that the workflow we are looking at isnt a test case (as I exempt them from this rule).

Next it builds the path to the xaml file itself (also not a property on the IWorkflowModel, I think it should be).
I then read the file as a xaml document, I need to load some namespaces to it to then select some nodes.
I match all the nodes that match the annotations pattern (they are in the xaml as a property) and then loop through each node (annotation).

On the annotation I check to see if it has an annotation attribute, if it doesnt there is no annotation (unless its stored the other way, as mentioned), arguments with no annotations are added to the message list and appear as validation rule recommendations to fix when you run this rule. You get one line in the rules list per argument per workflow that violates the rule.

You can also use a similar method to make sure the root activity also has an annotation, this is also important for properly documented libraries in my opinion.

Regarding ‘this requires more effort’. You are talk about checking something different? Unless I misunderstand what you propose doesn’t check the annotations on arguments at all but unrelated annotations?

So its not really a matter of ‘more effort’ but rather one method does check argument annotations, the other performs a totally different rule check no?

Adding to the approach from @Jon_Smith but in PowerShell.

This implementation gets arguments, variables and their associated properties.

@ahzaradsh not exactly what you are looking for as PowerShell won’t help your use case to build a custom rule. Nonetheless an approach good to know.

1 Like

That’s right, there is no property fo argument annotation. @Jon_Smith solution is the correct one. I meant this solution with reading parsing xaml file is not easy :slight_smile: But there is no better way and Jon did a great job :slight_smile:

Ah, no worries then.
Indeed it is not easy to read the file directly but I have found many scenarios where I have needed to do this, either to fix bugs (as the annotations dont compile properly when you publish a library with an non-xaml files in it) or to generate documentation as I have a bot that builds a readme file about my projects and gets important information from each xaml file.
At some point you have to bite the bullet and do it.
I will say though I think the object models for the Uipath.Activities.API really do need to be expanded.

@jeevith also provided a valuable script I think as perhaps that can be incorporated into a build pipeline etc?

Interesting topic!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.