次の方法で共有


属性メタデータに関する作業

 

公開日: 2016年11月

対象: Dynamics CRM 2015

このトピックでは、「サンプル: 属性メタデータに関する作業」のコード スニペットを使用して属性操作の一般的な方法を説明します。

このトピックの内容

属性の作成

属性の取得

属性の更新

検索属性の作成

状態値の更新

グローバル オプション セットを使用する候補リストの作成

新しいステータス値の挿入

ローカル オプション セットへの新規オプションの挿入

ローカル オプション セット内のオプションの順序の変更

属性の削除

属性の作成

属性を作成するには、いずれかの AttributeMetadata 型を定義し、それを CreateAttributeRequest メッセージに渡します。

次のサンプルでは、いくつかの異なる属性型の AttributeMetadata が定義され、List<AttributeMetadata> に追加されます。 コードの最後のところで、それらの属性定義が CreateAttributeRequest class and the attribute is created using the Execute メソッドのインスタンスに渡されます。

次のサンプルでは、現在のカスタマイズ接頭辞が "new" であると仮定しています。それが組織のソリューション発行者の既定のカスタマイズ接頭辞だからです。 実際には現実のソリューション コンテキストに沿ったソリューション発行者のカスタマイズ接頭辞を使うようにしてください。


// Create storage for new attributes being created
addedAttributes = new List<AttributeMetadata>();

