protected virtual void AddTagMetadata(ICmsEntityEditingMetadataContainer container, string entityLogicalName, IEnumerable <string> tags)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            container.AddTagMetadata(entityLogicalName, tags);
        }
        protected virtual void AddServiceReference(ICmsEntityEditingMetadataContainer container, string servicePath, string cssClass, string title = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            container.AddServiceReference(servicePath, cssClass, title);
        }
        protected virtual void AddFileBrowserServiceReference(ICmsEntityEditingMetadataContainer container, IPortalContext portal, bool setWorkingDirectory = false)
        {
            var elFinderConnectorPath = VirtualPathUtility.ToAbsolute(ElFinderRouteHandler.GetAppRelativePath(portal.Website.Id));
            var elFinderConnectorUrl  = new UrlBuilder(elFinderConnectorPath);

            if (setWorkingDirectory && portal.Entity != null)
            {
                elFinderConnectorUrl.QueryString.Set("working", new DirectoryContentHash(portal.Entity.ToLanguageContainerEntityReference()).ToString());
            }

            container.AddAttribute("data-filebrowser-url", elFinderConnectorUrl.PathWithQueryString);
            container.AddAttribute("data-filebrowser-dialog-url", ElFinderRouteHandler.DialogPath);
        }
        protected virtual void AddPicklistMetadata(ICmsEntityEditingMetadataContainer container, OrganizationServiceContext serviceContext, string entityLogicalName, string attributeLogicalName)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            Dictionary <int, string> options;

            if (!TryGetPicklistOptions(serviceContext, entityLogicalName, attributeLogicalName, out options))
            {
                return;
            }

            container.AddPicklistMetadata(entityLogicalName, attributeLogicalName, options);
        }
        public virtual void AddEntityMetadata(ICmsEntityEditingMetadataContainer container, EntityReference entity, string portalName = null, string entityDisplayName = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

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

            var portal = PortalCrmConfigurationManager.CreatePortalContext(PortalName);

            AddEntityMetadata(container, entity, portal, true, portalName, entityDisplayName);
        }
        protected virtual void AddEntityTemplateRenderServiceReference(ICmsEntityEditingMetadataContainer container, IPortalContext portal, EntityReference entity, string context = null)
        {
            var current = HttpContext.Current;

            if (current == null)
            {
                return;
            }

            AddRouteServiceReferenceAttribute(container, "data-cmstemplate-render-url", "CmsTemplate_GetLivePreview", new
            {
                __portalScopeId__ = portal.Website.Id,
                entityLogicalName = entity.LogicalName,
                id = entity.Id,
                __currentSiteMapNodeUrl__ = Convert.ToBase64String(Encoding.UTF8.GetBytes(current.Request.Url.AbsolutePath)),
                context
            });
        }
        protected virtual void AddSiteMapNodeMetadata(ICmsEntityEditingMetadataContainer container, EntityReference entity, IPortalContext portal, string portalName = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

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

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

            IEnumerable <string> siteMapChildEntityNames;

            if (!SiteMapChildEntitiesByEntityName.TryGetValue(entity.LogicalName, out siteMapChildEntityNames))
            {
                return;
            }

            var serviceContext = PortalCrmConfigurationManager.CreateServiceContext(portalName ?? PortalName);
            var allEntities    = GetEntityDictionary(serviceContext);

            foreach (var entityName in siteMapChildEntityNames.Where(entityName => EntityNameExistsInSchema(entityName, allEntities)))
            {
                AddEntityServiceReferenceTemplate(container, portal, entityName);

                if (DeletableEntityNames.Contains(entityName))
                {
                    AddEntityDeleteServiceReferenceTemplate(container, portal, entityName);
                }
            }
        }
        public virtual void AddAttributeMetadata(ICmsEntityEditingMetadataContainer container, EntityReference entity, string attributeLogicalName, string attributeDisplayName, string portalName = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

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

            var portal      = PortalCrmConfigurationManager.CreatePortalContext(portalName ?? PortalName);
            var servicePath = CmsEntityAttributeRouteHandler.GetAppRelativePath(portal.Website.Id, entity, attributeLogicalName);

            container.AddLabel(attributeDisplayName);
            container.AddAttribute("data-xrm-base", VirtualPathUtility.ToAbsolute("~/xrm-adx/"));
            container.AddAttribute("data-editable-url", VirtualPathUtility.ToAbsolute(servicePath));
            container.AddAttribute("data-editable-title", attributeDisplayName ?? attributeLogicalName);

            AddEntityTemplateServiceReference(container, portal, entity);
            AddEntityTemplateRenderServiceReference(container, portal, entity);
            AddFileBrowserServiceReference(container, portal, FileBrowserDirectoryEntityNames.Contains(entity.LogicalName));
        }
        protected virtual void AddEntityUrlServiceReferenceTemplate(ICmsEntityEditingMetadataContainer container, IPortalContext portal, string entityLogicalName, string idTemplateVariableName = "Id")
        {
            var servicePath = CmsEntityUrlRouteHandler.GetAppRelativePathTemplate(portal.Website.Id, entityLogicalName, idTemplateVariableName);

            AddServiceReference(container, servicePath, "xrm-uri-template xrm-entity-{0}-url-ref".FormatWith(entityLogicalName), "Url");
        }
        protected virtual void AddEntityUrlServiceReference(ICmsEntityEditingMetadataContainer container, IPortalContext portal, EntityReference entity)
        {
            var servicePath = CmsEntityUrlRouteHandler.GetAppRelativePath(portal.Website.Id, entity);

            AddServiceReference(container, servicePath, "xrm-entity-url-ref", "Url");
        }
        protected virtual void AddEntitySetServiceReference(ICmsEntityEditingMetadataContainer container, IPortalContext portal, string entityLogicalName, string title = null)
        {
            var servicePath = CmsEntitySetRouteHandler.GetAppRelativePath(portal.Website.Id, entityLogicalName);

            AddServiceReference(container, servicePath, "xrm-entity-{0}-ref".FormatWith(entityLogicalName), title);
        }
        protected virtual void AddEntityRelationshipServiceReferenceTemplate(ICmsEntityEditingMetadataContainer container, IPortalContext portal, string entityLogicalName, Relationship relationship, string idTemplateVariableName = "Id", string cssClassFormat = "xrm-entity-{0}-ref")
        {
            var servicePath = CmsEntityRelationshipRouteHandler.GetAppRelativePathTemplate(portal.Website.Id, entityLogicalName, idTemplateVariableName, relationship);

            AddServiceReference(container, servicePath, "xrm-uri-template " + cssClassFormat.FormatWith(relationship.ToSchemaName("_")));
        }
        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("_")));
        }
        protected virtual void AddEntityFileAttachmentServiceReference(ICmsEntityEditingMetadataContainer container, IPortalContext portal, EntityReference entity, string title = null)
        {
            var servicePath = CmsEntityFileAttachmentRouteHandler.GetAppRelativePath(portal.Website.Id, entity);

            AddServiceReference(container, servicePath, "xrm-entity-attachment-ref", title);
        }
        protected virtual void AddEntityChildrenServiceReference(ICmsEntityEditingMetadataContainer container, IPortalContext portal, EntityReference entity, string cssClass, string title = null)
        {
            var servicePath = CmsEntityChildrenRouteHandler.GetAppRelativePath(portal.Website.Id, entity);

            AddServiceReference(container, servicePath, cssClass, title);
        }
        protected virtual void AddEntityMetadata(ICmsEntityEditingMetadataContainer container, EntityReference entityReference, IPortalContext portal, bool addSiteMapNodeMetadata, string portalName = null, string entityDisplayName = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

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

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

            container.AddLabel(entityDisplayName ?? entityReference.Name);
            container.AddAttribute("data-xrm-base", VirtualPathUtility.ToAbsolute("~/xrm-adx/"));
            container.AddAttribute("data-logicalname", entityReference.LogicalName);
            container.AddAttribute("data-id", entityReference.Id.ToString());

            AddRouteServiceReferenceAttribute(container, "data-parentoptions", "CmsParent_GetParentOptions", new
            {
                __portalScopeId__ = portal.Website.Id
            });

            container.AddAttribute("data-parentoptions-uritemplate", VirtualPathUtility.ToAbsolute(CmsParentController.GetAppRelativePathTemplate(portal.Website.Id, "LogicalName", "Id")));

            portalName = portalName ?? PortalName;

            var serviceContext = PortalCrmConfigurationManager.CreateServiceContext(portalName);
            var entity         = GetEntity(serviceContext, entityReference);

            AddEntityTemplateServiceReference(container, portal, entityReference);
            AddFileBrowserServiceReference(container, portal, FileBrowserDirectoryEntityNames.Contains(entity.LogicalName));

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

                // Output the service reference for the child web links of the set.
                AddEntityRelationshipServiceReference(container, portal, entityReference, new Relationship("adx_weblinkset_weblink"));
                AddEntityRelationshipServiceReference(container, portal, entityReference, new Relationship("adx_weblinkset_weblink"), "xrm-entity-{0}-update-ref");

                AddEntityDeleteServiceReferenceTemplate(container, portal, "adx_weblink");

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

                // Output the service reference and schema map for site publishing states (required to create new web links).
                AddEntitySetServiceReference(container, portal, "adx_publishingstate");

                return;
            }

            var allEntities = GetEntityDictionary(serviceContext);

            AddEntityServiceReference(container, portal, entityReference);
            AddEntityUrlServiceReference(container, portal, entityReference);

            // If the entity is deletable, add a service reference for delete of the entity.
            if (DeletableEntityNames.Contains(entityReference.LogicalName))
            {
                AddEntityDeleteServiceReference(container, portal, entityReference);
            }

            if (FileAttachmentEntityNames.Contains(entityReference.LogicalName))
            {
                AddEntityFileAttachmentServiceReference(container, portal, entityReference);
            }

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

            // Add the service reference URI Templates for the notes associated with given entity types.
            foreach (var fileAttachmentEntity in FileAttachmentEntityNames)
            {
                AddEntityFileAttachmentServiceReferenceTemplate(container, portal, fileAttachmentEntity);
            }

            // Add the service reference URI Templates for getting URLs for specific entity types.
            foreach (var urlEntityName in UrlEntityNames.Where(entityName => EntityNameExistsInSchema(entityName, allEntities)))
            {
                AddEntityUrlServiceReferenceTemplate(container, portal, urlEntityName);
            }

            IEnumerable <Relationship> childRelationships;

            if (ChildRelationshipsByEntityName.TryGetValue(entityReference.LogicalName, out childRelationships))
            {
                foreach (var relationship in childRelationships.Where(relationship => RelationshipExistsInSchema(relationship, allEntities)))
                {
                    AddEntityRelationshipServiceReference(container, portal, entityReference, relationship);
                }
            }

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

            if (previewPermission.IsPermitted)
            {
                container.AddPreviewPermittedMetadata();
            }

            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(entityReference.LogicalName, out parentalRelationship))
            {
                var parent = entity.GetRelatedEntity(serviceContext, parentalRelationship);

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

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

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

            // Output the sitemarkers of the current web page into the DOM.
            if (entityReference.LogicalName == "adx_webpage")
            {
                foreach (var siteMarker in entity.GetRelatedEntities(serviceContext, "adx_webpage_sitemarker"))
                {
                    container.AddSiteMarkerMetadata(entityReference.LogicalName, siteMarker.GetAttributeValue <string>("adx_name"));
                }

                if (EntityNameExistsInSchema("adx_communityforum", allEntities))
                {
                    AddEntitySetServiceReference(container, portal, "adx_communityforum");
                }

                if (EntityNameExistsInSchema("adx_event", allEntities))
                {
                    AddEntitySetServiceReference(container, portal, "adx_event");
                }

                if (EntityNameExistsInSchema("adx_entityform", allEntities))
                {
                    AddEntitySetServiceReference(container, portal, "adx_entityform");
                }

                if (EntityNameExistsInSchema("adx_entitylist", allEntities))
                {
                    AddEntitySetServiceReference(container, portal, "adx_entitylist");
                }

                if (EntityNameExistsInSchema("adx_webform", allEntities))
                {
                    AddEntitySetServiceReference(container, portal, "adx_webform");
                }

                if (EntityNameExistsInSchema("adx_weblinkset", allEntities))
                {
                    AddEntitySetServiceReference(container, portal, "adx_weblinkset");
                }

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

                AddPublishingStateSetServiceReference(container, portal);

                AddPicklistMetadata(container, serviceContext, "adx_webpage", "adx_feedbackpolicy");

                AddEntityRelationshipServiceReferenceTemplate(container, portal, "adx_webpage", "adx_webpage_navigation_weblinkset".ToRelationship());

                if (entity.GetAttributeValue <EntityReference>("adx_parentpageid") == null && string.Equals(entity.GetAttributeValue <string>("adx_partialurl"), "/", StringComparison.OrdinalIgnoreCase))
                {
                    container.AddAttribute("data-root", "true");
                }

                var langContext = HttpContext.Current.GetContextLanguageInfo();

                // For multi language portals, add root webpage id to the dom
                if (langContext.IsCrmMultiLanguageEnabled)
                {
                    //add language information
                    container.AddAttribute("data-languagename", langContext.ContextLanguage.Name);
                    container.AddAttribute("data-languageid", langContext.ContextLanguage.EntityReference.Id.ToString());

                    var rootPageReference = portal.Entity.GetAttributeValue <EntityReference>("adx_rootwebpageid");

                    if (rootPageReference != null)
                    {
                        container.AddAttribute("data-rootwebpageid", rootPageReference.Id.ToString());
                    }
                    else
                    {
                        ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Root page for current content page is null, id ={0}", entityReference.Id));
                    }
                }
            }

            if (entityReference.LogicalName == "adx_event")
            {
                AddPicklistMetadata(container, serviceContext, "adx_eventschedule", "adx_recurrence");
                AddPicklistMetadata(container, serviceContext, "adx_eventschedule", "adx_week");
            }

            if (addSiteMapNodeMetadata && SiteMapChildEntitiesByEntityName.ContainsKey(entityReference.LogicalName))
            {
                container.AddCssClass("xrm-editable-sitemapchildren");

                AddEntityChildrenServiceReference(container, portal, entityReference, "xrm-entity-ref-sitemapchildren", GetEntityName(entity));

                AddSiteMapNodeMetadata(container, entityReference, portal, portalName);
            }

            if (entityReference.LogicalName == "adx_blog" || entityReference.LogicalName == "adx_webpage")
            {
                AddPicklistMetadata(container, serviceContext, "adx_blog", "adx_commentpolicy");
            }

            if (entityReference.LogicalName == "adx_blog" || entityReference.LogicalName == "adx_blogpost")
            {
                AddPicklistMetadata(container, serviceContext, "adx_blogpost", "adx_commentpolicy");

                var tags = GetWebsiteTags(portal, serviceContext);

                AddTagMetadata(container, "adx_blogpost", tags);
            }

            if (entityReference.LogicalName == "adx_communityforumthread")
            {
                AddEntitySetServiceReference(container, portal, "adx_forumthreadtype");

                var tags = GetWebsiteTags(portal, serviceContext);

                AddTagMetadata(container, "adx_communityforumthread", tags);
            }

            AddPicklistMetadata(container, serviceContext, "adx_webfile", "adx_contentdisposition");
            AddEntitySetServiceReference(container, portal, "subject");
        }