Create a new folder in a shared mailbox with Invoke Code in Uipath

Uipath Studio Version: 2021.10.3
Language: C#

Objective: I need to create a new folder and subfolder at the same level of the inbox of a shared mailbox. I need to implement this solution with Uipath and Invoke code activity.

Chosen approach: I did not find any official activity in Uipath to achieve my goal, so I decided to develop it through Invoke Code.

Source: I used this script to create a folder because I want to implement it using Exchange server.
Authtenticate a EWS application by using OAuth

I already created the appId in our azure cloud.

Code created:

Main({sharedMailboxAddress,parentFolderName,childFolderName,appId});

class program
{
    static async System.Threading.Tasks.Task Main(string[] args)
            {
                // Using Microsoft.Identity.Client 4.22.0
            
                // Configure the MSAL client to get tokens
                var pcaOptions = new Microsoft.Identity.Client.PublicClientApplicationOptions
                {
                    // Client ID (also known as App ID) of the application as registered in the application registration porta
                   ClientId = args[4]
                };
              
               var pca = Microsoft.Identity.Client.PublicClientApplicationBuilder.CreateWithApplicationOptions(pcaOptions).Build();
            
               // The permission scope required for EWS access
               var ewsScopes = new string[] { "https://outlook.office365.com/EWS.AccessAsUser.All" };
                 
                try
                {
                    // Make the interactive token request
                    var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();
            
                    // 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 from Shared Mailbox FolderId
                    System.Console.WriteLine(WellKnownFolderName.Root); 
                    FolderId SharedMailboxFolderId = new FolderId(WellKnownFolderName.Root, args[0]);
                    
                    // Set Parent FolderId
                    FolderId parentFolderID = null;
                    if(args[1]!="")
                    {
                        var findParentFolder = ewsClient.FindFolders(
                            SharedMailboxFolderId,
                            new SearchFilter.SearchFilterCollection(
                                Microsoft.Exchange.WebServices.Data.LogicalOperator.Or,
                                new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, args[1])),
                            fv);
                        parentFolderID = findParentFolder.ElementAt(0).Id;
                        System.Console.WriteLine("Parent Folder exists");   
                    }
                    else
                    {
                        parentFolderID = SharedMailboxFolderId;
                    }
                    
                    var findChildFolder = ewsClient.FindFolders(
                            parentFolderID,
                             new SearchFilter.SearchFilterCollection(
                                Microsoft.Exchange.WebServices.Data.LogicalOperator.Or,
                                new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, args[1])),
                            fv);
                        
                    FolderId childFolderID = findChildFolder.ElementAt(0).Id;
                    
                    //if child folder not found, create it
                    if (childFolderID == 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 folder = new Folder(ewsClient);
                        // Set the folder properties
                        folder.DisplayName = args[3];
                        // Save the new folder in a specified parent folder.
                        folder.Save(parentFolderID);
                    }
                    else
                    {
                        System.Console.WriteLine($"Child Folder exists");   
                    }
                    
                }
                catch (Microsoft.Identity.Client.MsalException ex)
                {
                    System.Console.WriteLine("Error acquiring access token: {ex}");
                }
                catch (Exception ex)
                {
                   System.Console.WriteLine("Error: {ex}");
                }
                
        }   
}

Questions:

  1. How to call the method Main in the Invoke code?
  2. How to pass the arguments to this static method?. I created the arguments and those look like:

enter image description here

However, I do not really know how to pass them. That is why I created this code: Main({…}) but it does not work.

  1. Should I add a namespace inside of the invoke code?
  2. How do I retrieve the error from the catch block and assign it to the variable ‘Fehlermeldung’?

That is a lot of questions in one 🙂
First of, you could most likely achieve the same using a lot of Assign activities.

But if you want to continue to use the chosen approach then the first thing would be to only include the actual content of the Main method in the Invoke Code.

Your Invoke Code Arguments looks correct.

You should not add namespaces in the Invoke Code.

I’m not sure what you mean with your last point, but if you just assigned the String that you are currently logging, to the output variable ´Fehlermeldung´, then that should do it?

Just remember to add a $ before your first " in the string, when you want to include the exception message

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