How to view customized field in UI of dynamics 365 CRM using Plugin in C#

94 Views Asked by At

I have created one customized field in contact module by using plugin in dynamic 365 crm. But, I am unable to see the field in the UI of contact field. I have tried with the following code. It is giving me "The type or namespace name 'SavedQuery' could not be found"

I have tried with "SavedQuery" to view the customized field in contact module.

My expectation is that I want to publish the customized field and should be visible to the UI by using plugin C#.

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);
        }
    }
}
1

There are 1 best solutions below

2
Henk van Boeijen On
  1. Create a solution.
  2. Add table Contact to the solution. (Best practice is to add only the form you need to edit.)
  3. Drag and drop the field you created on the form.
  4. Save the form.
  5. Publish the customizations.

The saved query you created is actually a view. It will be visible among the other standard views (All Contacts, My Contacts, Active Contacts etc.)