in apps/samplecode/automotive/apisamples/Microsoft.Dynamics.Automotive.Samples/helpercode/CrmServiceHelpers.cs [485:548]
public void SaveConfiguration(String pathname, Configuration config, bool append)
{
if (String.IsNullOrWhiteSpace(pathname)) throw new ArgumentNullException("pathname");
if (config == null) throw new ArgumentNullException("config");
// Target is the key with which associated credentials can be fetched from windows credentials manager.
String target = config.ServerAddress + ":" + config.OrganizationName;
if(null != config.Credentials)
{
switch(config.EndpointType)
{
case AuthenticationProviderType.ActiveDirectory:
target = target + ":" + config.Credentials.Windows.ClientCredential.UserName;
break;
case AuthenticationProviderType.LiveId:
case AuthenticationProviderType.Federation:
case AuthenticationProviderType.OnlineFederation:
target = target + ":" + config.Credentials.UserName.UserName;
break;
default:
target = String.Empty;
break;
}
}
XElement configurationsFromFile = XElement.Load(pathname);
XElement newConfig =
new XElement("Configuration",
new XElement("ServerAddress", config.ServerAddress),
new XElement("OrganizationName", config.OrganizationName),
new XElement("DiscoveryUri",
(config.DiscoveryUri != null)
? config.DiscoveryUri.OriginalString
: String.Empty),
new XElement("OrganizationUri",
(config.OrganizationUri != null)
? config.OrganizationUri.OriginalString
: String.Empty),
new XElement("HomeRealmUri",
(config.HomeRealmUri != null)
? config.HomeRealmUri.OriginalString
: String.Empty),
ParseOutCredentials(config.Credentials, config.EndpointType, target),
new XElement("EndpointType", config.EndpointType.ToString()),
new XElement("UserPrincipalName",
(config.UserPrincipalName != null)
? config.UserPrincipalName
: String.Empty)
);
if (append)
{
configurationsFromFile.Add(newConfig);
}
else
{
configurationsFromFile.ReplaceAll(newConfig);
}
using (XmlTextWriter writer = new XmlTextWriter(pathname, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
configurationsFromFile.Save(writer);
}
}