共用方式為


Sample: Use deployment service messages

 

Applies To: Dynamics CRM 2013

Requirements

This sample code is for Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online. Download the Microsoft Dynamics CRM SDK package. The code can be found in the following location in the download package:

SampleCode\CS\DeploymentService\UseDeploymentServiceMessages.cs

SampleCode\VB\DeploymentService\UseDeploymentServiceMessages.vb

Demonstrates

The following sample shows how to access and call the deployment service using the IDeploymentService. Execute method. To add the service to your project, see Introduction to the deployment web service.

Example


using System;
using System.Net;
using System.ServiceModel;

// These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
// located in the SDK\bin folder of the SDK download.
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;

// These namespaces are found in the Microsoft.Xrm.Sdk.Deployment.dll assembly
// located in the SDK\bin folder of the SDK download.
using Microsoft.Xrm.Sdk.Deployment;
using Deployment = Microsoft.Xrm.Sdk.Deployment;

namespace Microsoft.Crm.Sdk.Samples
{
    /// <summary>
    /// Demonstrates how to retrieve deployment information programmatically.</summary>
    /// <remarks>
    /// NOTE: The deployment service only supports Active Directory, so the user running
    /// this sample must have a valid Active Directory account.
    /// </remarks>
    public class UseDeploymentServiceMessages
    {
        #region Class Level Members

        private OrganizationServiceProxy _serviceProxy;

        #endregion Class Level Members

        #region How To Sample Code
        /// <summary>
        /// This method first connects to the Deployment service. Then,
        /// a variety of messages are used to retrieve deployment information.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete
        /// all created entities.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                // Connect to the Organization service. 
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    // Instantiate DeploymentServiceClient for calling the service.
                    DeploymentServiceClient serviceClient = 
                        Deployment.Proxy.ProxyClientHelper.CreateClient(
                        new Uri(serverConfig.DiscoveryUri.ToString()
                            .Replace("Services", "Deployment")
                            .Replace("Discovery", "Deployment")));

                    // Setting credentials from the current security context. 
                    if (serverConfig.Credentials == null)
                    {
                        serviceClient.ClientCredentials.Windows.ClientCredential =
                            CredentialCache.DefaultNetworkCredentials; 
                    }
                    else
                    {
                        serviceClient.ClientCredentials.Windows.ClientCredential =
                            serverConfig.Credentials.Windows.ClientCredential;
                    }

                    // Retrieve all deployed instances of Microsoft Dynamics CRM.
                    var organizations =
                        serviceClient.RetrieveAll(DeploymentEntityType.Organization);

                    // Print list of all retrieved organizations.
                    Console.WriteLine("Organizations in your deployment");
                    Console.WriteLine("================================");
                    foreach (var organization in organizations)
                    {
                        Console.WriteLine(organization.Name);
                    }
                    Console.WriteLine("<End of Listing>");
                    Console.WriteLine();


                    // Retrieve details of first organization from previous call.
                    Deployment.Organization deployment =
                        (Deployment.Organization)serviceClient.Retrieve(
                            DeploymentEntityType.Organization,
                            organizations[0]);

                    // Print out retrieved details about your organization.
                    Console.WriteLine(String.Format(
                        "Selected deployment details for {0}",
                        serverConfig.OrganizationName));
                    Console.WriteLine("=========================================");
                    Console.Write("Friendly Name: ");
                    Console.WriteLine(deployment.FriendlyName);
                    Console.Write("Unique Name: ");
                    Console.WriteLine(deployment.UniqueName);
                    Console.Write("Organization Version: ");
                    Console.WriteLine(deployment.Version);
                    Console.Write("SQL Server Name: ");
                    Console.WriteLine(deployment.SqlServerName);
                    Console.Write("SRS URL: ");
                    Console.WriteLine(deployment.SrsUrl);
                    Console.WriteLine("<End of Listing>");
                    Console.WriteLine();

