Exemplo n.º 1
0
        public static PickListEnums GetOptionPickListValues(string entityLogicalName, string entityDisplayName, string attributeName, IOrganizationService _serviceProxy)
        {
            // set the properties to get back the codeable concept picklist values
            // from the picklist (optionset)
            RetrieveAttributeRequest retrieveEntityRequest = new RetrieveAttributeRequest
            {
                EntityLogicalName     = entityLogicalName,
                LogicalName           = attributeName,
                RetrieveAsIfPublished = true
            };

            // execute the call and retrieve the actual metadata directly
            RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)_serviceProxy.Execute(retrieveEntityRequest);
            var attributeMetadata = (EnumAttributeMetadata)retrieveAttributeResponse.AttributeMetadata;

            // retrieve each option list item
            var optionList = (from o in attributeMetadata.OptionSet.Options
                              select new { Value = o.Value, Text = o.Label.UserLocalizedLabel.Label }).ToList();

            // iterate through each option and write out the value and text
            // this will enable us to map the "Text" value given to us in the
            // codeable concepts file that we generated OR if you generate some new ones
            // you can run this program again to create the mapping file
            // or at least have the copy that was generated for you

            PickListEnums ple = new PickListEnums();

            ple.AttributeLogicalName = attributeName;
            ple.EntityDisplayName    = entityDisplayName;
            ple.EntityLogicalName    = entityLogicalName;

            ple.EnumName = GenerateEnumName(entityDisplayName, attributeName);

            foreach (var option in optionList)
            {
                totalOptions++;
                PickListValues plv = new PickListValues();

                // add to our stirng list of options
                picklistmappingdata.Add(entityDisplayName + "," + entityLogicalName + "," + attributeName + "," + option.Text.Trim() + "," + option.Value.ToString().Trim()); //default to first string of the label

                plv.PickListDisplay = option.Text.Trim();
                plv.PickListValue   = option.Value.ToString().Trim();

                ple.EnumValues.Add(option.Text.Trim(), plv);

                // write out our option count
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.WriteLine("Total Options Found [" + totalOptions.ToString() + "]");

                System.Threading.Thread.Sleep(1);
            }

            return(ple);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Demonstrates sharing records by exercising various access messages including:
        /// Grant, Modify, Revoke, RetrievePrincipalAccess, and
        /// RetrievePrincipalsAndAccess.
        /// </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)
        {
            try
            {
                Console.WriteLine("Looking for picklists to export values for");

                string solutionName = ConfigurationManager.AppSettings["cdm:solution"];

                // we need this to support communicating with Dynamics Online Instances
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                //<snippetSharingRecords1>
                // Connect to the Organization service.
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    _service = (IOrganizationService)_serviceProxy;

                    IEnumerable <EntityMetadata> entities = getSolutionEntities(solutionName, _serviceProxy);

                    foreach (EntityMetadata em in entities)
                    {
                        foreach (AttributeMetadata am in em.Attributes)
                        {
                            if (am.AttributeType == AttributeTypeCode.Picklist)
                            {
                                if ((em.LogicalName.ToLower().StartsWith("msemr_")) || (am.LogicalName.ToLower().StartsWith("msemr_")))
                                {
                                    PickListEnums ple = new PickListEnums();
                                    ple = GetOptionPickListValues(em.LogicalName, em.DisplayName.UserLocalizedLabel.Label.ToString(), am.LogicalName, _serviceProxy);

                                    enums.Add(ple);

                                    newNumerations += ple.GenerateEnum();
                                }
                            }
                        }
                    }


                    string picklistoutputfile = ConfigurationManager.AppSettings["cdm:picklistvaluesoutputfilename"];
                    string enumfile           = ConfigurationManager.AppSettings["cdm:enumsourceoutputfilename"];

                    // if we don't have any don't write anything
                    if (picklistmappingdata.Count > 0)
                    {
                        File.Delete(picklistoutputfile); // Remove this line if you don't want to erase the current version
                        File.WriteAllLines(picklistoutputfile, picklistmappingdata);
                    }
                    else
                    {
                        Console.WriteLine("Could not find any pick list Types");
                    }

                    File.Delete(enumfile); // Remove this line if you don't want to erase the current version
                    File.WriteAllText(enumfile, newNumerations);
                }
            }

            // 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;
            }
        }