分享方式:


範例:設定及擷取實體影像

 

發佈日期: 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 所提供範例程式碼的需求資訊,請參閱使用範例和 Helper 程式碼

示範

此範例顯示如何為實體影像設定和擷取資料。

範例執行以下工作:

  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. 著作權所有,並保留一切權利。 著作權