Share via


Sample: Enable document management for entities

 

Applies To: Dynamics CRM 2013

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

SampleCode\CS\SharePointIntegration\EnableDocumentManagement.cs

SampleCode\VB\SharePointIntegration\EnableDocumentManagement.vb

Requirements

For more information about the requirements for running the sample code provided in this SDK, see Use the sample and helper code.

Demonstrates

The following sample shows enable document management for entities in Microsoft Dynamics CRM. In this sample code, document management is enabled for the Contact entity using the UpdateEntityRequest message. The setting for the Contact entity is not enabled by default in a new installation of Microsoft Dynamics CRM.

Example


using System;
using System.ServiceModel;
using System.ServiceModel.Description;

// 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;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

// This namespace is found in the Microsoft.Crm.Sdk.Proxy.dll assembly
// located in the SDK\bin folder of the SDK download.
using Microsoft.Crm.Sdk.Messages;

namespace Microsoft.Crm.Sdk.Samples
{
    /// <summary>
    /// Demonstrates how to enable document management
    /// for an entity.</summary>
    /// <remarks>
    /// At run-time, you will be given the option to revert
    /// the changes done by this program.</remarks>
    public class EnableDocumentManagement
    {
        #region Class Level Members

        private OrganizationServiceProxy _serviceProxy;

        #endregion Class Level Members

        #region How-To Sample Code
        /// <summary>
        /// This method first connects to the Organization service. Afterwards,
        /// retrieves the Contact entity record, and then enables the document management 
        /// for the entity.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforRevert">When True, the user will be prompted to revert all
        /// the changes done in this sample.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptforRevert)
        {
            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();

                    CreateRequiredRecords();

                    // Retrieve an entity for which you want to enable document management.
                    // In this sample, we will retrieve and enable document management
                    // for the Contact entity because by default, document management is
                    // not enabled for this entity.                    

                    RetrieveEntityRequest entityRequest = new RetrieveEntityRequest
                    {
                        EntityFilters = EntityFilters.All,
                        LogicalName = Contact.EntityLogicalName,

                        // Retrieve only the currently published changes, ignoring the changes 
                        // that have not been published.
                        RetrieveAsIfPublished = false
                    };
                    RetrieveEntityResponse entityResponse = (RetrieveEntityResponse)_serviceProxy.Execute(entityRequest);
                    Console.WriteLine("Retrieved the contact entity.");

                    // Get the entity from the response.
                    EntityMetadata contactEntity = entityResponse.EntityMetadata;

                    // Enable document management for the retrieved entity.
                    contactEntity.IsDocumentManagementEnabled = true;

                    // Create an update request.                    
                    UpdateEntityRequest updateRequest = new UpdateEntityRequest
                    {
                        Entity = contactEntity                        
                    };
                    _serviceProxy.Execute(updateRequest);

                    // Publish the entity.
                    // All customizations must be published before they can be used.
                    PublishAllXmlRequest enableRequest = new PublishAllXmlRequest();
                    _serviceProxy.Execute(enableRequest);                    

                    // Retrieve the contact entity, and verify that document management is enabled.
                    entityResponse = (RetrieveEntityResponse)_serviceProxy.Execute(entityRequest);
                    contactEntity = entityResponse.EntityMetadata;
                    if (contactEntity.IsDocumentManagementEnabled.Value == true)
                    {
                        Console.WriteLine("Enabled document management for the contact entity.");
                    }                    

                    RevertChanges(promptforRevert);
                }

            }

            // 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;
            }
        }

        /// <summary>
        /// Creates any entity records that this sample requires.
        /// </summary>
        public void CreateRequiredRecords()
        {
            // For this sample, no entities are created.
        }

        /// <summary>
        /// Reverts any changes that were done by this sample.
        /// <param name="prompt">Indicates whether to prompt the user 
        /// to revert the changes done by this sample.</param>
        /// </summary>
        public void RevertChanges(bool prompt)
        {
            bool revertChanges = true;

            if (prompt)
            {
                Console.WriteLine("\nDo you want these changes reverted? (y/n) [y]: ");
                String answer = Console.ReadLine();

                revertChanges = (answer.StartsWith("y") || answer.StartsWith("Y") || answer == String.Empty);
            }

            if (revertChanges)
            {
                RetrieveEntityRequest entityRequest = new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.All,
                    LogicalName = Contact.EntityLogicalName,                    
                    RetrieveAsIfPublished = false
                };
                RetrieveEntityResponse entityResponse = (RetrieveEntityResponse)_serviceProxy.Execute(entityRequest);

                EntityMetadata contactEntity = entityResponse.EntityMetadata;

                // Disable document management for the retrieved entity.
                contactEntity.IsDocumentManagementEnabled = false;

                UpdateEntityRequest updateRequest = new UpdateEntityRequest
                {
                    Entity = contactEntity
                };
                _serviceProxy.Execute(updateRequest);

                // Publish the customizations to the entity.                
                PublishAllXmlRequest disableRequest = new PublishAllXmlRequest();
                _serviceProxy.Execute(disableRequest);

                Console.WriteLine("Changes have been reverted.");
            }
        }

        #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();

                EnableDocumentManagement app = new EnableDocumentManagement();
                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");
                    }
                }
            }
            finally
            {
                Console.WriteLine("Press <Enter> to exit.");
                Console.ReadLine();
            }
        }
        #endregion Main method
    }
}

