使用程序导出Sdk Message Processing Step

我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面的微软最有价值专家(Microsoft MVP),欢迎关注我的微信公众号 MSFTDynamics365erLuoYong ,回复388或者20200116可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!

今天发现Sdk Message Processing Step的高级查找界面没有导出按钮,如下图。

那我要导出来就只有写代码了,我这里用组织服务来导出,做个小笔记:

        private static void GetAllSdkSteps(OrganizationServiceProxy orgSvc)
        {
            string outPutFileName = ConfigurationManager.AppSettings["outPutFileName"];
            if (!File.Exists(outPutFileName))
            {
                string clientHeader = $"eventhandler,primaryobjecttypecode,secondaryobjecttypecode,sdkmessageid,stage,mode,statecode,execution order,name,sdkmessageprocessingstepid,description,impersonatinguserid,supporteddeployment,sdkmessagefilterid,filteringattributes,asyncautodelete,ismanaged,ishidden,createdon,modifiedon,plugintypeid{Environment.NewLine}";
                File.WriteAllText(outPutFileName, clientHeader);
            }
            StringBuilder sb = new StringBuilder();
            var fetchXml = @"<fetch version='1.0' mapping='logical' distinct='false' no-lock='true'>
  <entity name='sdkmessageprocessingstep'>
    <attribute name='name' />
    <attribute name='sdkmessageprocessingstepid' />
    <attribute name='description' />
    <attribute name='eventhandler' />
    <attribute name='impersonatinguserid' />
    <attribute name='supporteddeployment' />
    <attribute name='statecode' />
    <attribute name='rank' />
    <attribute name='mode' />
    <attribute name='sdkmessagefilterid' />
    <attribute name='sdkmessageid' />
    <attribute name='filteringattributes' />
    <attribute name='configuration' />
    <attribute name='asyncautodelete' />
    <attribute name='ismanaged' />
    <attribute name='ishidden' />
    <attribute name='stage' />
    <attribute name='createdon' />
    <attribute name='modifiedon' />
    <attribute name='plugintypeid' />
    <attribute name='supporteddeployment' />
    <order attribute='sdkmessageprocessingstepid' descending='false' />
    <filter type='and'>
      <condition attribute='iscustomizable' operator='eq' value='true' />
      <condition attribute='name' operator='ne' value='ObjectModel Implementation' />
    </filter>
    <link-entity name='sdkmessagefilter' from='sdkmessagefilterid' to='sdkmessagefilterid' visible='false' link-type='outer' alias='sdkfilter'>
      <attribute name='secondaryobjecttypecode' />
      <attribute name='primaryobjecttypecode' />
    </link-entity>
  </entity>
</fetch>";
            int pageNumber = 1;
            int fetchCount = 500;
            string pagingCookie = null;
            while (true)
            {
                string xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
                RetrieveMultipleRequest pageRequest1 = new RetrieveMultipleRequest
                {
                    Query = new FetchExpression(xml)
                };
                EntityCollection returnCollection = ((RetrieveMultipleResponse)orgSvc.Execute(pageRequest1)).EntityCollection;
                foreach (var entity in returnCollection.Entities)
                {
                    sb = new StringBuilder();
                    if (entity.Contains("eventhandler"))
                    {
                        sb.Append(entity.GetAttributeValue<EntityReference>("eventhandler").Name.Replace(',', ';'));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("sdkfilter.primaryobjecttypecode"))
                    {
                        sb.Append(entity.GetAttributeValue<AliasedValue>("sdkfilter.primaryobjecttypecode").Value.ToString().Replace(',', ';'));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append("All,");
                    }
                    if (entity.Contains("sdkfilter.secondaryobjecttypecode"))
                    {
                        sb.Append(entity.GetAttributeValue<AliasedValue>("sdkfilter.secondaryobjecttypecode").Value.ToString().Replace(',', ';'));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("sdkmessageid"))
                    {
                        sb.Append(entity.GetAttributeValue<EntityReference>("sdkmessageid").Name.Replace(',', ';'));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("stage"))
                    {
                        switch (entity.GetAttributeValue<OptionSetValue>("stage").Value)
                        {
                            case 10:
                                sb.Append("Pre-validation");
                                break;
                            case 20:
                                sb.Append("Pre-operation");
                                break;
                            case 30:
                                sb.Append("Main Operation");
                                break;
                            case 40:
                                sb.Append("Post-operation");
                                break;
                            default:
                                sb.Append("Others");
                                break;
                        }
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("mode"))
                    {
                        switch (entity.GetAttributeValue<OptionSetValue>("mode").Value)
                        {
                            case 0:
                                sb.Append("Synchronous");
                                break;
                            case 1:
                                sb.Append("Asynchronous");
                                break;
                            default:
                                sb.Append("Others");
                                break;
                        }
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("statecode"))
                    {
                        sb.Append(entity.GetAttributeValue<OptionSetValue>("statecode").Value == 0 ? "Enabled" : "Disabled");
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("rank"))
                    {
                        sb.Append(entity.GetAttributeValue<int>("rank"));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("name"))
                    {
                        sb.Append(entity.GetAttributeValue<string>("name").Replace(',', ';'));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    sb.Append(entity.GetAttributeValue<Guid>("sdkmessageprocessingstepid"));
                    sb.Append(",");
                    if (entity.Contains("description"))
                    {
                        sb.Append(entity.GetAttributeValue<string>("description").Replace(',',';'));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("impersonatinguserid"))
                    {
                        sb.Append(entity.GetAttributeValue<EntityReference>("impersonatinguserid").Name);
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("supporteddeployment"))
                    {
                        switch (entity.GetAttributeValue<OptionSetValue>("supporteddeployment").Value)
                        {
                            case 0:
                                sb.Append("Server Only");
                                break;
                            case 1:
                                sb.Append("Microsoft Dynamics 365 Client for Outlook Only");
                                break;
                            case 2:
                                sb.Append("Both");
                                break;
                            default:
                                sb.Append("Unknown");
                                break;
                        }
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("sdkmessagefilterid"))
                    {
                        sb.Append(entity.GetAttributeValue<EntityReference>("sdkmessagefilterid").Name);
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("filteringattributes"))
                    {
                        //把都好替换成分号,方便CSV
                        sb.Append(entity.GetAttributeValue<string>("filteringattributes").Replace(',',';'));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    //if (entity.Contains("configuration"))
                    //{
                    //    sb.Append(entity.GetAttributeValue<string>("configuration"));
                    //    sb.Append(",");
                    //}
                    //else
                    //{
                    //    sb.Append(",");
                    //}
                    if (entity.Contains("asyncautodelete"))
                    {
                        sb.Append(entity.GetAttributeValue<bool>("asyncautodelete").ToString());
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("ismanaged"))
                    {
                        sb.Append(entity.GetAttributeValue<bool>("ismanaged").ToString());
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("ishidden"))
                    {
                        sb.Append(entity.GetAttributeValue<BooleanManagedProperty>("ishidden").Value.ToString());
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("createdon"))
                    {
                        sb.Append(entity.GetAttributeValue<DateTime>("createdon").ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("modifiedon"))
                    {
                        sb.Append(entity.GetAttributeValue<DateTime>("modifiedon").ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss.fff"));
                        sb.Append(",");
                    }
                    else
                    {
                        sb.Append(",");
                    }
                    if (entity.Contains("plugintypeid"))
                    {
                        sb.Append(entity.GetAttributeValue<EntityReference>("plugintypeid").Name.Replace(',', ';'));
                    }
                    File.AppendAllText(outPutFileName, $"{sb.ToString()}{Environment.NewLine}");
                }
                if (returnCollection.MoreRecords)
                {
                    pageNumber++;
                    pagingCookie = returnCollection.PagingCookie;
                }
                else
                {
                    break;
                }
            }
        }

        public static string CreateXml(string xml, string cookie, int page, int count)
        {
            StringReader stringReader = new StringReader(xml);
            XmlTextReader reader = new XmlTextReader(stringReader);
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            return CreateXml(doc, cookie, page, count);
        }


        public static string CreateXml(XmlDocument doc, string cookie, int page, int count)
        {
            XmlAttributeCollection attrs = doc.DocumentElement.Attributes;
            if (cookie != null)
            {
                XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie");
                pagingAttr.Value = cookie;
                attrs.Append(pagingAttr);
            }
            XmlAttribute pageAttr = doc.CreateAttribute("page");
            pageAttr.Value = System.Convert.ToString(page);
            attrs.Append(pageAttr);
            XmlAttribute countAttr = doc.CreateAttribute("count");
            countAttr.Value = System.Convert.ToString(count);
            attrs.Append(countAttr);
            StringBuilder sb = new StringBuilder(1024);
            StringWriter stringWriter = new StringWriter(sb);
            XmlTextWriter writer = new XmlTextWriter(stringWriter);
            doc.WriteTo(writer);
            writer.Close();
            return sb.ToString();
        }

连接Dynamics 365 Customer Engagement Online可以使用类似如下代码:

CrmServiceClient crmSvc = new CrmServiceClient(@"AuthType=Office365;Url=https://crm558602.api.crm.dynamics.com;UserName=admin@CRM558602.onmicrosoft.com;Password=lupssie0283");

使用FetchXml进行分页查询记录请参考官方文档: Sample: Use FetchXML with a paging cookie .

原文地址:https://www.cnblogs.com/luoyong0201/p/Dynamics_365_Export_Sdk_Message_Processing_Steps.html