샘플: 엔터티 이미지 설정 및 검색

 

게시 날짜: 2017년 1월

적용 대상: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

이 샘플 코드는 Microsoft Dynamics 365(온라인 및 온-프레미스)용입니다.Microsoft Dynamics CRM SDK 패키지를 다운로드합니다. 다운로드 패키지의 다음 위치에서 확인할 수 있습니다.

SampleCode\CS\GeneralProgramming\LateBound\EntityImages.cs

요구 사항

이 SDK에서 제공된 샘플 코드를 실행하기 위한 요구 사항에 대한 자세한 내용은 샘플 및 도우미 코드 사용을 참조하십시오.

보여 주기

이 샘플에서는 엔터티 이미지에 대한 데이터를 설정하고 검색하는 방법을 보여 줍니다.

샘플에서는 다음과 같은 작업을 수행합니다.

  1. 다음에 CreateImageAttributeDemoEntity 메서드를 사용합니다.

    1. 스키마 이름이 sample_ImageAttributeDemo인 사용자 지정 엔터티와 스키마 이름이 sample_Name인 기본 특성을 만듭니다.

    2. 스키마 이름이 EntityImage인 이미지 특성을 만듭니다. 모든 이미지 특성은 이 이름을 사용합니다.

    3. sample_ImageAttributeDemo 엔터티를 위한 기본 양식을 검색하고 업데이트하여 <form> (FormXml)showImage 특성을 true에 설정함으로써 이미지가 양식에 표시되게 합니다.

    4. sample_ImageAttributeDemo 엔터티를 게시합니다.

  2. 다음과 같이 Images 폴더에 있는 다른 크기의 이미지 다섯 개를 사용하여 sample_ImageAttributeDemo 엔터티에 대해 새 레코드를 다섯 개 만듭니다.

    원본 이미지의 상대적인 크기

    sample_Name 기본 특성 필드 값은 파일의 이름입니다.

    각 레코드를 만든 후 원본 이미지가 양식에서 사용할 수 있는 공간에 맞게 크기가 조정되는 방법을 볼 수 있도록 ShowEntityFormInBrowser 메서드를 사용하여 웹 브라우저 응용 프로그램에서 레코드를 볼 수 있습니다.

    
    //Use a 144x144 pixel image
    Entity imageEntity1 = new Entity(_customEntityName.ToLower());
    imageEntity1["sample_name"] = "144x144.png";
    imageEntity1["entityimage"] = File.ReadAllBytes("Images\\144x144.png");
    Guid imageEntity1Id = _serviceProxy.Create(imageEntity1);
    ShowEntityFormInBrowser(promptforDelete, "144x144.png", imageEntity1Id);
    
    //Use a 144x400 pixel image
    Entity imageEntity2 = new Entity(_customEntityName.ToLower());
    imageEntity2["sample_name"] = "144x400.png";
    imageEntity2["entityimage"] = File.ReadAllBytes("Images\\144x400.png");
    Guid imageEntity2Id = _serviceProxy.Create(imageEntity2);     
    ShowEntityFormInBrowser(promptforDelete, "144x400.png", imageEntity2Id);
    
    //Use a 400x144 pixel image
    Entity imageEntity3 = new Entity(_customEntityName.ToLower());
    imageEntity3["sample_name"] = "400x144.png";
    imageEntity3["entityimage"] = File.ReadAllBytes("Images\\400x144.png");
    Guid imageEntity3Id = _serviceProxy.Create(imageEntity3);
    ShowEntityFormInBrowser(promptforDelete, "400x144.png", imageEntity3Id);
    
    //Use a 400x500 pixel image
    Entity imageEntity4 = new Entity(_customEntityName.ToLower());
    imageEntity4["sample_name"] = "400x500.png";
    imageEntity4["entityimage"] = File.ReadAllBytes("Images\\400x500.png");
    Guid imageEntity4Id = _serviceProxy.Create(imageEntity4);
    ShowEntityFormInBrowser(promptforDelete, "400x500.png", imageEntity4Id);
    
    //Use a 60x80 pixel image
    Entity imageEntity5 = new Entity(_customEntityName.ToLower());
    imageEntity5["sample_name"] = "60x80.png";
    imageEntity5["entityimage"] = File.ReadAllBytes("Images\\60x80.png");
    Guid imageEntity5Id = _serviceProxy.Create(imageEntity5);
    ShowEntityFormInBrowser(promptforDelete, "60x80.png", imageEntity5Id);
    

    참고

    엔터티가 샘플에 만들어지고 강력한 유형의 클래스로 사용할 수 없으므로 이 코드는 런타임에 바인딩 엔터티를 사용합니다. 하지만 초기 바인딩 클래스가 생성된 후 강력한 유형의 클래스를 사용할 수 있습니다.추가 정보:코드 생성 도구(CrmSvcUtil.exe)를 사용하여 초기 바인딩 엔터티 클래스 만들기

  3. entityimage 특성으로 레코드를 검색하고 크기가 조정된 파일을 저장합니다. 샘플을 실행한 후 \bin\Debug 폴더에 저장된 파일을 찾을 수 있습니다.

    
         //Retrieve and download the binary images
         string binaryImageQuery =
    String.Format(@"<fetch mapping='logical'>
      <entity name='{0}'>
        <attribute name='sample_name' />
        <attribute name='entityimage' />
      </entity>
    </fetch>",_customEntityName.ToLower());
    
         EntityCollection binaryImageResults = _serviceProxy.RetrieveMultiple(new FetchExpression(binaryImageQuery));
    
    
         Console.WriteLine("Records retrieved and image files saved to: {0}", Directory.GetCurrentDirectory());
         foreach (Entity record in binaryImageResults.Entities)
         {
          String recordName = record["sample_name"] as String;
          String downloadedFileName = String.Format("Downloaded_{0}", recordName);
          byte[] imageBytes = record["entityimage"] as byte[];
          var fs = new BinaryWriter(new FileStream(downloadedFileName, FileMode.Append, FileAccess.Write));
          fs.Write(imageBytes);
          fs.Close();
          Console.WriteLine(downloadedFileName);
         }
    

    이미지 파일은 다음과 같이 크기가 조정됩니다.

    다운로드된 이미지의 상대적인 크기

  4. entityimage_url 특성으로 레코드를 검색하고 응용 프로그램에 포함할 수 있는 상대 URL 값을 표시하여 이미지에 액세스합니다. 전송되는 데이터 양이 더 작으므로 이 쿼리는 더 많이 반응해야 합니다.

    
         //Retrieve and the records with just the url
         string imageUrlQuery =
    String.Format(@"<fetch mapping='logical'>
      <entity name='{0}'>
        <attribute name='sample_name' />
        <attribute name='entityimage_url' />
      </entity>
    </fetch>", _customEntityName.ToLower());
    
         EntityCollection imageUrlResults = _serviceProxy.RetrieveMultiple(new FetchExpression(imageUrlQuery));
    
    
         Console.WriteLine("These are the relative URLs for the images retrieved:");
         foreach (Entity record in imageUrlResults.Entities)
         {
          String imageUrl = record["entityimage_url"] as String;
          Console.WriteLine(imageUrl);
         }
    

    상대 URL은 다음 예제와 같이 보입니다.

    • /Image/download.aspx?Entity=sample_imageattributedemo&Attribute=entityimage&Id=2ed5a666-7b51-e311-9088-00155d9c5607&Timestamp=635205044273046819

    • /Image/download.aspx?Entity=sample_imageattributedemo&Attribute=entityimage&Id=22f17c6d-7b51-e311-9088-00155d9c5607&Timestamp=635205044320978850

    • /Image/download.aspx?Entity=sample_imageattributedemo&Attribute=entityimage&Id=24f17c6d-7b51-e311-9088-00155d9c5607&Timestamp=635205044333049824

    • /Image/download.aspx?Entity=sample_imageattributedemo&Attribute=entityimage&Id=26f17c6d-7b51-e311-9088-00155d9c5607&Timestamp=635205044343080227

    • /Image/download.aspx?Entity=sample_imageattributedemo&Attribute=entityimage&Id=28f17c6d-7b51-e311-9088-00155d9c5607&Timestamp=635205044353421800

DeleteImageAttributeDemoEntity 메서드를 사용하면 sample_ImageAttributeDemo 엔터티를 삭제하고 조직을 샘플을 실행하기 전의 상태로 되돌릴 수 있습니다. 이 엔터티를 삭제할 때까지 샘플을 다시 실행할 수 없습니다.

예제

다음은 샘플의 전체 코드입니다.


using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Diagnostics;
using System.Xml.Linq;
using System.IO;

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

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

namespace Microsoft.Crm.Sdk.Samples
{
 /// <summary>
 /// Demonstrates how to do create and retrieve records with image data
 /// </summary>
 /// <remarks>
 /// At run-time, you will be given the option to delete all the
 /// database records created by this program.</remarks>
 public class EntityImages
 {
  #region Class Level Members
  private OrganizationServiceProxy _serviceProxy;
  private const String _customEntityName = "sample_ImageAttributeDemo";
  #endregion Class Level Members

  #region How To Sample Code
  /// <summary>
  /// This method first connects to the Organization service. Afterwards,
  /// a custom entity is created and configured to support entity images.
  /// Then records are created using this custom entity with image data. 
  /// The records are then retrieved and the resized images are saved.
  /// Finally, the custom entity can be deleted.
  /// </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();

     Console.WriteLine("Please wait while the custom Image Attribute Demo entity used by this sample is created.");
     //Creates the Image Attribute Demo entity used in this sample
     CreateImageAttributeDemoEntity();

     //Create 5 records using different sized images.
     /*

      This sample uses late-binding because the entity was just created and is
      not included in the 'MyOrganizationsCrmSdkTypes.cs' file created by the 
      code generation tool (CrmSvcUtil.exe)

     */
     //Use a 144x144 pixel image
     Entity imageEntity1 = new Entity(_customEntityName.ToLower());
     imageEntity1["sample_name"] = "144x144.png";
     imageEntity1["entityimage"] = File.ReadAllBytes("Images\\144x144.png");
     Guid imageEntity1Id = _serviceProxy.Create(imageEntity1);
     ShowEntityFormInBrowser(promptforDelete, "144x144.png", imageEntity1Id);

     //Use a 144x400 pixel image
     Entity imageEntity2 = new Entity(_customEntityName.ToLower());
     imageEntity2["sample_name"] = "144x400.png";
     imageEntity2["entityimage"] = File.ReadAllBytes("Images\\144x400.png");
     Guid imageEntity2Id = _serviceProxy.Create(imageEntity2);     
     ShowEntityFormInBrowser(promptforDelete, "144x400.png", imageEntity2Id);

     //Use a 400x144 pixel image
     Entity imageEntity3 = new Entity(_customEntityName.ToLower());
     imageEntity3["sample_name"] = "400x144.png";
     imageEntity3["entityimage"] = File.ReadAllBytes("Images\\400x144.png");
     Guid imageEntity3Id = _serviceProxy.Create(imageEntity3);
     ShowEntityFormInBrowser(promptforDelete, "400x144.png", imageEntity3Id);

     //Use a 400x500 pixel image
     Entity imageEntity4 = new Entity(_customEntityName.ToLower());
     imageEntity4["sample_name"] = "400x500.png";
     imageEntity4["entityimage"] = File.ReadAllBytes("Images\\400x500.png");
     Guid imageEntity4Id = _serviceProxy.Create(imageEntity4);
     ShowEntityFormInBrowser(promptforDelete, "400x500.png", imageEntity4Id);

     //Use a 60x80 pixel image
     Entity imageEntity5 = new Entity(_customEntityName.ToLower());
     imageEntity5["sample_name"] = "60x80.png";
     imageEntity5["entityimage"] = File.ReadAllBytes("Images\\60x80.png");
     Guid imageEntity5Id = _serviceProxy.Create(imageEntity5);
     ShowEntityFormInBrowser(promptforDelete, "60x80.png", imageEntity5Id);
     Console.WriteLine();
     //Retrieve and download the binary images
     string binaryImageQuery =
String.Format(@"<fetch mapping='logical'>
  <entity name='{0}'>
    <attribute name='sample_name' />
    <attribute name='entityimage' />
  </entity>
</fetch>",_customEntityName.ToLower());

     EntityCollection binaryImageResults = _serviceProxy.RetrieveMultiple(new FetchExpression(binaryImageQuery));


     Console.WriteLine("Records retrieved and image files saved to: {0}", Directory.GetCurrentDirectory());
     foreach (Entity record in binaryImageResults.Entities)
     {
      String recordName = record["sample_name"] as String;
      String downloadedFileName = String.Format("Downloaded_{0}", recordName);
      byte[] imageBytes = record["entityimage"] as byte[];
      var fs = new BinaryWriter(new FileStream(downloadedFileName, FileMode.Append, FileAccess.Write));
      fs.Write(imageBytes);
      fs.Close();
      Console.WriteLine(downloadedFileName);
     }
     Console.WriteLine();
     //Retrieve and the records with just the url
     string imageUrlQuery =
String.Format(@"<fetch mapping='logical'>
  <entity name='{0}'>
    <attribute name='sample_name' />
    <attribute name='entityimage_url' />
  </entity>
</fetch>", _customEntityName.ToLower());

     EntityCollection imageUrlResults = _serviceProxy.RetrieveMultiple(new FetchExpression(imageUrlQuery));


     Console.WriteLine("These are the relative URLs for the images retrieved:");
     foreach (Entity record in imageUrlResults.Entities)
     {
      String imageUrl = record["entityimage_url"] as String;
      Console.WriteLine(imageUrl);
     }


     DeleteImageAttributeDemoEntity(promptforDelete);
    }
   }

   // 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 CreateImageAttributeDemoEntity()
  {
   //Create a Custom entity
   CreateEntityRequest createrequest = new CreateEntityRequest
   {

    //Define the entity
    Entity = new EntityMetadata
    {
     SchemaName = _customEntityName,
     DisplayName = new Label("Image Attribute Demo", 1033),
     DisplayCollectionName = new Label("Image Attribute Demos", 1033),
     Description = new Label("An entity created by an SDK sample to demonstrate how to upload and retrieve entity images.", 1033),
     OwnershipType = OwnershipTypes.UserOwned,
     IsActivity = false,

    },

    // Define the primary attribute for the entity
    PrimaryAttribute = new StringAttributeMetadata
    {
     SchemaName = "sample_Name",
     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
     MaxLength = 100,
     FormatName = StringFormatName.Text,
     DisplayName = new Label("Name", 1033),
     Description = new Label("The primary attribute for the Image Attribute Demo entity.", 1033)
    }

   };
   _serviceProxy.Execute(createrequest);
   Console.WriteLine("The Image Attribute Demo entity has been created.");

   //Create an Image attribute for the custom entity
   // Only one Image attribute can be added to an entity that doesn't already have one.
   CreateAttributeRequest createEntityImageRequest = new CreateAttributeRequest
   {
    EntityName = _customEntityName.ToLower(),
    Attribute = new ImageAttributeMetadata
    {
     SchemaName = "EntityImage", //The name is always EntityImage
     //Required level must be AttributeRequiredLevel.None
     RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), 
     DisplayName = new Label("Image", 1033),
     Description = new Label("An image to show with this demonstration.", 1033)

    }
   };
   _serviceProxy.Execute(createEntityImageRequest);
   Console.WriteLine("The Image attribute has been created.");

   QueryExpression qe = new QueryExpression("systemform");
   qe.Criteria.AddCondition("type", ConditionOperator.Equal, 2); //main form
   qe.Criteria.AddCondition("objecttypecode", ConditionOperator.Equal, _customEntityName.ToLower()); 
   qe.ColumnSet.AddColumn("formxml");

   SystemForm ImageAttributeDemoMainForm = (SystemForm)_serviceProxy.RetrieveMultiple(qe).Entities[0];

   XDocument ImageAttributeDemoMainFormXml = XDocument.Parse(ImageAttributeDemoMainForm.FormXml);
   //Set the showImage attribute so the entity image will be displayed
   ImageAttributeDemoMainFormXml.Root.SetAttributeValue("showImage", true);

   //Updating the entity form definition
   ImageAttributeDemoMainForm.FormXml = ImageAttributeDemoMainFormXml.ToString();

   _serviceProxy.Update(ImageAttributeDemoMainForm);
   Console.WriteLine("The Image Attribute Demo main form has been updated to show images.");


   PublishXmlRequest pxReq1 = new PublishXmlRequest { ParameterXml = String.Format(@"
   <importexportxml>
    <entities>
     <entity>{0}</entity>
    </entities>
   </importexportxml>", _customEntityName.ToLower()) };
   _serviceProxy.Execute(pxReq1);

   Console.WriteLine("The Image Attribute Demo entity was published");
  }


  public void ShowEntityFormInBrowser(bool prompt, String name, Guid id)
  {
   if (prompt)
   {
    Console.WriteLine("\nDo you want to view record '{0}' form? (y/n)", name);
    String viewFormAnswer = Console.ReadLine();
    if (viewFormAnswer.StartsWith("y") || viewFormAnswer.StartsWith("Y"))
    {
     try
     {
      String webServiceURL = _serviceProxy.EndpointSwitch.PrimaryEndpoint.AbsoluteUri;
      String entityInDefaultSolutionUrl = webServiceURL.Replace("XRMServices/2011/Organization.svc",
       String.Format("main.aspx?etn={0}&amp;pagetype=entityrecord&amp;id=%7B{1}%7D", _customEntityName.ToLower(),id.ToString()));

      ProcessStartInfo browserProcess = new ProcessStartInfo("iexplore.exe");
      browserProcess.Arguments = entityInDefaultSolutionUrl;
      Process.Start(browserProcess);

     }
     catch (SystemException)
     {
      Console.WriteLine("\nThere was a problem opening Internet Explorer.");
     }
    }
   }
  }

  /// <summary>
  /// Deletes any entity records that were created for this sample.
  /// <param name="prompt">Indicates whether to prompt the user 
  /// to delete the records created in this sample.</param>
  /// </summary>
  public void DeleteImageAttributeDemoEntity(bool prompt)
  {
   bool deleteRecords = true;

   if (prompt)
   {
    Console.WriteLine("\nDo you want to delete the entity created for this sample? (y/n) [y]: ");
    String answer = Console.ReadLine();

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

   if (deleteRecords)
   {
    DeleteEntityRequest der = new DeleteEntityRequest() { LogicalName = _customEntityName.ToLower() };
    _serviceProxy.Execute(der);
    Console.WriteLine("The Image Attribute Demo entity has been deleted.");
   }
  }

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

    EntityImages app = new EntityImages();
    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("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("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
 }
}

참고 항목

이미지 특성
Microsoft Dynamics 365의 엔터티 특성 소개
Microsoft Dynamics 365에서 엔터티에 대한 소개

Microsoft Dynamics 365

© 2017 Microsoft. All rights reserved. 저작권 정보