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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Picklists in the SDK

(0) ShareShare
ReportReport
Posted on by 615

Hello,

I am working on converting our current CRM into Dynamics.  I have most of the ocde written to create accounts and contacts.  I am strugling with the picklists for Relationship Type (accountcategorycode) and Industry (industrycode).  Can someone please post an example in C# or VB that will show me how to create the picklist object and set the correct value from the picklist. 

 Thanks,

 Russell

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    Hi Russel! I did kind of the same thing for my company. We had some database tables from FoxPro that I had to import into CRM. Building a relationship between Accounts and Contacts in code was less of a struggle than picklists! Although, that was my experience... Here is some code that might help you :

    //first I check wheter the DataRow.row column (from the FoxPro table) has data
    if (!String.IsNullOrEmpty(row["rol"].ToString()))  
    {
        //these are my picklist values in CRM :
        /* sales = 1
         * marketing = 2
         * hrm = 3
         * finance = 4
         * principal = 5
         * senior = 6
         * itpp = 7
         * stagiair = 8
         * directie = 9
         * junior = 10
         * medior = 11                       
        */
        
        //this row["rol"] is of type DataRow and the column contains department data
        string rolValue = row["rol"].ToString().ToLower().Trim();
        
        //default picklistvalue to be chosen...
        int pickListValue = 0;
        switch (rolValue)
        {
            case "stage":
                pickListValue = 8;
                break;
            case "junior":
                pickListValue = 10;
                break;
            case "medior":
                pickListValue = 11;
                break;
            case "senior":
                pickListValue = 6;
                break;
            case "senior - it expert center":
                pickListValue = 5;
                break;
            case "partner":
                pickListValue = 0;
                break;
            case "office":
                pickListValue = 0;
                break;
            default:
                pickListValue = 0;
                break;
        }
        //create a new picklist
        CrmContact.new_department = new Picklist();
        if (pickListValue == 0)
        {
            //assign the picklist a default value if its still 0
            CrmContact.new_department.IsNull = true;
            CrmContact.new_department.IsNullSpecified = true;
        }
        else
        {
            //assign the picklist value
            CrmContact.new_department.Value = pickListValue;
        }
    }


    After this, you can call your CrmService and go and Create your CrmContact...
    Good luck!
  • Russell Bahr Profile Picture
    615 on at

    Hello Bert,

     

    That worked perfectly.  Thanks for the help!!  Much Appreciated!!

     

    Regards,

     

    Russell

  • Community Member Profile Picture
    on at

     Glad to help out, Russel

  • hassan chandoklow Profile Picture
    175 on at

    using System;

    using System.IO;

    using System.Text;

    using System.Configuration;

    using System.Collections.Generic;

    using CreatePicklist.MetadataSdk;

    namespace CreatePicklist

    {

    class Program

    {

    private static MetadataService metadata;

    private static int languageCode = 1033;

    static void Main(string[] args)

    {

    string entityName = ConfigurationManager.AppSettings["entity"];

    Authenticate();

    PicklistAttributeMetadata picklist = CreatePicklistAttribute();

    CreateAttributeRequest request =
    new CreateAttributeRequest();

    request.Attribute = picklist;

    request.EntityName = entityName;

    CreateAttributeResponse response = (CreateAttributeResponse)metadata.Execute(request);

    }

    private static PicklistAttributeMetadata CreatePicklistAttribute()

    {

    /* get values from our config file */

    string schemaName = ConfigurationManager.AppSettings["schemaName"];

    string displayName = ConfigurationManager.AppSettings["displayName"];

    string description = ConfigurationManager.AppSettings["description"];

    int requirement = Convert.ToInt32(ConfigurationManager.AppSettings["requirement"]);

    PicklistAttributeMetadata picklist = new PicklistAttributeMetadata();

    picklist.SchemaName = schemaName;

    picklist.DisplayName = CreateSingleLabel(displayName);

    picklist.Description = CreateSingleLabel(description);

    picklist.RequiredLevel =
    new CrmAttributeRequiredLevel();

    /* set the requirment level */

    if (requirement == 0)

    picklist.RequiredLevel.Value = AttributeRequiredLevel.None;

    else if (requirement == 1)

    picklist.RequiredLevel.Value = AttributeRequiredLevel.Recommended;

    else

    picklist.RequiredLevel.Value = AttributeRequiredLevel.Required;

    /* only creating picklists here */

    picklist.AttributeType = new CrmAttributeType();

    picklist.AttributeType.Value = AttributeType.Picklist;

    List<Option> options = CreatePicklistOptions();

    picklist.Options = options.ToArray();

    return picklist;

    }

    private static void Authenticate()

    {

    string username = ConfigurationManager.AppSettings["username"];

    string password = ConfigurationManager.AppSettings["password"];

    string domain = ConfigurationManager.AppSettings["domain"];

    string organisation = ConfigurationManager.AppSettings["organisation"];

    CrmAuthenticationToken token = new CrmAuthenticationToken();

    token.AuthenticationType = 0;

    token.OrganizationName = organisation;

    metadata =
    new MetadataService();

    metadata.Credentials = new System.Net.NetworkCredential(username, password, domain);

    metadata.UnsafeAuthenticatedConnectionSharing = true;

    metadata.CrmAuthenticationTokenValue = token;

    }

    private static Option CreatePicklistOption(string label, int value)

    {

    Option option =
    new Option();

    option.Label = CreateSingleLabel(label);

    option.Value =
    new CrmNumber();

    option.Value.Value = value;

    return option;

    }

    public static CrmLabel CreateSingleLabel(string label)

    {

    CrmNumber crmNumber =
    new CrmNumber();

    crmNumber.Value = languageCode;

    LocLabel locLabel =
    new LocLabel();

    locLabel.LanguageCode = crmNumber;

    locLabel.Label = label;

    CrmLabel crmLabel =
    new CrmLabel();

    crmLabel.LocLabels = new LocLabel[] { locLabel };

    return crmLabel;

    }

    private static List<Option> CreatePicklistOptions()

    {

    string inputfile = ConfigurationManager.AppSettings["inputfile"];

    List<Option> options = new List<Option>();

    string[] lines = File..ReadAllLines(inputfile);

    int index = 1;

    foreach (string entry in lines)

    {

    string[] tmp = entry.Split(new char[] { ';' });string countryname = UCFirstWord(tmp[0].ToLower());

    options.Add(CreatePicklistOption(countryname, index++));

    }

    return options;

    }

    private static string UCFirstWord(string value)

    {

    string result = string.Empty;

    string[] words = value.Split(new char[] { ' ' });

    foreach (string word in words)

    result += (char.ToUpper(word[0]) + word.Substring(1)) + " ";

    return result.Trim();

    }

    }

    }

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans