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);
        }
        private static void AddRouteServiceReferenceAttribute(ICmsEntityEditingMetadataContainer container, string name, string routeName, object routeValues)
        {
            var current = HttpContext.Current;

            if (current == null)
            {
                return;
            }

            var http      = new HttpContextWrapper(current);
            var urlHelper = new UrlHelper(new RequestContext(http, RouteTable.Routes.GetRouteData(http) ?? new RouteData()), RouteTable.Routes);

            try
            {
                var url = urlHelper.RouteUrl(routeName, routeValues);

                if (string.IsNullOrEmpty(url))
                {
                    return;
                }

                container.AddAttribute(name, url);
            }
            // If the route isn't found, just silently fail to add the data attribute.
            catch (ArgumentException) {}
        }
        public void AddEntityMetadata(ICmsEntityEditingMetadataContainer container, string entityLogicalName, string portalName = null, string entityDisplayName = null, JObject initialValues = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            var portal  = PortalCrmConfigurationManager.CreatePortalContext(portalName ?? PortalName);
            var website = portal.Website.ToEntityReference();

            container.AddLabel(entityDisplayName);
            container.AddAttribute("data-xrm-base", VirtualPathUtility.ToAbsolute("~/xrm-adx/"));
            container.AddAttribute("data-logicalname", entityLogicalName);

            AddFileBrowserServiceReference(container, portal, FileBrowserDirectoryEntityNames.Contains(entityLogicalName));
            AddEntityTemplateServiceReference(container, portal, website, entityLogicalName);
            AddEntityTemplateRenderServiceReference(container, portal, website, entityLogicalName);

            if (initialValues != null)
            {
                container.AddAttribute("data-create-initial", initialValues.ToString(Formatting.None));
            }

            if (entityLogicalName == "adx_contentsnippet")
            {
                container.AddAttribute("data-create-url", VirtualPathUtility.ToAbsolute(CmsEntityRelationshipRouteHandler.GetAppRelativePath(website.Id, website, new Relationship("adx_website_contentsnippet"))));
                container.AddAttribute("data-create-attribute", "adx_value");
                container.AddAttribute("data-editable-uritemplate", VirtualPathUtility.ToAbsolute(CmsEntityAttributeRouteHandler.GetAppRelativePathTemplate(website.Id, entityLogicalName, "Id", "adx_value")));
            }
        }
        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 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");
        }