protected virtual void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, string entityLogicalName) { if (context == null) { throw new ArgumentNullException("context"); } if (serviceProvider == null) { throw new ArgumentNullException("serviceProvider"); } if (portal == null) { throw new ArgumentNullException("portal"); } if (serviceContext == null) { throw new ArgumentNullException("serviceContext"); } var entityMetadata = new CmsEntityMetadata(serviceContext, entityLogicalName); var entities = serviceProvider.ExecuteEntitySetQuery(context, portal, serviceContext, entityLogicalName, entityMetadata, context.Request.Params["filter"]); WriteResponse(context.Response, new JObject { { "d", new JArray(entities.Select(e => GetEntityReferenceJson(e, entityMetadata, portalScopeId))) } }); }
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."); }
protected virtual void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, Entity entity, CmsEntityMetadata entityMetadata, ICrmEntitySecurityProvider security) { if (IsRequestMethod(context.Request, "GET")) { WriteResponse(context.Response, new JObject { { "d", GetEntityJson(context, serviceProvider, portalScopeId, portal, serviceContext, entity, entityMetadata) } }); return; } if (IsRequestMethod(context.Request, "POST")) { var preImage = entity.Clone(false); var extensions = UpdateEntityFromJsonRequestBody(context.Request, serviceContext, entity, entityMetadata); serviceProvider.InterceptChange(context, portal, serviceContext, entity, entityMetadata, CmsEntityOperation.Update, preImage); serviceContext.UpdateObject(entity); serviceProvider.InterceptExtensionChange(context, portal, serviceContext, entity, entityMetadata, extensions, CmsEntityOperation.Update); serviceContext.SaveChanges(); WriteNoContentResponse(context.Response); return; } throw new CmsEntityServiceException(HttpStatusCode.MethodNotAllowed, "Request method {0} not allowed for this resource.".FormatWith(context.Request.HttpMethod)); }
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 override void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, Entity entity, CmsEntityMetadata entityMetadata, ICrmEntitySecurityProvider security) { if (!IsRequestMethod(context.Request, "POST")) { throw new CmsEntityServiceException(HttpStatusCode.MethodNotAllowed, "Request method {0} not allowed for this resource.".FormatWith(context.Request.HttpMethod)); } var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(requestContext: context.Request.RequestContext, portalName: PortalName); var annotationDataAdapter = new AnnotationDataAdapter(dataAdapterDependencies); var website = context.GetWebsite(); var location = website.Settings.Get <string>("WebFiles/StorageLocation"); StorageLocation storageLocation; if (!Enum.TryParse(location, true, out storageLocation)) { storageLocation = StorageLocation.CrmDocument; } var maxFileSizeErrorMessage = website.Settings.Get <string>("WebFiles/MaxFileSizeErrorMessage"); var annotationSettings = new AnnotationSettings(dataAdapterDependencies.GetServiceContext(), storageLocation: storageLocation, maxFileSizeErrorMessage: maxFileSizeErrorMessage); var files = context.Request.Files; var postedFiles = new List <HttpPostedFile>(); for (var i = 0; i < files.Count; i++) { postedFiles.Add(files[i]); } foreach (var file in postedFiles) { annotationDataAdapter.CreateAnnotation(new Annotation { Regarding = entity.ToEntityReference(), FileAttachment = AnnotationDataAdapter.CreateFileAttachment(new HttpPostedFileWrapper(file), annotationSettings.StorageLocation) }, annotationSettings); } context.Response.ContentType = "text/plain"; context.Response.Write("OK"); }
protected virtual void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, EntityReference entityReference) { if (context == null) { throw new ArgumentNullException("context"); } if (serviceProvider == null) { throw new ArgumentNullException("serviceProvider"); } if (portal == null) { throw new ArgumentNullException("portal"); } if (serviceContext == null) { throw new ArgumentNullException("serviceContext"); } if (entityReference == null) { throw new ArgumentNullException("entityReference"); } var entityMetadata = new CmsEntityMetadata(serviceContext, entityReference.LogicalName); var entity = serviceProvider.ExecuteEntityQuery(context, portal, serviceContext, entityReference, entityMetadata); if (entity == null) { throw new CmsEntityServiceException(HttpStatusCode.NotFound, "Entity not found."); } var security = PortalCrmConfigurationManager.CreateCrmEntitySecurityProvider(PortalName); AssertRequestEntitySecurity(portal, serviceContext, entity, security); ProcessRequest(context, serviceProvider, portalScopeId, portal, serviceContext, entity, entityMetadata, security); }
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) } } } }); }
protected override void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, string entityLogicalName) { if (context == null) { throw new ArgumentNullException("context"); } if (serviceProvider == null) { throw new ArgumentNullException("serviceProvider"); } if (portal == null) { throw new ArgumentNullException("portal"); } if (serviceContext == null) { throw new ArgumentNullException("serviceContext"); } if (string.IsNullOrWhiteSpace(entityLogicalName)) { throw new ArgumentException("Value can't be null or whitespace.", "entityLogicalName"); } Guid parsedId; var id = Id ?? (Guid.TryParse(context.Request.Params["id"], out parsedId) ? new Guid?(parsedId) : null); if (id == null) { throw new CmsEntityServiceException(HttpStatusCode.BadRequest, "Unable to determine entity ID from request."); } ProcessRequest(context, serviceProvider, portalScopeId, portal, serviceContext, new EntityReference(entityLogicalName, id.Value)); }
protected override void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, Entity entity, CmsEntityMetadata entityMetadata, ICrmEntitySecurityProvider security) { var relationshipSchemaName = string.IsNullOrWhiteSpace(RelationshipSchemaName) ? context.Request.Params["relationshipSchemaName"] : RelationshipSchemaName; if (string.IsNullOrWhiteSpace(relationshipSchemaName)) { throw new CmsEntityServiceException(HttpStatusCode.BadRequest, "Unable to determine entity relationship schema name from request."); } var match = _relationshipSchemaNameRegex.Match(relationshipSchemaName); if (!match.Success) { throw new CmsEntityServiceException(HttpStatusCode.BadRequest, "Unable to determine entity relationship schema name from request."); } var schemaName = match.Groups["schemaName"].Value; if (string.IsNullOrWhiteSpace(schemaName)) { throw new CmsEntityServiceException(HttpStatusCode.BadRequest, "Unable to determine entity relationship schema name from request."); } var entityRole = match.Groups["entityRole"].Value; EntityRole parsedRole; var relationship = new Relationship(schemaName) { PrimaryEntityRole = Enum.TryParse(entityRole, true, out parsedRole) ? new EntityRole?(parsedRole) : null }; CmsEntityRelationshipInfo relationshipInfo; if (!entityMetadata.TryGetRelationshipInfo(relationship, out relationshipInfo)) { throw new CmsEntityServiceException(HttpStatusCode.NotFound, "Entity relationship not found."); } // If the current request entity is the current website, do security handling here, since we skipped it earlier. if (entity.ToEntityReference().Equals(portal.Website.ToEntityReference())) { AssertRequestEntitySecurity(portal, serviceContext, entity, security, CreateWebsiteAccessPermissionProvider(portal), relationshipInfo); } if (IsRequestMethod(context.Request, "GET")) { if (relationshipInfo.IsCollection) { var readableRelatedEntities = entity.GetRelatedEntities(serviceContext, relationship) .Where(e => security.TryAssert(serviceContext, e, CrmEntityRight.Read)); var entityMetadataLookup = new Dictionary <string, CmsEntityMetadata>(); var entityJsonObjects = readableRelatedEntities.Select(e => { CmsEntityMetadata relatedEntityMetadata; if (!entityMetadataLookup.TryGetValue(e.LogicalName, out relatedEntityMetadata)) { relatedEntityMetadata = new CmsEntityMetadata(serviceContext, e.LogicalName); entityMetadataLookup[e.LogicalName] = relatedEntityMetadata; } return(GetEntityJson(context, serviceProvider, portalScopeId, portal, serviceContext, e, relatedEntityMetadata)); }); WriteResponse(context.Response, new JObject { { "d", new JArray(entityJsonObjects) } }); } else { var relatedEntity = entity.GetRelatedEntity(serviceContext, relationship); if (relatedEntity == null) { throw new CmsEntityServiceException(HttpStatusCode.NotFound, "Related entity not found."); } if (!security.TryAssert(serviceContext, relatedEntity, CrmEntityRight.Read)) { throw new CmsEntityServiceException(HttpStatusCode.Forbidden, "Related entity access denied."); } WriteResponse(context.Response, new JObject { { "d", GetEntityJson(context, serviceProvider, portalScopeId, portal, serviceContext, relatedEntity, new CmsEntityMetadata(serviceContext, relatedEntity.LogicalName)) } }); } return; } if (IsRequestMethod(context.Request, "POST")) { if (relationshipInfo.IsCollection) { OneToManyRelationshipMetadata relationshipMetadata; if (!entityMetadata.TryGetOneToManyRelationshipMetadata(relationship, out relationshipMetadata)) { throw new CmsEntityServiceException(HttpStatusCode.BadRequest, "Unable to retrieve the one-to-many relationship metadata for relationship {0} on entity type".FormatWith(relationship.ToSchemaName("."), entity.LogicalName)); } var relatedEntity = CreateEntityOfType(serviceContext, relationshipMetadata.ReferencingEntity); var relatedEntityMetadata = new CmsEntityMetadata(serviceContext, relatedEntity.LogicalName); var extensions = UpdateEntityFromJsonRequestBody(context.Request, serviceContext, relatedEntity, relatedEntityMetadata); var preImage = relatedEntity.Clone(false); // Ensure the reference to the target entity is set. relatedEntity.SetAttributeValue(relationshipMetadata.ReferencingAttribute, new EntityReference(entity.LogicalName, entity.GetAttributeValue <Guid>(relationshipMetadata.ReferencedAttribute))); serviceProvider.InterceptChange(context, portal, serviceContext, relatedEntity, relatedEntityMetadata, CmsEntityOperation.Create, preImage); serviceContext.AddObject(relatedEntity); serviceProvider.InterceptExtensionChange(context, portal, serviceContext, relatedEntity, relatedEntityMetadata, extensions, CmsEntityOperation.Create); serviceContext.SaveChanges(); var refetchedEntity = serviceContext.CreateQuery(relatedEntity.LogicalName) .FirstOrDefault(e => e.GetAttributeValue <Guid>(relatedEntityMetadata.PrimaryIdAttribute) == relatedEntity.Id); if (refetchedEntity == null) { throw new CmsEntityServiceException(HttpStatusCode.InternalServerError, "Unable to retrieve the created entity."); } WriteResponse(context.Response, new JObject { { "d", GetEntityJson(context, serviceProvider, portalScopeId, portal, serviceContext, refetchedEntity, relatedEntityMetadata) } }, HttpStatusCode.Created); } else { OneToManyRelationshipMetadata relationshipMetadata; if (!entityMetadata.TryGetManyToOneRelationshipMetadata(relationship, out relationshipMetadata)) { throw new CmsEntityServiceException(HttpStatusCode.BadRequest, "Unable to retrieve the many-to-one relationship metadata for relationship {0} on entity type".FormatWith(relationship.ToSchemaName("."), entity.LogicalName)); } var relatedEntity = CreateEntityOfType(serviceContext, relationshipMetadata.ReferencedEntity); var relatedEntityMetadata = new CmsEntityMetadata(serviceContext, relatedEntity.LogicalName); var extensions = UpdateEntityFromJsonRequestBody(context.Request, serviceContext, relatedEntity, relatedEntityMetadata); serviceProvider.InterceptChange(context, portal, serviceContext, relatedEntity, relatedEntityMetadata, CmsEntityOperation.Create); serviceContext.AddObject(relatedEntity); serviceContext.AddLink(relatedEntity, relationship, entity); serviceProvider.InterceptExtensionChange(context, portal, serviceContext, relatedEntity, relatedEntityMetadata, extensions, CmsEntityOperation.Create); serviceContext.SaveChanges(); var refetchedEntity = serviceContext.CreateQuery(relatedEntity.LogicalName) .FirstOrDefault(e => e.GetAttributeValue <Guid>(relatedEntityMetadata.PrimaryIdAttribute) == relatedEntity.Id); if (refetchedEntity == null) { throw new CmsEntityServiceException(HttpStatusCode.InternalServerError, "Unable to retrieve the created entity."); } WriteResponse(context.Response, new JObject { { "d", GetEntityJson(context, serviceProvider, portalScopeId, portal, serviceContext, refetchedEntity, relatedEntityMetadata) } }, HttpStatusCode.Created); } return; } throw new CmsEntityServiceException(HttpStatusCode.MethodNotAllowed, "Request method {0} not allowed for this resource.".FormatWith(context.Request.HttpMethod)); }
protected override void ProcessRequest(HttpContext context, ICmsEntityServiceProvider serviceProvider, Guid portalScopeId, IPortalContext portal, OrganizationServiceContext serviceContext, Entity entity, CmsEntityMetadata entityMetadata, ICrmEntitySecurityProvider security) { if (!(IsRequestMethod(context.Request, "POST") || IsRequestMethod(context.Request, "DELETE"))) { throw new CmsEntityServiceException(HttpStatusCode.MethodNotAllowed, "Request method {0} not allowed for this resource.".FormatWith(context.Request.HttpMethod)); } CrmEntityInactiveInfo inactiveInfo; if (CrmEntityInactiveInfo.TryGetInfo(entity.LogicalName, out inactiveInfo)) { serviceContext.SetState(inactiveInfo.InactiveState, inactiveInfo.InactiveStatus, entity); WriteNoContentResponse(context.Response); return; } if (entity.LogicalName == "adx_communityforumthread") { var forum = entity.GetRelatedEntity(serviceContext, new Relationship("adx_communityforum_communityforumthread")); if (forum != null) { var forumDataAdapter = new ForumDataAdapter( forum.ToEntityReference(), new PortalConfigurationDataAdapterDependencies(portalName: PortalName, requestContext: context.Request.RequestContext)); forumDataAdapter.DeleteThread(entity.ToEntityReference()); WriteNoContentResponse(context.Response); if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.TelemetryFeatureUsage)) { PortalFeatureTrace.TraceInstance.LogFeatureUsage(FeatureTraceCategory.Forum, HttpContext.Current, "delete_forumthread", 1, entity.ToEntityReference(), "delete"); } return; } } if (entity.LogicalName == "adx_communityforumpost") { var forumThread = entity.GetRelatedEntity(serviceContext, new Relationship("adx_communityforumthread_communityforumpost")); if (forumThread != null) { var forumThreadDataAdapter = new ForumThreadDataAdapter( forumThread.ToEntityReference(), new PortalConfigurationDataAdapterDependencies(portalName: PortalName, requestContext: context.Request.RequestContext)); forumThreadDataAdapter.DeletePost(entity.ToEntityReference()); WriteNoContentResponse(context.Response); if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.TelemetryFeatureUsage)) { PortalFeatureTrace.TraceInstance.LogFeatureUsage(FeatureTraceCategory.Forum, HttpContext.Current, "delete_forumpost", 1, entity.ToEntityReference(), "delete"); } return; } } serviceContext.DeleteObject(entity); serviceContext.SaveChanges(); WriteNoContentResponse(context.Response); if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.TelemetryFeatureUsage)) { PortalFeatureTrace.TraceInstance.LogFeatureUsage(FeatureTraceCategory.Blog, HttpContext.Current, "delete_blogpost", 1, entity.ToEntityReference(), "delete"); } }