如何使用C#插件查看Dynamics 365 CRM UI中的自定义字段

问题描述 投票:0回答:1

我使用动态 365 crm 中的插件在联系人模块中创建了一个自定义字段。但是,我无法在联系人字段的用户界面中看到该字段。我尝试过以下代码。它给我“找不到类型或命名空间名称‘SavedQuery’

我尝试使用“SavedQuery”查看联系人模块中的自定义字段。

我的期望是我想要发布自定义字段,并且应该通过使用插件 C# 对 UI 可见。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.Net;
using Microsoft.Xrm.Tooling.Connector;
using System.Configuration;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;

namespace ConnectD365Crm
{
    public class Program
    {
        static void Main(string[] args)
        {
            IOrganizationService service = getCRMService();

            CreateField(service);
        }
        public static IOrganizationService getCRMService()
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                string cs = ConfigurationManager  
               .ConnectionStrings["CRMConnectionString"].ConnectionString;
                CrmServiceClient serviceClient = new CrmServiceClient(cs);
                if (serviceClient != null)
                {
                    //return (IOrganizationService)serviceClient.;
                    // Get the IOrganizationService instance from the CrmServiceClient
                    IOrganizationService organizationService = serviceClient
                    .OrganizationWebProxyClient != null
                        ? (IOrganizationService)serviceClient.OrganizationWebProxyClient
                        : (IOrganizationService)serviceClient.OrganizationServiceProxy;
                    return organizationService;

                }
                else
                {
                    throw new Exception(serviceClient.LastCrmError);
                }
            }
            catch (Exception e) { throw e; }
        }

        public static void CreateField(IOrganizationService service)
        {
            StringAttributeMetadata stringAttribute = new StringAttributeMetadata
            {
                // Set base properties
                SchemaName = "new_jobcity",
                DisplayName = new Label("Job City", 1033),
                RequiredLevel=new   AttributeRequiredLevelManagedProperty(None),
                Description = new Label("Your Job City", 1033),
                // Set extended properties
                MaxLength = 100
            };

            CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
            {
                EntityName = "contact",
                Attribute = stringAttribute
            };

            service.Execute(createAttributeRequest);

            string layoutXml = @"<grid name='resultset' object='2' jump='name'
            select='1'preview='1'icon='1'>
            <row name='result' id='contactid'>
               <cell name='name' width='150' /> 
               <cell name='new_jobcity' width='150' />
           </row>
        </grid>";

            string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical'    distinct='false'>
        <entity name='contact'>
           <order attribute='new_jobcity' descending='false' />
           <attribute name='new_jobcity' />
           <attribute name='contactid' /> 
        </entity>
        </fetch>";

            SavedQuery sq = new SavedQuery
            {
                Name = "A New Custom Public View",
                Description = "A Saved Query created in code",
                ReturnedTypeCode = "contact",
                FetchXml = fetchXml,
                LayoutXml = layoutXml,
                QueryType = 0
            };

            service.Create(sq);


            PublishAllXmlRequest publishRequest = new PublishAllXmlRequest();
            service.Execute(publishRequest);
        }
    }
}
dynamics-crm microsoft-dynamics dynamics-crm-2013 dynamics-365
1个回答
0
投票
  1. 创建解决方案。
  2. 将表联系人添加到解决方案中。 (最佳实践是仅添加您需要编辑的表单。)
  3. 拖放您在表单上创建的字段。
  4. 保存表格。
  5. 发布定制。

您创建的保存的查询实际上是一个视图。它将在其他标准视图(所有联系人、我的联系人、活动联系人等)中可见

© www.soinside.com 2019 - 2024. All rights reserved.