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 override void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, Entity entity, CmsEntityMetadata entityMetadata, ICrmEntitySecurityProvider security)
        {
            if (!IsRequestMethod(context.Request, "GET"))
            {
                throw new CmsEntityServiceException(HttpStatusCode.MethodNotAllowed, "Request method {0} not allowed for this resource.".FormatWith(context.Request.HttpMethod));
            }

            var children = GetChildren(serviceContext, entity, entityMetadata)
                           .Where(child => security.TryAssert(serviceContext, child, CrmEntityRight.Read))
                           .ToList();

            children.Sort(new EntitySiteMapDisplayOrderComparer());

            var entityMetadataCache = new Dictionary <string, CmsEntityMetadata>();

            var childInfos = children.Select(e =>
            {
                var info = new ExtendedSiteMapChildInfo
                {
                    Title             = GetChildTitle(e),
                    EntityUri         = VirtualPathUtility.ToAbsolute(CmsEntityRouteHandler.GetAppRelativePath(portalScopeId, e.ToEntityReference())),
                    HasPermission     = security.TryAssert(serviceContext, e, CrmEntityRight.Change),
                    Id                = e.Id,
                    LogicalName       = e.LogicalName,
                    HiddenFromSiteMap = e.Attributes.Contains("adx_hiddenfromsitemap") && e.GetAttributeValue <bool?>("adx_hiddenfromsitemap").GetValueOrDefault(),
                    Url               = e.Contains("adx_partialurl") ? e.GetAttributeValue <string>("adx_partialurl") : null
                };

                CmsEntityMetadata childEntityMetadata;

                if (TryGetEntityMetadata(serviceContext, e.LogicalName, entityMetadataCache, out childEntityMetadata))
                {
                    if (childEntityMetadata.HasAttribute("adx_displayorder"))
                    {
                        info.DisplayOrder             = e.Attributes.Contains("adx_displayorder") ? e.GetAttributeValue <int?>("adx_displayorder") : null;
                        info.DisplayOrderPropertyName = "adx_displayorder";
                    }
                }

                return(info);
            }).ToArray();

            var childJson = SerializeChildInfos(childInfos);

            WriteResponse(context.Response, new JObject
            {
                { "d", new JRaw(childJson) }
            });
        }
        protected JObject GetEntityReferenceJson(Entity entity, CmsEntityMetadata entityMetadata, Guid portalScopeId)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

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

            var primaryNameAttribute = entityMetadata.PrimaryNameAttribute;
            var name = entity.Attributes.Contains(primaryNameAttribute) ? entity.GetAttributeValue <string>(primaryNameAttribute) : null;

            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) },
                { "Name", new JValue(name) },
                { primaryNameAttribute, new JValue(name) },
                { entityMetadata.PrimaryIdAttribute, new JValue(entity.Id.ToString()) }
            };

            if (entityMetadata.HasAttribute("adx_description"))
            {
                json["adx_description"] = new JValue(entity.GetAttributeValue <string>("adx_description"));
            }

            if (entityMetadata.HasAttribute("adx_isdefault"))
            {
                json["adx_isdefault"] = new JValue(entity.Attributes.Contains("adx_isdefault") && entity.GetAttributeValue <bool?>("adx_isdefault").GetValueOrDefault(false));
            }

            if (entityMetadata.HasAttribute("adx_isvisible"))
            {
                json["adx_isvisible"] = new JValue(entity.Attributes.Contains("adx_isvisible") && entity.GetAttributeValue <bool?>("adx_isvisible").GetValueOrDefault(false));
            }

            return(json);
        }
        private static JObject GetWebPageReferenceJson(IDirectory directory, Guid portalScopeId)
        {
            var entity = directory.Entity;
            var name   = entity.GetAttributeValue <string>("adx_name");

            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) },
                { "Name", new JValue(name) },
                { "adx_name", new JValue(name) },
                { "adx_webpageid", new JValue(entity.Id.ToString()) }
            };

            return(json);
        }
        protected virtual void AddEntityServiceReferenceTemplate(ICmsEntityEditingMetadataContainer container, IPortalContext portal, string entityLogicalName, string idTemplateVariableName = "Id")
        {
            var servicePath = CmsEntityRouteHandler.GetAppRelativePathTemplate(portal.Website.Id, entityLogicalName, idTemplateVariableName);

            AddServiceReference(container, servicePath, "xrm-uri-template xrm-entity-{0}-ref".FormatWith(entityLogicalName));
        }
        protected virtual void AddEntityServiceReference(ICmsEntityEditingMetadataContainer container, IPortalContext portal, EntityReference entity, string title = null)
        {
            var servicePath = CmsEntityRouteHandler.GetAppRelativePath(portal.Website.Id, entity);

            AddServiceReference(container, servicePath, "xrm-entity-ref", title);
        }