Imports System.ServiceModel
Imports System.ServiceModel.Description

' 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
Imports Microsoft.Xrm.Sdk.Client
Imports Microsoft.Xrm.Sdk.Query
Imports Microsoft.Xrm.Sdk.Messages
Imports Microsoft.Xrm.Sdk.Metadata

' This namespace is found in the Microsoft.Crm.Sdk.Proxy.dll assembly
' located in the SDK\bin folder of the SDK download.
Imports Microsoft.Crm.Sdk.Messages

Namespace Microsoft.Crm.Sdk.Samples
 ''' <summary>
 ''' Demonstrates how to enable document management
 ''' for an entity.</summary>
 ''' <remarks>
 ''' At run-time, you will be given the option to revert
 ''' the changes done by this program.</remarks>
 Public Class EnableDocumentManagement
#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 Organization service. Afterwards,
  ''' retrieves the Contact entity record, and then enables the document management 
  ''' for the entity.
  ''' </summary>
  ''' <param name="serverConfig">Contains server connection information.</param>
  ''' <param name="promptforRevert">When True, the user will be prompted to revert all
  ''' the changes done in this sample.</param>
  Public Sub Run(ByVal serverConfig As ServerConnection.Configuration,
                 ByVal promptforRevert 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()

     CreateRequiredRecords()

     ' Retrieve an entity for which you want to enable document management.
     ' In this sample, we will retrieve and enable document management
     ' for the Contact entity because by default, document management is
     ' not enabled for this entity.                    

     Dim entityRequest As RetrieveEntityRequest =
      New RetrieveEntityRequest With {
       .EntityFilters = EntityFilters.All,
       .LogicalName = Contact.EntityLogicalName,
       .RetrieveAsIfPublished = False
      }
     ' Retrieve only the currently published changes, ignoring the changes 
     ' that have not been published.
     Dim entityResponse As RetrieveEntityResponse =
      CType(_serviceProxy.Execute(entityRequest), RetrieveEntityResponse)
     Console.WriteLine("Retrieved the contact entity.")

     ' Get the entity from the response.
     Dim contactEntity As EntityMetadata = entityResponse.EntityMetadata

     ' Enable document management for the retrieved entity.
     contactEntity.IsDocumentManagementEnabled = True

     ' Create an update request.                    
     Dim updateRequest As UpdateEntityRequest =
      New UpdateEntityRequest With {
       .Entity = contactEntity
      }
     _serviceProxy.Execute(updateRequest)

     ' Publish the entity.
     ' All customizations must be published before they can be used.
     Dim enableRequest As New PublishAllXmlRequest()
     _serviceProxy.Execute(enableRequest)

     ' Retrieve the contact entity, and verify that document management is enabled.
     entityResponse = CType(_serviceProxy.Execute(entityRequest), RetrieveEntityResponse)
     contactEntity = entityResponse.EntityMetadata
     If contactEntity.IsDocumentManagementEnabled.Value = True Then
      Console.WriteLine("Enabled document management for the contact entity.")
     End If

     RevertChanges(promptforRevert)
    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

  ''' <summary>
  ''' Creates any entity records that this sample requires.
  ''' </summary>
  Public Sub CreateRequiredRecords()
   ' For this sample, no entities are created.
  End Sub

  ''' <summary>
  ''' Reverts any changes that were done by this sample.
  ''' <param name="prompt">Indicates whether to prompt the user 
  ''' to revert the changes done by this sample.</param>
  ''' </summary>
  Public Sub RevertChanges(ByVal prompt As Boolean)
   Dim revertChanges As Boolean = True

   If prompt Then
    Console.WriteLine(vbLf &amp; "Do you want these changes reverted? (y/n) [y]: ")
    Dim answer As String = Console.ReadLine()

    revertChanges = (answer.StartsWith("y") OrElse answer.StartsWith("Y") OrElse answer = String.Empty)
   End If

   If revertChanges Then
    Dim entityRequest As RetrieveEntityRequest =
     New RetrieveEntityRequest With {
      .EntityFilters = EntityFilters.All,
      .LogicalName = Contact.EntityLogicalName,
      .RetrieveAsIfPublished = False
     }
    Dim entityResponse As RetrieveEntityResponse =
     CType(_serviceProxy.Execute(entityRequest), RetrieveEntityResponse)

    Dim contactEntity As EntityMetadata = entityResponse.EntityMetadata

    ' Disable document management for the retrieved entity.
    contactEntity.IsDocumentManagementEnabled = False

    Dim updateRequest As UpdateEntityRequest =
     New UpdateEntityRequest With {
      .Entity = contactEntity
     }
    _serviceProxy.Execute(updateRequest)

    ' Publish the customizations to the entity.                
    Dim disableRequest As New PublishAllXmlRequest()
    _serviceProxy.Execute(disableRequest)

    Console.WriteLine("Changes have been reverted.")
   End If
  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 EnableDocumentManagement()
    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
   Finally
    Console.WriteLine("Press <Enter> to exit.")
    Console.ReadLine()
   End Try
  End Sub
#End Region ' Main method
 End Class
End Namespace

See Also

Integrate SharePoint with Microsoft Dynamics CRM 2013
Enable document management for entities
Sample: Create, retrieve, update, and delete a SharePoint location record
Helper code: ServerConnection class
Sample: Create, retrieve, update, and delete a SharePoint location record