// Create a boolean attribute
BooleanAttributeMetadata boolAttribute = new BooleanAttributeMetadata
{
    // Set base properties
    SchemaName = "new_boolean",
    DisplayName = new Label("Sample Boolean", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("Boolean Attribute", _languageCode),
    // Set extended properties
    OptionSet = new BooleanOptionSetMetadata(
        new OptionMetadata(new Label("True", _languageCode), 1),
        new OptionMetadata(new Label("False", _languageCode), 0)
        )
};

// Add to list
addedAttributes.Add(boolAttribute);

// Create a date time attribute
DateTimeAttributeMetadata dtAttribute = new DateTimeAttributeMetadata
{
    // Set base properties
    SchemaName = "new_datetime",
    DisplayName = new Label("Sample DateTime", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("DateTime Attribute", _languageCode),
    // Set extended properties
    Format = DateTimeFormat.DateOnly,
    ImeMode = ImeMode.Disabled
};

// Add to list
addedAttributes.Add(dtAttribute);

// Create a decimal attribute   
DecimalAttributeMetadata decimalAttribute = new DecimalAttributeMetadata
{
    // Set base properties
    SchemaName = "new_decimal",
    DisplayName = new Label("Sample Decimal", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("Decimal Attribute", _languageCode),
    // Set extended properties
    MaxValue = 100,
    MinValue = 0,
    Precision = 1
};

// Add to list
addedAttributes.Add(decimalAttribute);

// Create a integer attribute   
IntegerAttributeMetadata integerAttribute = new IntegerAttributeMetadata
{
    // Set base properties
    SchemaName = "new_integer",
    DisplayName = new Label("Sample Integer", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("Integer Attribute", _languageCode),
    // Set extended properties
    Format = IntegerFormat.None,
    MaxValue = 100,
    MinValue = 0
};

// Add to list
addedAttributes.Add(integerAttribute);

// Create a memo attribute 
MemoAttributeMetadata memoAttribute = new MemoAttributeMetadata
{
    // Set base properties
    SchemaName = "new_memo",
    DisplayName = new Label("Sample Memo", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("Memo Attribute", _languageCode),
    // Set extended properties
    Format = StringFormat.TextArea,
    ImeMode = ImeMode.Disabled,
    MaxLength = 500
};

// Add to list
addedAttributes.Add(memoAttribute);

// Create a money attribute 
MoneyAttributeMetadata moneyAttribute = new MoneyAttributeMetadata
{
    // Set base properties
    SchemaName = "new_money",
    DisplayName = new Label("Money Picklist", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("Money Attribue", _languageCode),
    // Set extended properties
    MaxValue = 1000.00,
    MinValue = 0.00,
    Precision = 1,
    PrecisionSource = 1,
    ImeMode = ImeMode.Disabled
};

// Add to list
addedAttributes.Add(moneyAttribute);

// Create a picklist attribute  
PicklistAttributeMetadata pickListAttribute =
    new PicklistAttributeMetadata
{
    // Set base properties
    SchemaName = "new_picklist",
    DisplayName = new Label("Sample Picklist", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("Picklist Attribute", _languageCode),
    // Set extended properties
    // Build local picklist options
    OptionSet = new OptionSetMetadata
        {
            IsGlobal = false,
            OptionSetType = OptionSetType.Picklist,
            Options = 
        {
            new OptionMetadata(
                new Label("Created", _languageCode), null),
            new OptionMetadata(
                new Label("Updated", _languageCode), null),
            new OptionMetadata(
                new Label("Deleted", _languageCode), null)
        }
        }
};

// Add to list
addedAttributes.Add(pickListAttribute);

// Create a string attribute
StringAttributeMetadata stringAttribute = new StringAttributeMetadata
{
    // Set base properties
    SchemaName = "new_string",
    DisplayName = new Label("Sample String", _languageCode),
    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
    Description = new Label("String Attribute", _languageCode),
    // Set extended properties
    MaxLength = 100
};

// Add to list
addedAttributes.Add(stringAttribute);

// NOTE: LookupAttributeMetadata cannot be created outside the context of a relationship.
// Refer to the WorkWithRelationships.cs reference SDK sample for an example of this attribute type.

// NOTE: StateAttributeMetadata and StatusAttributeMetadata cannot be created via the SDK.

foreach (AttributeMetadata anAttribute in addedAttributes)
{
    // Create the request.
    CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
    {
        EntityName = Contact.EntityLogicalName,
        Attribute = anAttribute
    };

    // Execute the request.
    _serviceProxy.Execute(createAttributeRequest);

    Console.WriteLine("Created the attribute {0}.", anAttribute.SchemaName);
}

' Create storage for new attributes being created
addedAttributes = New List(Of AttributeMetadata)()

' Create a boolean attribute
Dim boolAttribute As BooleanAttributeMetadata = New BooleanAttributeMetadata With {
 .SchemaName = "new_boolean",
 .DisplayName = New Label("Sample Boolean", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Boolean Attribute", _languageCode),
 .OptionSet = New BooleanOptionSetMetadata(
  New OptionMetadata(
   New Label("True", _languageCode), 1),
   New OptionMetadata(
   New Label("False", _languageCode), 0)
  )
}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(boolAttribute)

' Create a date time attribute
Dim dtAttribute As DateTimeAttributeMetadata = New DateTimeAttributeMetadata With {
 .SchemaName = "new_datetime",
 .DisplayName = New Label("Sample DateTime", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("DateTime Attribute", _languageCode),
 .Format = DateTimeFormat.DateOnly,
 .ImeMode = ImeMode.Disabled}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(dtAttribute)

' Create a decimal attribute    
Dim decimalAttribute As DecimalAttributeMetadata = New DecimalAttributeMetadata With {
 .SchemaName = "new_decimal",
 .DisplayName = New Label("Sample Decimal", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Decimal Attribute", _languageCode),
 .MaxValue = 100,
 .MinValue = 0,
 .Precision = 1}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(decimalAttribute)

' Create a integer attribute    
Dim integerAttribute As IntegerAttributeMetadata = New IntegerAttributeMetadata With {
 .SchemaName = "new_integer",
 .DisplayName = New Label("Sample Integer", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Integer Attribute", _languageCode),
 .Format = IntegerFormat.None,
 .MaxValue = 100,
 .MinValue = 0}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(integerAttribute)

' Create a memo attribute 
Dim memoAttribute As MemoAttributeMetadata = New MemoAttributeMetadata With {
 .SchemaName = "new_memo",
 .DisplayName = New Label("Sample Memo", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Memo Attribute", _languageCode),
 .Format = StringFormat.TextArea,
 .ImeMode = ImeMode.Disabled,
 .MaxLength = 500}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(memoAttribute)

' Create a money attribute    
Dim moneyAttribute As MoneyAttributeMetadata = New MoneyAttributeMetadata With {
 .SchemaName = "new_money",
 .DisplayName = New Label("Money Picklist", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Money Attribue", _languageCode),
 .MaxValue = 1000.0,
 .MinValue = 0.0,
 .Precision = 1,
 .PrecisionSource = 1,
 .ImeMode = ImeMode.Disabled}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(moneyAttribute)

' Create a picklist attribute    
Dim pickListAttribute As PicklistAttributeMetadata = New PicklistAttributeMetadata With {
 .SchemaName = "new_picklist",
 .DisplayName = New Label("Sample Picklist", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("Picklist Attribute", _languageCode)}
Dim pickListOptionSetMetadata As OptionSetMetadata = New OptionSetMetadata() With {
 .IsGlobal = False,
 .OptionSetType = OptionSetType.Picklist}
pickListOptionSetMetadata.Options.Add(New OptionMetadata(New Label("Created", _languageCode), Nothing))
pickListOptionSetMetadata.Options.Add(New OptionMetadata(New Label("Updated", _languageCode), Nothing))
pickListOptionSetMetadata.Options.Add(New OptionMetadata(New Label("Deleted", _languageCode), Nothing))
pickListAttribute.OptionSet = pickListOptionSetMetadata
' Set base properties
' Set extended properties
' Build local picklist options

' Add to list
addedAttributes.Add(pickListAttribute)

' Create a string attribute
Dim stringAttribute As StringAttributeMetadata = New StringAttributeMetadata With {
 .SchemaName = "new_string",
 .DisplayName = New Label("Sample String", _languageCode),
 .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
 .Description = New Label("String Attribute", _languageCode),
 .MaxLength = 100}
' Set base properties
' Set extended properties

' Add to list
addedAttributes.Add(stringAttribute)

' NOTE: LookupAttributeMetadata cannot be created outside the context of a relationship.
' Refer to the WorkWithRelationships.cs reference SDK sample for an example of this attribute type.

' NOTE: StateAttributeMetadata and StatusAttributeMetadata cannot be created via the SDK.

For Each anAttribute As AttributeMetadata In addedAttributes
 ' Create the request.
 Dim createAttributeRequest As CreateAttributeRequest = New CreateAttributeRequest With {
  .EntityName = Contact.EntityLogicalName,
  .Attribute = anAttribute}

 ' Execute the request.
 _serviceProxy.Execute(createAttributeRequest)

 Console.WriteLine("Created the attribute {0}.", anAttribute.SchemaName)
Next anAttribute

属性の取得

このサンプルは RetrieveAttributeRequest を使用して、属性のAttributeMetadata を取得する方法を説明します。 このサンプルは、"new_string" という名前のカスタム StringAttributeMetadata 属性のメタデータを、以前「属性の作成」で作成した Contact エンティティから取得します。

注意

RetrieveAsIfPublished の値が true なので、この要求はこの属性の現在非公開の定義を返します。 属性エディターを開発していて、属性の非公開の定義を取得する必要があるような状況では、これを使うことも考えられます。 そうでなければ、RetrieveAsIfPublished は指定しないでください。詳細:非公開メタデータの取得


// Create the request
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
{
    EntityLogicalName = Contact.EntityLogicalName,
    LogicalName = "new_string",
    RetrieveAsIfPublished = true
};

// Execute the request
RetrieveAttributeResponse attributeResponse =
    (RetrieveAttributeResponse)_serviceProxy.Execute(attributeRequest);

Console.WriteLine("Retrieved the attribute {0}.",
    attributeResponse.AttributeMetadata.SchemaName);

' Create the request
Dim attributeRequest As RetrieveAttributeRequest = New RetrieveAttributeRequest With {
 .EntityLogicalName = Contact.EntityLogicalName,
 .LogicalName = "new_string",
 .RetrieveAsIfPublished = True}

' Execute the request
Dim attributeResponse As RetrieveAttributeResponse = CType(_serviceProxy.Execute(attributeRequest), RetrieveAttributeResponse)

Console.WriteLine("Retrieved the attribute {0}.", attributeResponse.AttributeMetadata.SchemaName)

属性の更新

このサンプルは属性の更新方法を示しています。 このサンプルは、UpdateAttributeRequest を使用して、Contact エンティティについて以前取得したカスタム属性の AttributeMetadata.DisplayName プロパティを変更します。


// Modify the retrieved attribute
AttributeMetadata retrievedAttributeMetadata =
    attributeResponse.AttributeMetadata;
retrievedAttributeMetadata.DisplayName =
    new Label("Update String Attribute", _languageCode);

// Update an attribute retrieved via RetrieveAttributeRequest
UpdateAttributeRequest updateRequest = new UpdateAttributeRequest
{
    Attribute = retrievedAttributeMetadata,
    EntityName = Contact.EntityLogicalName,
    MergeLabels = false
};

// Execute the request
_serviceProxy.Execute(updateRequest);

Console.WriteLine("Updated the attribute {0}.",
    retrievedAttributeMetadata.SchemaName);

' Modify the retrieved attribute
Dim retrievedAttributeMetadata As AttributeMetadata = attributeResponse.AttributeMetadata
retrievedAttributeMetadata.DisplayName = New Label("Update String Attribute", _languageCode)

' Update an attribute retrieved via RetrieveAttributeRequest
Dim updateRequest As UpdateAttributeRequest = New UpdateAttributeRequest With {
 .Attribute = retrievedAttributeMetadata,
 .EntityName = Contact.EntityLogicalName,
 .MergeLabels = False}

' Execute the request
_serviceProxy.Execute(updateRequest)

Console.WriteLine("Updated the attribute {0}.", retrievedAttributeMetadata.SchemaName)

検索属性の作成

このサンプルでは、検索属性の作成方法を説明します。

検索属性の作成には CreateOneToManyRequest を使用します。


CreateOneToManyRequest req = new CreateOneToManyRequest()
{
 Lookup = new LookupAttributeMetadata()
 {
  Description = new Label("The owner of the bank account", 1033),
  DisplayName = new Label("Account Owner", 1033),
  LogicalName = "new_parent_contactid",
  SchemaName = "New_Parent_ContactId",
  RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired)
 },
 OneToManyRelationship = new OneToManyRelationshipMetadata()
 {
  AssociatedMenuConfiguration = new AssociatedMenuConfiguration()
  {
   Behavior = AssociatedMenuBehavior.UseCollectionName,
   Group = AssociatedMenuGroup.Details,
   Label = new Label("Bank Accounts", 1033),
   Order = 10000
  },
  CascadeConfiguration = new CascadeConfiguration()
  {
   Assign = CascadeType.Cascade,
   Delete = CascadeType.Cascade,
   Merge = CascadeType.Cascade,
   Reparent = CascadeType.Cascade,
   Share = CascadeType.Cascade,
   Unshare = CascadeType.Cascade
  },
  ReferencedEntity = Contact.EntityLogicalName,
  ReferencedAttribute = "contactid",
  ReferencingEntity = _customEntityName,
  SchemaName = "new_contact_new_bankaccount"
 }
};
_serviceProxy.Execute(req);

Dim req As New CreateOneToManyRequest() With {
 .Lookup = New LookupAttributeMetadata() With {
  .Description = New Label("The owner of the bank account", 1033),
  .DisplayName = New Label("Account Owner", 1033),
  .LogicalName = "new_parent_contactid",
  .SchemaName = "New_Parent_ContactId",
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.ApplicationRequired)},
 .OneToManyRelationship = New OneToManyRelationshipMetadata() With {
  .AssociatedMenuConfiguration = New AssociatedMenuConfiguration() With {
   .Behavior = AssociatedMenuBehavior.UseCollectionName,
   .Group = AssociatedMenuGroup.Details,
   .Label = New Label("Bank Accounts", 1033),
   .Order = 10000},
  .CascadeConfiguration = New CascadeConfiguration() With {
   .Assign = CascadeType.Cascade,
   .Delete = CascadeType.Cascade,
   .Merge = CascadeType.Cascade,
   .Reparent = CascadeType.Cascade,
   .Share = CascadeType.Cascade,
   .Unshare = CascadeType.Cascade},
  .ReferencedEntity = Contact.EntityLogicalName,
  .ReferencedAttribute = "contactid",
  .ReferencingEntity = _customEntityName,
  .SchemaName = "new_contact_new_bankaccount"
 }
}
_serviceProxy.Execute(req)

グローバル オプション セットを使用する候補リストの作成

このサンプルでは、グローバル オプション セットに関連付けられた PicklistAttributeMetadata 属性の作成方法を示します。

次のサンプルでは、CreateAttributeRequest を使用し、名前が文字列型変数 _globalOptionSetName で表されるグローバル オプション セットが使われるように PicklistAttributeMetadata 属性のオプションを設定しています。詳細:グローバル オプション セットのカスタマイズ


// Create a Picklist linked to the option set.
// Specify which entity will own the picklist, and create it.
CreateAttributeRequest createRequest = new CreateAttributeRequest
{
    EntityName = Contact.EntityLogicalName,
    Attribute = new PicklistAttributeMetadata
    {
        SchemaName = "sample_examplepicklist",
        LogicalName = "sample_examplepicklist",
        DisplayName = new Label("Example Picklist", _languageCode),
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),

        // In order to relate the picklist to the global option set, be sure
        // to specify the two attributes below appropriately.
        // Failing to do so will lead to errors.
        OptionSet = new OptionSetMetadata
        {
            IsGlobal = true,
            Name = _globalOptionSetName
        }
    }
};

_serviceProxy.Execute(createRequest);

' Create a Picklist linked to the option set.
' Specify which entity will own the picklist, and create it.
Dim createRequest As CreateAttributeRequest = New CreateAttributeRequest With {
 .EntityName = Contact.EntityLogicalName,
 .Attribute = New PicklistAttributeMetadata With {
  .SchemaName = "sample_examplepicklist", .LogicalName = "sample_examplepicklist",
  .DisplayName = New Label("Example Picklist", _languageCode),
  .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
  .OptionSet = New OptionSetMetadata With {
   .IsGlobal = True,
   .Name = _globalOptionSetName
  }
 }
}
' In order to relate the picklist to the global option set, be sure
' to specify the two attributes below appropriately.
' Failing to do so will lead to errors.

_serviceProxy.Execute(createRequest)

新しいステータス値の挿入

このサンプルは StatusAttributeMetadata 属性に新しい [ステータス] オプションを挿入する方法を示します。

次のサンプルでは、InsertStatusValueRequest を使用して、Contact エンティティの Contact.StatusCode 属性に対して、Contact.StateCode が 0 (Active) のときに有効となる新しいオプションを指定しています。 この要求は IOrganizationService.Execute メソッドで処理されます。

次のサンプルでは、アクティブな取引先担当者に関する 2 つの有効な**[ステータス]オプションとして、[Active]** (活動中) と [Dormant] (休止中) を使用できるようにします。


// Use InsertStatusValueRequest message to insert a new status 
// in an existing status attribute. 
// Create the request.
InsertStatusValueRequest insertStatusValueRequest =
    new InsertStatusValueRequest
{
    AttributeLogicalName = "statuscode",
    EntityLogicalName = Contact.EntityLogicalName,
    Label = new Label("Dormant", _languageCode),
    StateCode = 0
};

// Execute the request and store newly inserted value 
// for cleanup, used later part of this sample. 
_insertedStatusValue = ((InsertStatusValueResponse)_serviceProxy.Execute(
    insertStatusValueRequest)).NewOptionValue;

Console.WriteLine("Created {0} with the value of {1}.",
    insertStatusValueRequest.Label.LocalizedLabels[0].Label,
    _insertedStatusValue);

' Use InsertStatusValueRequest message to insert a new status 
' in an existing status attribute. 
' Create the request.
Dim insertStatusValueRequest As InsertStatusValueRequest = New InsertStatusValueRequest With {
 .AttributeLogicalName = "statuscode",
 .EntityLogicalName = Contact.EntityLogicalName,
 .Label = New Label("Dormant", _languageCode),
 .StateCode = 0}

' Execute the request and store newly inserted value 
' for cleanup, used later part of this sample. 
_insertedStatusValue = (CType(_serviceProxy.Execute(insertStatusValueRequest), InsertStatusValueResponse)).NewOptionValue

Console.WriteLine("Created {0} with the value of {1}.", insertStatusValueRequest.Label.LocalizedLabels(0).Label, _insertedStatusValue)

状態値の更新

このサンプルは StateAttributeMetadata 属性のオプションのラベルの変更方法を示します。

次のサンプルでは、UpdateStateValueRequest を使用して Contact.StateCode オプションのラベルを [Active] から [Open] に変更します。


// Modify the state value label from Active to Open.
// Create the request.
UpdateStateValueRequest updateStateValue = new UpdateStateValueRequest
{
    AttributeLogicalName = "statecode",
    EntityLogicalName = Contact.EntityLogicalName,
    Value = 1,
    Label = new Label("Open", _languageCode)
};

// Execute the request.
_serviceProxy.Execute(updateStateValue);

Console.WriteLine(
    "Updated {0} state attribute of {1} entity from 'Active' to '{2}'.",
    updateStateValue.AttributeLogicalName,
    updateStateValue.EntityLogicalName,
    updateStateValue.Label.LocalizedLabels[0].Label
    );

' Modify the state value label from Active to Open.
' Create the request.
Dim updateStateValue As UpdateStateValueRequest = New UpdateStateValueRequest With {
 .AttributeLogicalName = "statecode",
 .EntityLogicalName = Contact.EntityLogicalName,
 .Value = 1,
 .Label = New Label("Open", _languageCode)}

' Execute the request.
_serviceProxy.Execute(updateStateValue)

Console.WriteLine("Updated {0} state attribute of {1} entity from 'Active' to '{2}'.",
                  updateStateValue.AttributeLogicalName,
                  updateStateValue.EntityLogicalName,
                  updateStateValue.Label.LocalizedLabels(0).Label)

StateCode オプションの追加や変更はできませんが、そのラベルは変更できます。

ローカル オプション セットへの新規オプションの挿入

このサンプルはローカル オプション セットに新しいオプションを追加する方法を示します。 次のサンプルは、InsertOptionValueRequest を使用して、新しいオプションを Contact エンティティのユーザー定義 PicklistAttributeMetadata 属性に追加します。


// Create a request.
InsertOptionValueRequest insertOptionValueRequest =
    new InsertOptionValueRequest
{
    AttributeLogicalName = "new_picklist",
    EntityLogicalName = Contact.EntityLogicalName,
    Label = new Label("New Picklist Label", _languageCode)
};

// Execute the request.
int insertOptionValue = ((InsertOptionValueResponse)_serviceProxy.Execute(
    insertOptionValueRequest)).NewOptionValue;

Console.WriteLine("Created {0} with the value of {1}.",
    insertOptionValueRequest.Label.LocalizedLabels[0].Label,
    insertOptionValue);

' Create a request.
Dim insertOptionValueRequest As InsertOptionValueRequest = New InsertOptionValueRequest With {
 .AttributeLogicalName = "new_picklist",
 .EntityLogicalName = Contact.EntityLogicalName,
 .Label = New Label("New Picklist Label", _languageCode)}

' Execute the request.
Dim insertOptionValue As Integer = (CType(_serviceProxy.Execute(insertOptionValueRequest), InsertOptionValueResponse)).NewOptionValue

Console.WriteLine("Created {0} with the value of {1}.", insertOptionValueRequest.Label.LocalizedLabels(0).Label, insertOptionValue)

ローカル オプション セット内のオプションの順序の変更

このサンプルはローカル オプション セット内のオプションの順序を変更する方法を示します。 次のサンプルは、ユーザー定義 PicklistAttributeMetadata 属性を取得して、OrderByLINQ 関数を使用した元のオプションの順序を、ラベル テキストの昇順でアイテムを並べ替えることで変更します。 次に、OrderOptionRequest を使用して、属性のオプションのその新しい順序を設定します。

アイテムを降順で並べ替えるには、OrderByDecending linq 関数を使用します。


// Use the RetrieveAttributeRequest message to retrieve  
// a attribute by it's logical name.
RetrieveAttributeRequest retrieveAttributeRequest =
    new RetrieveAttributeRequest
{
    EntityLogicalName = Contact.EntityLogicalName,
    LogicalName = "new_picklist",
    RetrieveAsIfPublished = true
};

// Execute the request.
RetrieveAttributeResponse retrieveAttributeResponse =
    (RetrieveAttributeResponse)_serviceProxy.Execute(
    retrieveAttributeRequest);

// Access the retrieved attribute.
PicklistAttributeMetadata retrievedPicklistAttributeMetadata =
    (PicklistAttributeMetadata)
    retrieveAttributeResponse.AttributeMetadata;

// Get the current options list for the retrieved attribute.
OptionMetadata[] optionList =
    retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray();

// Change the order of the original option's list.
// Use the OrderBy (OrderByDescending) linq function to sort options in  
// ascending (descending) order according to label text.
// For ascending order use this:
var updateOptionList =
    optionList.OrderBy(x => x.Label.LocalizedLabels[0].Label).ToList();

// For descending order use this:
// var updateOptionList =
//      optionList.OrderByDescending(
//      x => x.Label.LocalizedLabels[0].Label).ToList();

// Create the request.
OrderOptionRequest orderOptionRequest = new OrderOptionRequest
{
    // Set the properties for the request.
    AttributeLogicalName = "new_picklist",
    EntityLogicalName = Contact.EntityLogicalName,
    // Set the changed order using Select linq function 
    // to get only values in an array from the changed option list.
    Values = updateOptionList.Select(x => x.Value.Value).ToArray()
};

// Execute the request
_serviceProxy.Execute(orderOptionRequest);

Console.WriteLine("Option Set option order changed");

' Use the RetrieveAttributeRequest message to retrieve  
' a attribute by it's logical name.
Dim retrieveAttributeRequest As RetrieveAttributeRequest = New RetrieveAttributeRequest With {
 .EntityLogicalName = Contact.EntityLogicalName,
 .LogicalName = "new_picklist",
 .RetrieveAsIfPublished = True}

' Execute the request.
Dim retrieveAttributeResponse As RetrieveAttributeResponse = CType(_serviceProxy.Execute(retrieveAttributeRequest), RetrieveAttributeResponse)

' Access the retrieved attribute.
Dim retrievedPicklistAttributeMetadata As PicklistAttributeMetadata = CType(retrieveAttributeResponse.AttributeMetadata, PicklistAttributeMetadata)

' Get the current options list for the retrieved attribute.
Dim optionList() As OptionMetadata = retrievedPicklistAttributeMetadata.OptionSet.Options.ToArray()

' Change the order of the original option's list.
' Use the OrderBy (OrderByDescending) linq function to sort options in  
' ascending (descending) order according to label text.
' For ascending order use this:
Dim updateOptionList = optionList.OrderBy(Function(x) x.Label.LocalizedLabels(0).Label).ToList()

' For descending order use this:
' var updateOptionList =
'      optionList.OrderByDescending(
'      x => x.Label.LocalizedLabels[0].Label).ToList();

' Create the request.
Dim orderOptionRequest As OrderOptionRequest = New OrderOptionRequest With {
 .AttributeLogicalName = "new_picklist",
 .EntityLogicalName = Contact.EntityLogicalName,
 .Values = updateOptionList.Select(Function(x) x.Value.Value).ToArray()}
' Set the properties for the request.
' Set the changed order using Select linq function 
' to get only values in an array from the changed option list.

' Execute the request
_serviceProxy.Execute(orderOptionRequest)

Console.WriteLine("Option Set option order changed")

属性の削除

このサンプルは List<AttributeMetadata> に格納されている、属性の作成で Contact のエンティティのために作成された属性を削除する方法を示します。 各 AttributeMetadata に対して、DeleteAttributeRequest は、IOrganizationService.Execute で処理される要求を準備します。


// Delete all attributes created for this sample.
foreach (AttributeMetadata anAttribute in addedAttributes)
{
    // Create the request object
    DeleteAttributeRequest deleteAttribute = new DeleteAttributeRequest
    {
        // Set the request properties 
        EntityLogicalName = Contact.EntityLogicalName,
        LogicalName = anAttribute.SchemaName
    };
    // Execute the request
    _serviceProxy.Execute(deleteAttribute);
}

' Delete all attributes created for this sample.
For Each anAttribute As AttributeMetadata In addedAttributes
 ' Create the request object
 Dim deleteAttribute As DeleteAttributeRequest = New DeleteAttributeRequest With {
  .EntityLogicalName = Contact.EntityLogicalName,
  .LogicalName = anAttribute.SchemaName}
 ' Set the request properties 
 ' Execute the request
 _serviceProxy.Execute(deleteAttribute)
Next anAttribute

関連項目

エンティティ属性メタデータのカスタマイズ
エンティティ属性のメタデータのメッセージ
サンプル: 属性メタデータに関する作業
エンティティ マッピングおよび属性マッピングのカスタマイズ

© 2017 Microsoft. All rights reserved. 著作権