web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to set (SET) a custom 'option set' in a custom field?

(0) ShareShare
ReportReport
Posted on by

//this snipped is for getting.....

var _entityName = "systemuser"; //user
var fieldName = "new_officetype"; //option set field

RetrieveEntityRequest entityRequest = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.All,
LogicalName = _entityName,
RetrieveAsIfPublished = true
};

// Get the Response and MetaData. Then convert to Option MetaData Array.
RetrieveEntityResponse entityResponse = (RetrieveEntityResponse)_orgService.Execute(entityRequest);
EntityMetadata metadata = entityResponse.EntityMetadata;
PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, fieldName, StringComparison.OrdinalIgnoreCase)) as PicklistAttributeMetadata;
OptionSetMetadata optionsSetMet = picklistMetadata.OptionSet;
var optionlist = (from o in optionsSetMet.Options
select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList();
foreach (var item in optionlist)
{
Console.WriteLine(item.Text + " " + item.Value);
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ashlega Profile Picture
    34,477 on at
    RE: How to set (SET) a custom 'option set' in a custom field?

    Hi,

     not sure if this is what you are asking about, but you can set optionset value like this:

    Entity ent = new Entity("systemuser");

    ent.Id = <Id>;

    ent["new_officetype"] = new OptionSetValue(100000000);

    service.Update(ent);

  • Community Member Profile Picture
    on at
    RE: How to set (SET) a custom 'option set' in a custom field?

    Actually I would like to update the labels in the custom option field but there is only a Get method!?

  • Suggested answer
    ashlega Profile Picture
    34,477 on at
    RE: How to set (SET) a custom 'option set' in a custom field?

    Here is an example:

    msdn.microsoft.com/.../gg509070.aspx

    For a local optionset, you'll need to use different attributes(EntityLogicalName and AttributeLogicalName):

    msdn.microsoft.com/.../microsoft.xrm.sdk.messages.updateoptionvaluerequest.aspx

  • Community Member Profile Picture
    on at
    RE: How to set (SET) a custom 'option set' in a custom field?

    I have got this error:

    Code: -2147187709

    Message: Cannot access directly the option set and option values using option set name because the option set is not Global

  • ashlega Profile Picture
    34,477 on at
    RE: How to set (SET) a custom 'option set' in a custom field?

    Please post the code you used when you got this error

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans