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:
However, I do not really know how to pass them. That is why I created this code: Main({...}) but it does not work.
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