                    // Retrieve license and user information for your organization.
                    TrackLicenseRequest licenseRequest = new TrackLicenseRequest();
                    TrackLicenseResponse licenseResponse =
                        (TrackLicenseResponse)serviceClient.Execute(licenseRequest);

                    // Print out the number of servers and the user list.
                    Console.WriteLine(String.Format(
                        "License and user information for {0}",
                        serverConfig.OrganizationName));
                    Console.WriteLine("=========================================");
                    Console.Write("Number of servers: ");
                    Console.WriteLine(licenseResponse.Servers != null
                        ? licenseResponse.Servers.Count.ToString()
                        : "null");
                    Console.WriteLine("Users:");
                    foreach (OrganizationUserInfo user in licenseResponse.Users.ToArray())
                    {
                        Console.WriteLine(user.FullName);
                    }
                    Console.WriteLine("<End of Listing>");
                    Console.WriteLine();

                    // Retrieve advanced settings for your organization.
                    // Note that the columnset must contain at least one column. Setting
                    // AllColumns to true results in an error.
                    RetrieveAdvancedSettingsRequest request =
                        new RetrieveAdvancedSettingsRequest
                        {
                            ConfigurationEntityName = "Server",
                            ColumnSet = new ColumnSet(
                                new string[] { "Id", "FullName", "Name", "Roles", "State", "Version" })
                        };
                    ConfigurationEntity configuration =
                        ((RetrieveAdvancedSettingsResponse)serviceClient.Execute(request)).Entity;

                    // Print out all advanced settings where IsWritable==true.
                    Console.WriteLine("Advanced deployment settings that can be updated");
                    Console.WriteLine("================================================");
                    foreach (var setting in configuration.Attributes)
                    {
                        if (setting.Key != "Id")
                        {
                            Console.WriteLine(
                                String.Format("{0}: {1}",
                                    setting.Key,
                                    setting.Value));
                        }
                    }
                    Console.WriteLine("<End of Listing>");
                    Console.WriteLine();

                }
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }

        #endregion How To Sample Code

        #region Main method

        /// <summary>
        /// Standard Main() method used by most SDK samples.
        /// </summary>
        /// <param name="args"></param>
        static public void Main(string[] args)
        {
            try
            {
                // Obtain the target organization's Web address and client logon 
                // credentials from the user.
                ServerConnection serverConnect = new ServerConnection();
                ServerConnection.Configuration config = serverConnect.GetServerConfiguration();

                UseDeploymentServiceMessages app = new UseDeploymentServiceMessages();
                app.Run(config, true);
            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
                Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
                Console.WriteLine("Message: {0}", ex.Detail.Message);
                Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText);
                Console.WriteLine("Inner Fault: {0}",
                    null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
            }
            catch (System.TimeoutException ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine("Message: {0}", ex.Message);
                Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
                Console.WriteLine("Inner Fault: {0}",
                    null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("The application terminated with an error.");
                Console.WriteLine(ex.Message);

                // Display the details of the inner exception.
                if (ex.InnerException != null)
                {
                    Console.WriteLine(ex.InnerException.Message);

                    FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
                        as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                    if (fe != null)
                    {
                        Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
                        Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
                        Console.WriteLine("Message: {0}", fe.Detail.Message);
                        Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText);
                        Console.WriteLine("Inner Fault: {0}",
                            null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
                    }
                }
            }
            // Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
            // SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.

            finally
            {
                Console.WriteLine("Press <Enter> to exit.");
                Console.ReadLine();
            }
        }
        #endregion Main method      

    }
}

Imports System.Net
Imports System.ServiceModel

' These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
' located in the SDK\bin folder of the SDK download.
Imports Microsoft.Xrm.Sdk.Query
Imports Microsoft.Xrm.Sdk.Client

' These namespaces are found in the Microsoft.Xrm.Sdk.Deployment.dll assembly
' located in the SDK\bin folder of the SDK download.
Imports Microsoft.Xrm.Sdk.Deployment

