0

I am looking for a way to automatically set the status text in Teams. My first approach was the API provided by Microsoft: https://learn.microsoft.com/de-de/graph/api/presence-setpresence?view=graph-rest-1.0&tabs=http Nice, but lacks the possibility to set a text message, so I posted a feature request: https://techcommunity.microsoft.com/t5/microsoft-365/ms-graph-setpresence-enable-support-to-set-a-message-text/m-p/3679411

So I was looking for an alternative for now and came accross the UI Automation: https://stackoverflow.com/a/67418882/1099519

As the sample for the calculator application works, I tried to do the same for MS Teams:

private const string TeamsProcessName = "Teams";

public static void Run()
{
    Process? teams = GetTeamsProcess();
    if (teams == null)
    {
        Console.WriteLine("Teams instance not found");
        return;
    }

    AutomationElement root = AutomationElement.RootElement;
    Condition condition = new PropertyCondition(AutomationElement.NameProperty, teams.MainWindowTitle);

    AutomationElement teamsUi = root.FindFirst(TreeScope.Children, condition);
    if (teamsUi != null)
    {
        Console.WriteLine("Teams-UI not found");
        return;
    }
}

public static Process? GetTeamsProcess()
{
    return Process.GetProcesses().FirstOrDefault(p => p.ProcessName == TeamsProcessName && !String.IsNullOrWhiteSpace(p.MainWindowTitle));

}

But the teamsUI variable is always null. The reason might be: https://stackoverflow.com/a/62051907/1099519

But I actually can find elements with the inspect tool: Screenshot if inspect.exe of tEams

I can even navigate to the status edit area (see red arrow).

The alternative posted here: https://stackoverflow.com/a/61691936/1099519 seems to be outdated:

Important: Chrome will be removing support for Chrome Apps on all platforms. Chrome browser and the Chrome Web Store will continue to support extensions. Read the announcement and learn more about migrating your app.

I might be close to the solution with the code above, but this is where I got stuck now. Any ideas or different approaches how I could achieve my goal?

DominikAmon
  • 892
  • 1
  • 14
  • 26

1 Answers1

0

It is actually possible to use System.Windows.Automation with MS Teams and it is simple:

Process? teams = Process.GetProcesses()
                       .FirstOrDefault(p => p.ProcessName == "Teams" 
                                          && !String.IsNullOrWhiteSpace(p.MainWindowTitle));

AutomationElement root = AutomationElement.FromHandle(teams.MainWindowHandle);

// Do what ever you like

So I was able to "automate" Teams in the way I want. Nevertheless it is a very "hacky" and unstable way to do it, because as soon Teams changes the UI somehow, chances of breaking code are pretty high.

Still open for more stable alternatives (or an API method from MS that would directly support that!)

DominikAmon
  • 892
  • 1
  • 14
  • 26