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);
        }
        protected override void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, Entity entity, CmsEntityMetadata entityMetadata, ICrmEntitySecurityProvider security)
        {
            var attributeLogicalName = string.IsNullOrWhiteSpace(AttributeLogicalName) ? context.Request.Params["attributeLogicalName"] : AttributeLogicalName;

            if (string.IsNullOrWhiteSpace(attributeLogicalName))
            {
                throw new CmsEntityServiceException(HttpStatusCode.BadRequest, "Unable to determine entity attribute logical name from request.");
            }

            if (entityMetadata.HasAttribute(AttributeLogicalName))
            {
                if (!IsRequestMethod(context.Request, "GET"))
                {
                    throw new CmsEntityServiceException(HttpStatusCode.MethodNotAllowed, "Request method {0} not allowed for this resource.".FormatWith(context.Request.HttpMethod));
                }

                var value = entity.Attributes.Contains(attributeLogicalName) ? entity.Attributes[attributeLogicalName] : null;

                var json = new JObject
                {
                    {
                        "d", new JObject
                        {
                            { attributeLogicalName, GetValueJson(value) }
                        }
                    },
                };

                WriteResponse(context.Response, json);

                return;
            }

            throw new CmsEntityServiceException(HttpStatusCode.NotFound, "Entity attribute not found.");
        }
Пример #3
0
        protected override void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, EntityReference entityReference)
        {
            var entityMetadata = new CmsEntityMetadata(serviceContext, entityReference.LogicalName);

            var query = serviceContext.CreateQuery(entityReference.LogicalName);

            // If the target entity is scoped to a website, filter the query by the current website.
            if (entityMetadata.HasAttribute("adx_websiteid"))
            {
                query = query.Where(e => e.GetAttributeValue <EntityReference>("adx_websiteid") == portal.Website.ToEntityReference());
            }

            var entity = query.FirstOrDefault(e => e.GetAttributeValue <Guid>(entityMetadata.PrimaryIdAttribute) == entityReference.Id);

            if (entity == null)
            {
                throw new CmsEntityServiceException(HttpStatusCode.NotFound, "Entity not found.");
            }

            var security = PortalCrmConfigurationManager.CreateCrmEntitySecurityProvider(PortalName);

            if (!security.TryAssert(serviceContext, entity, CrmEntityRight.Read))
            {
                throw new CmsEntityServiceException(HttpStatusCode.Forbidden, "Entity access denied.");
            }

            var url = serviceContext.GetUrl(entity);

            if (url == null)
            {
                throw new CmsEntityServiceException(HttpStatusCode.NotFound, "URL for entity not found.");
            }

            WriteResponse(context.Response, new JObject
            {
                { "d", new JObject {
                      { "Url", new JValue(url) }
                  } }
            });
        }