protected JObject GetEntityJson(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, Entity entity, CmsEntityMetadata entityMetadata)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            if (entityMetadata == null)
            {
                throw new ArgumentNullException("entityMetadata");
            }

            var json = new JObject
            {
                {
                    "__metadata", new JObject
                    {
                        { "uri", new JValue(VirtualPathUtility.ToAbsolute(CmsEntityRouteHandler.GetAppRelativePath(portalScopeId, entity.ToEntityReference()))) },
                        { "type", new JValue(entity.GetType().FullName) },
                    }
                },
                { "Id", new JValue(entity.Id.ToString()) },
                { "LogicalName", new JValue(entity.LogicalName) },
            };

            foreach (var attributeLogicalName in entityMetadata.Attributes)
            {
                json[attributeLogicalName] = entity.Attributes.Contains(attributeLogicalName)
                                        ? GetValueJson(entity.Attributes[attributeLogicalName])
                                        : null;
            }

            var extensions = new JObject();

            json[ExtensionsKey] = extensions;

            serviceProvider.ExtendEntityJson(context, portal, serviceContext, entity, entityMetadata, extensions);

            foreach (var relationship in entityMetadata.Relationships)
            {
                json[relationship.ToSchemaName(".")] = new JObject
                {
                    {
                        "__deferred", new JObject
                        {
                            { "uri", new JValue(VirtualPathUtility.ToAbsolute(CmsEntityRelationshipRouteHandler.GetAppRelativePath(portalScopeId, entity.ToEntityReference(), relationship))) }
                        }
                    },
                };
            }

            return(json);
        }
        protected virtual void AddEntityRelationshipServiceReference(ICmsEntityEditingMetadataContainer container, IPortalContext portal, EntityReference entity, Relationship relationship, string cssClassFormat = "xrm-entity-{0}-ref")
        {
            var servicePath = CmsEntityRelationshipRouteHandler.GetAppRelativePath(portal.Website.Id, entity, relationship);

            AddServiceReference(container, servicePath, cssClassFormat.FormatWith(relationship.ToSchemaName("_")));
        }
        public void AddEntityMetadata(ICmsEntityEditingMetadataContainer container, string entityLogicalName, string portalName = null, string entityDisplayName = null, JObject initialValues = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            var portal  = PortalCrmConfigurationManager.CreatePortalContext(portalName ?? PortalName);
            var website = portal.Website.ToEntityReference();

            container.AddLabel(entityDisplayName);
            container.AddAttribute("data-xrm-base", VirtualPathUtility.ToAbsolute("~/xrm-adx/"));
            container.AddAttribute("data-logicalname", entityLogicalName);

            AddFileBrowserServiceReference(container, portal, FileBrowserDirectoryEntityNames.Contains(entityLogicalName));
            AddEntityTemplateServiceReference(container, portal, website, entityLogicalName);
            AddEntityTemplateRenderServiceReference(container, portal, website, entityLogicalName);

            if (initialValues != null)
            {
                container.AddAttribute("data-create-initial", initialValues.ToString(Formatting.None));
            }

            if (entityLogicalName == "adx_contentsnippet")
            {
                container.AddAttribute("data-create-url", VirtualPathUtility.ToAbsolute(CmsEntityRelationshipRouteHandler.GetAppRelativePath(website.Id, website, new Relationship("adx_website_contentsnippet"))));
                container.AddAttribute("data-create-attribute", "adx_value");
                container.AddAttribute("data-editable-uritemplate", VirtualPathUtility.ToAbsolute(CmsEntityAttributeRouteHandler.GetAppRelativePathTemplate(website.Id, entityLogicalName, "Id", "adx_value")));
            }
        }