Exemplo n.º 1
0
        public virtual void AddEntityMetadata(string portalName, IEditableCrmEntityControl control, Control container, Entity entity)
        {
            if (control == null || container == null || entity == null)
            {
                return;
            }

            if (entity.LogicalName == "adx_weblinkset")
            {
                // Output the service reference for the web link set itself.
                AddEntityServiceReference(control, entity, entity.GetAttributeValue <string>("adx_name"), container);

                // Output the service reference for the child web links of the set.
                AddEntityAssocationSetServiceReferenceForWebLinkSet(control, entity, "adx_weblinkset_weblink".ToRelationship(), container);
                AddEntityAssocationSetServiceReference(portalName, control, entity, "adx_weblinkset_weblink".ToRelationship(), container, "xrm-entity-{0}-update-ref");
                AddEntitySetSchemaMap(control, "adx_weblink", container);

                // Output the service reference and schema map for site web pages (required to create new web links).
                AddEntitySetServiceReference(control, "adx_webpage", container);
                AddEntitySetSchemaMap(control, "adx_webpage", container);

                string weblinkDeleteUriTemplate;

                if (TryGetCrmEntityDeleteDataServiceUriTemplate(control, "adx_weblink", out weblinkDeleteUriTemplate))
                {
                    AddServiceReference(control, weblinkDeleteUriTemplate, "xrm-uri-template xrm-entity-adx_weblink-delete-ref", container);
                }

                return;
            }

            string serviceUri;

            if (!TryGetDataServiceEntityUri(control, entity, out serviceUri))
            {
                return;
            }

            // Add the service reference to the bound entity.
            container.Controls.Add(new HyperLink {
                CssClass = "xrm-entity-ref", NavigateUrl = VirtualPathUtility.ToAbsolute(serviceUri), Text = string.Empty
            });

            string entityUrlServiceUri;

            // Add the service reference for getting the URL of the bound entity.
            if (TryGetCrmEntityUrlDataServiceUri(control, entity, out entityUrlServiceUri))
            {
                AddServiceReference(control, entityUrlServiceUri, "xrm-entity-url-ref", container, "GetEntityUrl");
            }

            var crmEntityName = entity.LogicalName;

            AddEntitySetSchemaMap(control, crmEntityName, container);

            // If the entity is "deletable", add a service reference for soft-delete of the entity.
            if (DeletableEntityNames.Contains(crmEntityName))
            {
                string deleteServiceUri;

                if (TryGetCrmEntityDeleteDataServiceUri(control, entity, out deleteServiceUri))
                {
                    AddServiceReference(control, deleteServiceUri, "xrm-entity-delete-ref", container);
                }
            }

            if (FileAttachmentEntityNames.Contains(crmEntityName))
            {
                string fileAttachmentServiceUri;

                if (TryGetCrmEntityFileAttachmentDataServiceUri(control, entity, out fileAttachmentServiceUri))
                {
                    AddServiceReference(control, fileAttachmentServiceUri, "xrm-entity-attachment-ref", container);
                }
            }

            // Add the service references on which the creation of various entities are dependent.
            foreach (var dependencyEntityName in DependencyEntityNames)
            {
                AddEntitySetServiceReference(control, dependencyEntityName, container);
                AddEntitySetSchemaMap(control, dependencyEntityName, container);
            }

            // Add the service reference URI Templates for the notes associated with given entity types.
            foreach (var fileAttachmentEntity in FileAttachmentEntityNames)
            {
                string uriTemplate;

                if (TryGetCrmEntityFileAttachmentDataServiceUriTemplate(control, fileAttachmentEntity, out uriTemplate))
                {
                    AddServiceReference(control, uriTemplate, "xrm-uri-template xrm-entity-{0}-attachment-ref".FormatWith(fileAttachmentEntity), container);
                }
            }

            // Add the service reference URI Templates for getting URLs for specific entity types.
            foreach (var urlEntityName in UrlEntityNames)
            {
                string uriTemplate;

                if (TryGetCrmEntityUrlDataServiceUriTemplate(control, urlEntityName, out uriTemplate))
                {
                    AddServiceReference(control, uriTemplate, "xrm-uri-template xrm-entity-{0}-url-ref".FormatWith(urlEntityName), container, "GetEntityUrl");
                }
            }

            IEnumerable <Relationship> childAssociations;

            if (ChildAssociationsByEntityName.TryGetValue(crmEntityName, out childAssociations))
            {
                foreach (var childAssociation in childAssociations)
                {
                    AddEntityAssocationSetServiceReference(portalName, control, entity, childAssociation, container);
                }
            }

            Relationship parentalRelationship;

            // Output the URL path of parent entity to the DOM (mostly to be read if the entity is deleted--the user
            // will then be redirected to the parent).
            if (_parentalRelationshipsByEntityName.TryGetValue(crmEntityName, out parentalRelationship))
            {
                var context = PortalCrmConfigurationManager.GetServiceContext(PortalName);
                entity = context.MergeClone(entity);

                var parent = entity.GetRelatedEntity(context, parentalRelationship);

                var dependencyProvider = PortalCrmConfigurationManager.CreateDependencyProvider(PortalName);

                if (dependencyProvider == null)
                {
                    throw new InvalidOperationException("Unable to create {0} for current portal configuration.".FormatWith(typeof(IDependencyProvider).FullName));
                }

                var urlProvider = dependencyProvider.GetDependency <IEntityUrlProvider>();

                if (urlProvider == null)
                {
                    throw new InvalidOperationException("Unable to create {0} for current portal configuration.".FormatWith(typeof(IEntityUrlProvider).FullName));
                }

                var parentPath = urlProvider.GetApplicationPath(context, parent ?? entity);

                if (parentPath != null)
                {
                    AddServiceReference(control, parentPath.AbsolutePath, "xrm-entity-parent-url-ref", container);
                }
            }

            // Output the sitemarkers of the current web page into the DOM.
            if (crmEntityName == "adx_webpage")
            {
                var context = PortalCrmConfigurationManager.GetServiceContext(PortalName);
                entity = context.MergeClone(entity);

                foreach (var siteMarker in entity.GetRelatedEntities(context, "adx_webpage_sitemarker"))
                {
                    var siteMarkerRef = new HtmlGenericControl("span");

                    siteMarkerRef.Attributes["class"] = "xrm-entity-adx_webpage_sitemarker";
                    siteMarkerRef.Attributes["title"] = siteMarker.GetAttributeValue <string>("adx_name");

                    container.Controls.Add(siteMarkerRef);
                }

                AddEntitySetSchemaMap(control, "adx_webfile", container);
            }
        }
            public override void AddEntityMetadata(string portalName, IEditableCrmEntityControl control, Control container, Entity entity)
            {
                if (control == null || container == null || entity == null)
                {
                    return;
                }

                var serviceContext = PortalCrmConfigurationManager.CreateServiceContext(portalName);
                var portalContext  = PortalCrmConfigurationManager.CreatePortalContext(portalName);

                var metadata = ((RetrieveEntityResponse)serviceContext.Execute(new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.Entity,
                    LogicalName = entity.LogicalName
                })).EntityMetadata;

                var refetchedEntity = serviceContext.CreateQuery(entity.LogicalName).FirstOrDefault(e => e.GetAttributeValue <Guid>(metadata.PrimaryIdAttribute) == entity.Id);

                if (entity.LogicalName == "adx_weblinkset")
                {
                    // Output the service reference for the web link set itself.
                    AddEntityServiceReference(control, entity, entity.GetAttributeValue <string>("adx_name"), container);

                    // Output the service reference for the child web links of the set.
                    AddEntityAssocationSetServiceReferenceForWebLinkSet(control, entity, "adx_weblinkset_weblink".ToRelationship(), container);
                    AddEntityAssocationSetServiceReference(portalName, control, entity, "adx_weblinkset_weblink".ToRelationship(), container, "xrm-entity-{0}-update-ref");
                    AddEntitySetSchemaMap(control, "adx_weblink", container);

                    // Output the service reference and schema map for site web pages (required to create new web links).
                    AddEntitySetServiceReference(control, "adx_webpage", container);
                    AddEntitySetSchemaMap(control, "adx_webpage", container);

                    string weblinkDeleteUriTemplate;

                    if (TryGetCrmEntityDeleteDataServiceUriTemplate(control, "adx_weblink", out weblinkDeleteUriTemplate))
                    {
                        AddServiceReference(control, weblinkDeleteUriTemplate, "xrm-uri-template xrm-entity-adx_weblink-delete-ref", container);
                    }

                    // Output the service reference and schema map for site publishing states (required to create new web links).
                    AddEntitySetServiceReference(control, "adx_publishingstate", container);
                    AddEntitySetSchemaMap(control, "adx_publishingstate", container);
                }
                else
                {
                    string serviceUri;

                    if (!TryGetDataServiceEntityUri(control, entity, out serviceUri))
                    {
                        return;
                    }

                    // Add the service reference to the bound entity.
                    container.Controls.Add(new HyperLink {
                        CssClass = "xrm-entity-ref", NavigateUrl = VirtualPathUtility.ToAbsolute(serviceUri), Text = string.Empty
                    });

                    string entityUrlServiceUri;

                    // Add the service reference for getting the URL of the bound entity.
                    if (TryGetCrmEntityUrlDataServiceUri(control, entity, out entityUrlServiceUri))
                    {
                        AddServiceReference(control, entityUrlServiceUri, "xrm-entity-url-ref", container, "GetEntityUrl");
                    }

                    var crmEntityName = entity.LogicalName;

                    AddEntitySetSchemaMap(control, crmEntityName, container);

                    // If the entity is "deletable", add a service reference for soft-delete of the entity.
                    if (DeletableEntityNames.Contains(crmEntityName))
                    {
                        string deleteServiceUri;

                        if (TryGetCrmEntityDeleteDataServiceUri(control, entity, out deleteServiceUri))
                        {
                            AddServiceReference(control, deleteServiceUri, "xrm-entity-delete-ref", container);
                        }
                    }

                    if (FileAttachmentEntityNames.Contains(crmEntityName))
                    {
                        string fileAttachmentServiceUri;

                        if (TryGetCrmEntityFileAttachmentDataServiceUri(control, entity, out fileAttachmentServiceUri))
                        {
                            AddServiceReference(control, fileAttachmentServiceUri, "xrm-entity-attachment-ref", container);
                        }
                    }

                    // Add the service references on which the creation of various entities are dependent.
                    foreach (var dependencyEntityName in DependencyEntityNames)
                    {
                        AddEntitySetServiceReference(control, dependencyEntityName, container);
                        AddEntitySetSchemaMap(control, dependencyEntityName, container);
                    }

                    // Add the service reference URI Templates for the notes associated with given entity types.
                    foreach (var fileAttachmentEntity in FileAttachmentEntityNames)
                    {
                        string uriTemplate;

                        if (TryGetCrmEntityFileAttachmentDataServiceUriTemplate(control, fileAttachmentEntity, out uriTemplate))
                        {
                            AddServiceReference(control, uriTemplate, "xrm-uri-template xrm-entity-{0}-attachment-ref".FormatWith(fileAttachmentEntity), container);
                        }
                    }

                    // Add the service reference URI Templates for getting URLs for specific entity types.
                    foreach (var urlEntityName in UrlEntityNames)
                    {
                        string uriTemplate;

                        if (TryGetCrmEntityUrlDataServiceUriTemplate(control, urlEntityName, out uriTemplate))
                        {
                            AddServiceReference(control, uriTemplate, "xrm-uri-template xrm-entity-{0}-url-ref".FormatWith(urlEntityName), container, "GetEntityUrl");
                        }
                    }

                    IEnumerable <Relationship> childAssociations;

                    if (ChildAssociationsByEntityName.TryGetValue(crmEntityName, out childAssociations))
                    {
                        foreach (var childAssociation in childAssociations)
                        {
                            AddEntityAssocationSetServiceReference(portalName, control, entity, childAssociation, container);
                        }
                    }

                    Relationship parentalRelationship2;

                    // Output the URL path of parent entity to the DOM (mostly to be read if the entity is deleted--the user
                    // will then be redirected to the parent).
                    if (_parentalRelationshipsByEntityName.TryGetValue(crmEntityName, out parentalRelationship2))
                    {
                        var parent = refetchedEntity.GetRelatedEntity(serviceContext, parentalRelationship2);

                        var urlProvider = PortalCrmConfigurationManager.CreateDependencyProvider(PortalName).GetDependency <IEntityUrlProvider>();

                        var parentPath = urlProvider.GetApplicationPath(serviceContext, parent ?? refetchedEntity);

                        if (parentPath != null)
                        {
                            AddServiceReference(control, parentPath.AbsolutePath, "xrm-entity-parent-url-ref", container);
                        }
                    }

                    // Output the sitemarkers of the current web page into the DOM.
                    if (crmEntityName == "adx_webpage")
                    {
                        foreach (var siteMarker in refetchedEntity.GetRelatedEntities(serviceContext, "adx_webpage_sitemarker"))
                        {
                            var siteMarkerRef = new HtmlGenericControl("span");

                            siteMarkerRef.Attributes["class"] = "xrm-entity-adx_webpage_sitemarker";
                            siteMarkerRef.Attributes["title"] = siteMarker.GetAttributeValue <string>("adx_name");

                            container.Controls.Add(siteMarkerRef);
                        }

                        AddEntitySetSchemaMap(control, "adx_webfile", container);

                        EntitySetInfo entitySetInfo;

                        if (OrganizationServiceContextInfo.TryGet(GetCrmDataContextType(), "adx_communityforum", out entitySetInfo))
                        {
                            AddEntitySetSchemaMap(control, "adx_communityforum", container);
                            AddEntitySetServiceReference(control, "adx_communityforum", container);
                        }

                        if (OrganizationServiceContextInfo.TryGet(GetCrmDataContextType(), "adx_event", out entitySetInfo))
                        {
                            AddEntitySetSchemaMap(control, "adx_event", container);
                            AddEntitySetServiceReference(control, "adx_event", container);
                        }

                        AddEntitySetSchemaMap(control, "adx_shortcut", container);

                        AddEntitySetServiceReference(control, "adx_webpage", container);
                        AddEntitySetServiceReference(control, "adx_webfile", container);

                        AddPublishingTransitionSetServiceReference(control, container);
                    }

                    if (entity.LogicalName == "adx_event")
                    {
                        AddEntitySetSchemaMap(control, "adx_eventschedule", container);
                        AddPicklistMetadata(serviceContext, control, "adx_eventschedule", "adx_recurrence", container);
                    }
                }

                var previewPermission = new PreviewPermission(portalContext.ServiceContext, portalContext.Website);

                if (previewPermission.IsPermitted)
                {
                    var previewPermittedMetadata = new HtmlGenericControl("span");

                    previewPermittedMetadata.Attributes["class"] = "xrm-preview-permitted";
                    previewPermittedMetadata.Attributes["style"] = "display:none;";

                    container.Controls.Add(previewPermittedMetadata);
                }

                Relationship parentalRelationship;

                // Output the URL path of parent entity to the DOM (mostly to be read if the entity is deleted--the user
                // will then be redirected to the parent).
                if (_parentalRelationshipsByEntityName.TryGetValue(entity.LogicalName, out parentalRelationship))
                {
                    var parent = refetchedEntity.GetRelatedEntity(serviceContext, parentalRelationship);

                    var urlProvider = PortalCrmConfigurationManager.CreateDependencyProvider(PortalName).GetDependency <IEntityUrlProvider>();

                    var parentPath = urlProvider.GetApplicationPath(serviceContext, parent ?? refetchedEntity);

                    if (parentPath != null)
                    {
                        AddServiceReference(control, parentPath.AbsolutePath, "xrm-adx-entity-parent-url-ref", container);
                    }
                }
            }