Run Application for Several Organizations and Servers



Well, call me an "Ideationista" because what takes me more than 30 minutes to do, my creativity sparks. I always have an idea to build something to do it easier and in the shortest time, so I can have more time for my coffee :D

Today's post is related to a small application that updates several organization and uses several servers, this means, instead of doing everything manually this does it automatically in a way it can be used several times. But I had a small issue when I was trying to run the application. My goal was to run the same code (in my case clean a certain workflows, plugins and do some update on configuration records) for several servers and organizations using a file as configuration so other users could use it without the need of editing the application code. Calling the right method with the right value and connections strings... and here's where the party begins.

Problem:
Even if you try to change the connection string by using the following code, the application continues to do the query expression based on the same server and organization, even if you see that the connection string is in fact changed.
string _Server = list6[i];
string _Org = list7[i] ;
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["xrm"].ConnectionString = "Server=https://"+_Server+".contoso.com/"+_Org+"; Username=Domain\Contoso; Password=ContosoPassword";
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
Solution:
First, I tryed to clean the connection string and change everything on the console application even on the app.config, but still with no success, It seems that the connection was still in memory... then I google it, I found this code:
 xrm.Dispose();
 xrm.ClearChanges();
 Microsoft.Xrm.Client.Caching.ObjectCacheManager.Reset();
I tried it, hoping this would clean the connection... but it didn't... so, a few webpages after, I found that without changing any app.config or other code, the only thing I needed was to replace the code above for the one below.... and it did work out:
 CrmConfigurationManager.Reset();
Application:
This application, what it does, is to stream a file:
var reader = new StreamReader(File.OpenRead(@"file.csv"));
Put everything into lists and then execute the methods and connection dynamically, in my case I have more than 60 organizations and this is the fastest way to update all the organization in one shot.

Example .CSV file:
CallMethod EntityName GuidId RecordFieldName KeyField Server Organization
DeactivateWorkflows workflow B0A401B0-0389-4D4F-A1C3-C14399DEA469 server1 HugoTestServer1
UpdateRecord account B0A401B0-0389-4D4F-A1C3-C14399DEA470 name test server1 HugoTestServer1
DeleteRelationships account B0A401B0-0389-4D4F-A1C3-C14399DEA471 server1 HugoTestServer1
DeactivateWorkflows workflow B0A401B0-0389-4D4F-A1C3-C14399DEA472 server2 HugoTestServer2
UpdateRecord account B0A401B0-0389-4D4F-A1C3-C14399DEA473 name test server2 HugoTestServer2
DeleteRelationships account B0A401B0-0389-4D4F-A1C3-C14399DEA474 server2 HugoTestServer2

Comments

Popular Posts