Namespace Microsoft.Crm.Sdk.Samples
    ''' <summary>
    ''' Demonstrates how to retrieve deployment information programmatically.</summary>
    ''' <remarks>
    ''' NOTE: The deployment service only supports Active Directory, so the user running
    ''' this sample must have a valid Active Directory account.
    ''' </remarks>
    Public Class UseDeploymentServiceMessages
#Region "Class Level Members"

        Private _serviceProxy As OrganizationServiceProxy

#End Region ' Class Level Members

#Region "How To Sample Code"
        ''' <summary>
        ''' This method first connects to the Deployment service. Then,
        ''' a variety of messages are used to retrieve deployment information.
        ''' </summary>
        ''' <param name="serverConfig">Contains server connection information.</param>
        ''' <param name="promptforDelete">When True, the user will be prompted to delete
        ''' all created entities.</param>
        Public Sub Run(ByVal serverConfig As ServerConnection.Configuration,
                       ByVal promptforDelete As Boolean)
            Try
                ' Connect to the Organization service. 
                ' The using statement assures that the service proxy will be properly disposed.
                _serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)
                Using _serviceProxy
                    ' This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes()

                    ' Instantiate DeploymentServiceClient for calling the service.
                    Dim serviceClient As DeploymentServiceClient =
                        Microsoft.Xrm.Sdk.Deployment.Proxy.ProxyClientHelper.CreateClient(
                            New Uri(serverConfig.DiscoveryUri.ToString() _
                                    .Replace("Services", "Deployment") _
                                    .Replace("Discovery", "Deployment")))

                    ' Setting credentials from the current security context. 
                    If serverConfig.Credentials Is Nothing Then
                        serviceClient.ClientCredentials.Windows.ClientCredential =
                        CredentialCache.DefaultNetworkCredentials
                    Else
                        serviceClient.ClientCredentials.Windows.ClientCredential =
                            serverConfig.Credentials.Windows.ClientCredential
                    End If

                    ' Retrieve all deployed instances of Microsoft Dynamics CRM.
                    Dim organizations = serviceClient.RetrieveAll(
                        DeploymentEntityType.Organization)

                    ' Print list of all retrieved organizations.
                    Console.WriteLine("Organizations in your deployment")
                    Console.WriteLine("================================")
                    For Each organization In organizations
                        Console.WriteLine(organization.Name)
                    Next organization
                    Console.WriteLine("<End of Listing>")
                    Console.WriteLine()


                    ' Retrieve details of first organization from previous call.
                    Dim deployment As Microsoft.Xrm.Sdk.Deployment.Organization =
                        CType(serviceClient.Retrieve(DeploymentEntityType.Organization,
                                                     organizations(0)), 
                                                 Microsoft.Xrm.Sdk.Deployment.Organization)

                    ' Print out retrieved details about your organization.
                    Console.WriteLine(String.Format("Selected deployment details for {0}",
                                                    serverConfig.OrganizationName))
                    Console.WriteLine("=========================================")
                    Console.Write("Friendly Name: ")
                    Console.WriteLine(deployment.FriendlyName)
                    Console.Write("Unique Name: ")
                    Console.WriteLine(deployment.UniqueName)
                    Console.Write("Organization Version: ")
                    Console.WriteLine(deployment.Version)
                    Console.Write("SQL Server Name: ")
                    Console.WriteLine(deployment.SqlServerName)
                    Console.Write("SRS URL: ")
                    Console.WriteLine(deployment.SrsUrl)
                    Console.WriteLine("<End of Listing>")
                    Console.WriteLine()

                    ' Retrieve license and user information for your organization.
                    Dim licenseRequest As New TrackLicenseRequest()
                    Dim licenseResponse As TrackLicenseResponse =
                        CType(serviceClient.Execute(licenseRequest), 
                            TrackLicenseResponse)

                    ' Print out the number of servers and the user list.
                    Console.WriteLine(String.Format("License and user information for {0}",
                                                    serverConfig.OrganizationName))
                    Console.WriteLine("=========================================")
                    Console.Write("Number of servers: ")

                    Console.WriteLine(If(licenseResponse.Servers IsNot Nothing,
                                         licenseResponse.Servers.Count.ToString(),
                                         "null"))
                    Console.WriteLine("Users:")
                    For Each user As OrganizationUserInfo In licenseResponse.Users.ToList()
                        Console.WriteLine(user.FullName)
                    Next user
                    Console.WriteLine("<End of Listing>")
                    Console.WriteLine()

                    ' Retrieve advanced settings for your organization.
                    Dim request As RetrieveAdvancedSettingsRequest =
                        New RetrieveAdvancedSettingsRequest With
                        {
                            .ConfigurationEntityName = "Server",
                            .ColumnSet = New ColumnSet(New String() {"Id", "FullName",
                                                "Name", "Roles", "State", "Version"})
                        }
                    Dim configuration As ConfigurationEntity =
                        (CType(serviceClient.Execute(request), 
                         RetrieveAdvancedSettingsResponse)).Entity

                    ' Print out all advanced settings where IsWritable==true.
                    Console.WriteLine("Advanced deployment settings that can be updated")
                    Console.WriteLine("================================================")
                    For Each setting In configuration.Attributes
                        If setting.Key <> "Id" Then
                            Console.WriteLine(String.Format("{0}: {1}", setting.Key, setting.Value))
                        End If
                    Next setting
                    Console.WriteLine("<End of Listing>")
                    Console.WriteLine()

                End Using

                ' Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            Catch fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault)
                ' You can handle an exception here or pass it back to the calling method.
                Throw
            End Try
        End Sub

#End Region ' How To Sample Code

#Region "Main method"

        ''' <summary>
        ''' Standard Main() method used by most SDK samples.
        ''' </summary>
        ''' <param name="args"></param>
        Public Shared Sub Main(ByVal args() As String)
            Try
                ' Obtain the target organization's Web address and client logon 
                ' credentials from the user.
                Dim serverConnect As New ServerConnection()
                Dim config As ServerConnection.Configuration =
                    serverConnect.GetServerConfiguration()

                Dim app As New UseDeploymentServiceMessages()
                app.Run(config, True)
            Catch ex As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault)
                Console.WriteLine("The application terminated with an error.")
                Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp)
                Console.WriteLine("Code: {0}", ex.Detail.ErrorCode)
                Console.WriteLine("Message: {0}", ex.Detail.Message)
                Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText)
                Console.WriteLine("Inner Fault: {0}",
                                  If(Nothing Is ex.Detail.InnerFault, "No Inner Fault", "Has Inner Fault"))
            Catch ex As TimeoutException
                Console.WriteLine("The application terminated with an error.")
                Console.WriteLine("Message: {0}", ex.Message)
                Console.WriteLine("Stack Trace: {0}", ex.StackTrace)
                Console.WriteLine("Inner Fault: {0}",
                                  If(Nothing Is ex.InnerException.Message, "No Inner Fault", ex.InnerException.Message))
            Catch ex As Exception
                Console.WriteLine("The application terminated with an error.")
                Console.WriteLine(ex.Message)

                ' Display the details of the inner exception.
                If ex.InnerException IsNot Nothing Then
                    Console.WriteLine(ex.InnerException.Message)

                    Dim fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault) =
                        TryCast(ex.InnerException, 
                            FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault))
                    If fe IsNot Nothing Then
                        Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp)
                        Console.WriteLine("Code: {0}", fe.Detail.ErrorCode)
                        Console.WriteLine("Message: {0}", fe.Detail.Message)
                        Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText)
                        Console.WriteLine("Inner Fault: {0}",
                                          If(Nothing Is fe.Detail.InnerFault, "No Inner Fault", "Has Inner Fault"))
                    End If
                End If
                ' Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
                ' SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.

            Finally
                Console.WriteLine("Press <Enter> to exit.")
                Console.ReadLine()
            End Try
        End Sub
#End Region ' Main method      

    End Class
End Namespace

See Also

IDeploymentService
Introduction to the deployment web service
Administer the deployment using the deployment web service