Execute a async method in Invoke Code of Uipath

Objective: Move a Mail into an Outlook subfolder that does not exist. That is why I have to create that folder and subfolder in Outlook.

Possible solution: I decided to try ExchangeWebservice to achieve the creation of the folder and subfolder because I could not find an official package from Uipath to do it. This code must be executed in Uipath with Invoke Code.

string[] args ={appId, sharedMailboxAddress, mailboxAddress, folderPath};

    static async System.Threading.Tasks.Task Main(string[] args)
            {
                System.Console.WriteLine("### Start of create Folder ###");

                // Configure the MSAL client to get token  
                var pca = Microsoft.Identity.Client.PublicClientApplicationBuilder.Create(args[0]).Build();
    
                // The permission scope required for EWS access
                var ewsScopes = new string[] { "https://outlook.office365.com/EWS.AccessAsUser.All" };
                
                try
                {
                    Microsoft.Identity.Client.AuthenticationResult authResult;
                    try
                    {
                        System.Console.WriteLine("Make the silent token request");
                        // Make the silent token request
                        var accounts = await pca.GetAccountsAsync();
                        authResult = await pca.AcquireTokenSilent(ewsScopes,accounts.FirstOrDefault()).ExecuteAsync();  
                    }
                    catch(Microsoft.Identity.Client.MsalUiRequiredException ex)
                    {
                        System.Console.WriteLine($"{ex}");
                        System.Console.WriteLine("Make the interactive token request");
                        // Make the interactive token request
                        authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();   
                    }
                    
                    // Configure the ExchangeService with the access token
                    System.Console.WriteLine("Configure the ExchangeService with the access token");    
                    var ewsClient = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
                    ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
                    ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
                    
                    FolderView fv = new FolderView(100);
                    
                    // Set Root Mailbox FolderId
                    System.Console.WriteLine($"Shared Mailbox: {args[1]}"); 
                    FolderId RootMailboxFolderId = null;
                    if(args[1]!="")
                    {
                        RootMailboxFolderId = new FolderId(WellKnownFolderName.Inbox, args[1]); 
                    }
                    else
                    {
                        RootMailboxFolderId = new FolderId(WellKnownFolderName.Inbox);  
                    }
                    
                    string s = args[3];
                    string[] newFolders = s.Split('\');
                        
                    var parentFolderId = RootMailboxFolderId;
                    
                    foreach(string folder in newFolders)
                    {
                        System.Console.WriteLine($"Parent folder: {parentFolderId}");
                        
                        var findChildFolder = ewsClient.FindFolders(
                            parentFolderId,
                            new SearchFilter.SearchFilterCollection(
                                Microsoft.Exchange.WebServices.Data.LogicalOperator.Or,
                                new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, folder)),
                            fv);
                        if(findChildFolder==null)
                        {
                            //Source: https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/dd633637(v=exchg.80)
                            // Create a generic folder with a valid ExchangeService object
                            Folder newFolder = new Folder(ewsClient);
                            // Set the folder properties
                            newFolder.DisplayName = folder;
                            // Save the new folder in a specified parent folder.
                            newFolder.Save(parentFolderId);
                            parentFolderId = newFolder.Id;
                            System.Console.WriteLine($"Child Folder created successfully");
                        }
                        else
                        {
                            System.Console.WriteLine($"Child Folder {folder} exists");  
                        }
                    }
                
                }   
                catch (Exception ex)
                {
                    System.Console.WriteLine($"Error: {ex.Message}");
                    //return ex.Message;
                }
                
        }   
            
Main(args);

Current issues: When I execute my code and it implements the below piece of code, the code does not continue in the invoke code and jumps directly to the next activity “Move Exchange Mail Message” in my workflow

authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();   

enter image description here

If I understood correctly this method run asynchronously and the keyword await suspend while the user types its credentials. I have two questions:

  1. Should I keep going on this approach or is there another easy solution to solve my requirement?

  2. If this is the right approach, How should I keep the execution without jumping to the next activity?.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật