如何更新Dynamics CRM状态码/状态码选项集的排序顺序?

时间:2018-09-25 05:25:31

标签: dynamics-crm microsoft-dynamics

Dynamics CRM中的UI编辑器不允许系统管理员直接在“活动实体”上编辑状态/状态码选择列表。如何以编程方式编辑订单?

在另一个问题中,提供了有关如何获取有关状态码和状态码的元数据的信息: Dynamics Crm: Get metadata for statuscode/statecode mapping

Microsoft提供了一个用于对选择列表进行排序的类: https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg327607(v%3dcrm.8)

我已成功编码,可以检索标准选择列表和状态码选择列表(状态码和状态码)的元数据。我可以以编程方式更新标准选择列表上的排序顺序,但是尝试在statuscode选项集上进行更新时,不会引发任何错误,也不会将更改发布到系统中。

如何更新活动状态码字段上的排序顺序?

下面的示例代码,同时完成了两个测试:


namespace ConsoleApps
{
    class EditAptStatusCodeOptionSetOrder
    {
        static void Main()
        {
            //Connect to Database
            string CRMConnectionString = @"AuthType = Office365; URL = https://URLOFSERVER; Username=USERNAME; Password=PASSWORD;";
            Console.WriteLine("Connecting to Auric Solar Sandbox Environment Using Ryan Perry's Credentials. ");
            CrmServiceClient crmServiceClient = new CrmServiceClient(CRMConnectionString);
            IOrganizationService crmService = crmServiceClient.OrganizationServiceProxy;

            //Test Retrieving Stage (Statuscode) from Test Entity. 
            //Get the Attribute
            RetrieveAttributeRequest retrieveStatusCodeAttributeRequest = new RetrieveAttributeRequest
            {
                EntityLogicalName = "new_testentity",
                LogicalName = "statuscode",
                RetrieveAsIfPublished = true
            };

            RetrieveAttributeResponse retrieveStatusCodeAttributeResponse =
                (RetrieveAttributeResponse)crmService.Execute(retrieveStatusCodeAttributeRequest);
            Console.WriteLine("Retreived Attribute:" +
                retrieveStatusCodeAttributeResponse.AttributeMetadata.LogicalName);
            Console.WriteLine("Retreived Attribute Description:" +
                retrieveStatusCodeAttributeResponse.AttributeMetadata.Description);

            StatusAttributeMetadata statusCodeMetaData =
                (StatusAttributeMetadata)retrieveStatusCodeAttributeResponse.AttributeMetadata;
            Console.WriteLine("Metadata Description:" +
                statusCodeMetaData.Description);
            Console.WriteLine("Metadata IsValidForUpdate:" +
                statusCodeMetaData.IsValidForUpdate.ToString());
            Console.WriteLine("OptionSet Type:" +
                statusCodeMetaData.OptionSet.Name.ToString());
            Console.WriteLine("OptionSet Name:" +
                statusCodeMetaData.OptionSet.OptionSetType.ToString());
            Console.WriteLine("Retrieved Options:");
            foreach (OptionMetadata optionMeta in statusCodeMetaData.OptionSet.Options)
            {
                Console.WriteLine(((StatusOptionMetadata)optionMeta).State.Value + " : " + optionMeta.Value + " : " + optionMeta.Label.UserLocalizedLabel.Label);

            }


            //Save Options in an array to reorder.
            OptionMetadata[] optionList = statusCodeMetaData.OptionSet.Options.ToArray();
            Console.WriteLine("Option Array:");
            foreach (OptionMetadata optionMeta in optionList)
            {
                Console.WriteLine(((StatusOptionMetadata)optionMeta).State.Value + " : " + optionMeta.Value + " : " + optionMeta.Label.UserLocalizedLabel.Label);
            }

            var sortedOptionList = optionList.OrderBy(x => x.Label.LocalizedLabels[0].Label).ToList();

            Console.WriteLine("Sorted Option Array:");
            foreach (OptionMetadata optionMeta in sortedOptionList)
            {
                Console.WriteLine(((StatusOptionMetadata)optionMeta).State.Value + " : " + optionMeta.Value + " : " + optionMeta.Label.UserLocalizedLabel.Label);
            }


            //Create the request.
            OrderOptionRequest orderStatusCodeOptionRequest = new OrderOptionRequest
            {
                AttributeLogicalName = "statuscode",
                EntityLogicalName = "new_testentity",
                Values = sortedOptionList.Select(X => X.Value.Value).ToArray()
            };
            Console.WriteLine("orderStatusCodeOptionRequest Created.");

            //Execute Request. //////THIS DOESN'T THROW ERRORS, BUT DOESN'T UPDATE SYSTEM. 
            OrderOptionResponse orderStatusCodeResponse = (OrderOptionResponse)crmService.Execute(orderStatusCodeOptionRequest);
            Console.WriteLine("Request Executed");







            ////////////////////////////////////////PICKLIST TEST//////////////////////////////////

            Console.WriteLine("\n\nTestingPickList");
            RetrieveAttributeRequest retrieveTestPicklistAttributeRequest = new RetrieveAttributeRequest
            {
                EntityLogicalName = "new_testentity",
                LogicalName = "new_testpicklist",
                RetrieveAsIfPublished = true
            };
            RetrieveAttributeResponse retrieveTestPicklistAttributeResponse =
                (RetrieveAttributeResponse)crmService.Execute(retrieveTestPicklistAttributeRequest);
            PicklistAttributeMetadata pickListMetaData =
                (PicklistAttributeMetadata)retrieveTestPicklistAttributeResponse.AttributeMetadata;
            Console.WriteLine("Retrieved Picklist Options:");
            foreach (OptionMetadata optionMeta in pickListMetaData.OptionSet.Options)
            {
                Console.WriteLine(optionMeta.Value + " : " + optionMeta.Label.UserLocalizedLabel.Label);

            }


            //Save Options in an array to reorder.
            OptionMetadata[] picklistOptionList = pickListMetaData.OptionSet.Options.ToArray();
            Console.WriteLine("Picklist Option Array:");
            foreach (OptionMetadata optionMeta in picklistOptionList)
            {
                Console.WriteLine(optionMeta.Value + " : " + optionMeta.Label.UserLocalizedLabel.Label);
            }

            var sortedPicklistOptionList = picklistOptionList.OrderBy(x => x.Label.LocalizedLabels[0].Label).ToList();

            Console.WriteLine("Picklist Sorted Option Array:");
            foreach (OptionMetadata optionMeta in sortedPicklistOptionList)
            {
                Console.WriteLine(optionMeta.Value + " : " + optionMeta.Label.UserLocalizedLabel.Label);
            }

            //Create the request.
            OrderOptionRequest orderPicklistOptionRequest = new OrderOptionRequest
            {
                AttributeLogicalName = "new_testpicklist",
                EntityLogicalName = "new_testentity",
                Values = sortedPicklistOptionList.Select(X => X.Value.Value).ToArray()
            };
            Console.WriteLine("orderPicklistOptionRequest Created.");

            //Execute Request.
            OrderOptionResponse orderPicklistResponse = (OrderOptionResponse)crmService.Execute(orderPicklistOptionRequest);
            Console.WriteLine("Order Picklist Request Executed");


            //Publish All.
            Console.WriteLine("Publishing. Please Wait...");
            PublishAllXmlRequest publishRequest = new PublishAllXmlRequest();
            crmService.Execute(publishRequest);
            Console.WriteLine("Done.");

            Console.ReadLine();

        }
    }
}

0 个答案:

没有答案