public IItem GetPageIdByIshLogicalReference(int publicationId, string ishLogicalRefValue)
        {
            try
            {
                Criteria dateCriteria = new ItemLastPublishedDateCriteria(DefaultPublishData, Criteria.GreaterThanOrEqual);
                CustomMetaKeyCriteria metaKeyCriteria = new CustomMetaKeyCriteria(RefFieldName);
                Criteria refCriteria = new CustomMetaValueCriteria(metaKeyCriteria, ishLogicalRefValue);
                Criteria pubCriteria = new PublicationCriteria(publicationId);
                Criteria itemType    = new ItemTypeCriteria((int)ItemType.Page);
                Criteria composite   = new AndCriteria(new[] { dateCriteria, refCriteria, itemType, pubCriteria });

                Query   query = new Query(composite);
                IItem[] items = query.ExecuteEntityQuery();
                if (items == null || items.Length == 0)
                {
                    return(new ItemImpl());
                }

                if (items.Length > 1)
                {
                    throw new TridionDocsApiException($"Too many page Ids found in publication with logical ref value {ishLogicalRefValue}");
                }

                return(items[0]);
            }
            catch (Exception)
            {
                throw new DxaItemNotFoundException($"Page reference by ishlogicalref.object.id = {ishLogicalRefValue} not found in publication {publicationId}.");
            }
        }
Example #2
0
        public static IPageMeta GetPageMetaFromSEO(HttpRequestBase request, string url)
        {
            PageMetaFactory pageMetaFactory = new PageMetaFactory(SiteGlobal.PublicationId);
            IPageMeta       pageMeta        = pageMetaFactory.GetMetaByUrl(SiteGlobal.PublicationId, url);

            if (pageMeta == null)
            {
                // Check if it is a Vanity URL
                var publicationCriteria = new PublicationCriteria(SiteGlobal.PublicationId);
                var pageCriteria        = new ItemTypeCriteria((int)TridionItemType.Page);
                var vanityUrlCriteria   = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("SEOUrl", Criteria.Equal), url);

                // --

                Query query = new Query();
                query.Criteria = CriteriaFactory.And(new Criteria[] { publicationCriteria, pageCriteria, vanityUrlCriteria });
                IEnumerable <string> pages = query.ExecuteQuery();

                //If no result, we try taking the ApplicationPath ['/en'] fro the url
                if (!pages.Any() && url.ToLower().StartsWith(request.ApplicationPath.ToLower()))
                {
                    var urlWithoutAppPath  = url.Substring(request.ApplicationPath.Length);
                    var vanityUrlCriteria2 = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("SEOUrl", Criteria.Equal), urlWithoutAppPath);
                    query.Criteria = CriteriaFactory.And(new Criteria[] { publicationCriteria, pageCriteria, vanityUrlCriteria2 });
                    pages          = query.ExecuteQuery();

                    //If no result, we try taking the '/' from the begining of the url.
                    if (!pages.Any())
                    {
                        urlWithoutAppPath  = urlWithoutAppPath.TrimStart('/');
                        vanityUrlCriteria2 = new CustomMetaValueCriteria(new CustomMetaKeyCriteria("SEOUrl", Criteria.Equal), urlWithoutAppPath);
                        query.Criteria     = CriteriaFactory.And(new Criteria[] { publicationCriteria, pageCriteria, vanityUrlCriteria2 });
                        pages = query.ExecuteQuery();
                    }
                }

                if (pages.Any())
                {
                    pageMeta = pageMetaFactory.GetMeta(pages.First());
                }
            }
            return(pageMeta);
        }
Example #3
0
        public void TryRedirect(Object s, EventArgs e)
        {
            HttpContext context = HttpContext.Current;

            int    publicationId = int.Parse(ConfigurationManager.AppSettings["GlobalPubId"]);
            string redirectField = ConfigurationManager.AppSettings["RedirectUrlField"];

            string url = context.Request.Url.PathAndQuery;
            int    pos = url.IndexOf("?");

            if (pos > 0)
            {
                url = url.Substring(0, pos);
            }

            PublicationCriteria     publicationCriteria = new PublicationCriteria(publicationId);
            ItemTypeCriteria        itemCriteria        = new ItemTypeCriteria(64);
            CustomMetaValueCriteria cmvCriteria         = new CustomMetaValueCriteria(new CustomMetaKeyCriteria(redirectField), url);
            AndCriteria             andCriteria         = new AndCriteria(new Criteria[] { publicationCriteria, itemCriteria, cmvCriteria });

            Query query = new Query(andCriteria);

            string[] results = query.ExecuteQuery();

            if (results.Length > 0)
            {
                PageLink pageLink = new PageLink(publicationId);
                Link     link     = pageLink.GetLink(results[0]);

                if (link.IsResolved)
                {
                    // Redirect
                    HttpResponse response = context.Response;
                    response.Clear();
                    response.RedirectLocation  = link.Url;
                    response.StatusCode        = 301;
                    response.StatusDescription = "301 Moved Permanently";
                    response.Write("Page has moved to " + link.Url);
                    response.End();
                }
            }
        }
Example #4
0
        public void TryRedirect(Object s, EventArgs e)
        {
            HttpContext context = HttpContext.Current;

            int publicationId = int.Parse(ConfigurationManager.AppSettings["GlobalPubId"]);
            string redirectField = ConfigurationManager.AppSettings["RedirectUrlField"];

            string url = context.Request.Url.PathAndQuery;
            int pos = url.IndexOf("?");
            if (pos > 0) url = url.Substring(0, pos);

            PublicationCriteria publicationCriteria = new PublicationCriteria(publicationId);
            ItemTypeCriteria itemCriteria = new ItemTypeCriteria(64);
            CustomMetaValueCriteria cmvCriteria = new CustomMetaValueCriteria(new CustomMetaKeyCriteria(redirectField), url);
            AndCriteria andCriteria = new AndCriteria(new Criteria[] { publicationCriteria, itemCriteria, cmvCriteria });

            Query query = new Query(andCriteria);

            string[] results = query.ExecuteQuery();

            if (results.Length > 0)
            {
                PageLink pageLink = new PageLink(publicationId);
                Link link = pageLink.GetLink(results[0]);

                if (link.IsResolved)
                {
                    // Redirect
                    HttpResponse response = context.Response;
                    response.Clear();
                    response.RedirectLocation = link.Url;
                    response.StatusCode = 301;
                    response.StatusDescription = "301 Moved Permanently";
                    response.Write("Page has moved to " + link.Url);
                    response.End();
                }
            }
        }
Example #5
0
        public Tridion.ContentDelivery.DynamicContent.Query.Query ToTridionQuery()
        {
            string[] basedOnSchemas              = QuerySchemas;
            DateTime lastPublishedDate           = LastPublishedDate;
            IList <MetaQueryItem> metaQueryItems = MetaQueryValues;
            QueryLogic            metaQueryLogic = MetaQueryLogic;
            int maxmimumComponents = MaximumComponents;

            Query q = null;
            //PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId);
            PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId);
            //format DateTime // 00:00:00.000
            ItemLastPublishedDateCriteria dateLastPublished = new ItemLastPublishedDateCriteria(lastPublishedDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), Criteria.GreaterThanOrEqual);
            //publicationAndLastPublishedDateCriteria.AddCriteria(dateLastPublished);

            Criteria basedOnSchemaAndInPublication;

            if (basedOnSchemas.Length > 0)
            {
                Criteria[] schemaCriterias = new Criteria[basedOnSchemas.Length];
                int        i = 0;
                foreach (var schema in basedOnSchemas)
                {
                    TcmUri schemaUri = new TcmUri(schema);
                    schemaCriterias.SetValue(new ItemSchemaCriteria(schemaUri.ItemId), i);
                    i++;
                }
                Criteria basedOnSchema = CriteriaFactory.Or(schemaCriterias);
                basedOnSchemaAndInPublication = CriteriaFactory.And(publicationAndLastPublishedDateCriteria, basedOnSchema);
            }
            else
            {
                basedOnSchemaAndInPublication = publicationAndLastPublishedDateCriteria;
            }

            // Add filtering for meta data
            Criteria schemasAndMetaData;

            if (metaQueryItems.Count > 0)
            {
                Criteria   metaQuery;
                Criteria[] metaCriterias = new Criteria[metaQueryItems.Count];
                int        metaCount     = 0;
                foreach (MetaQueryItem queryItem in metaQueryItems)
                {
                    CustomMetaKeyCriteria   metaField = new CustomMetaKeyCriteria(queryItem.MetaField);
                    CustomMetaValueCriteria metaCriteria;
                    FieldOperator           metaOperator = typeof(Criteria).GetField(queryItem.MetaOperator.ToString()).GetValue(null) as FieldOperator;

                    switch (queryItem.MetaValue.GetType().Name)
                    {
                    case "DateTime":
                        DateTime tempDate = (DateTime)queryItem.MetaValue;
                        metaCriteria = new CustomMetaValueCriteria(metaField, tempDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), "yyyy-MM-dd HH:mm:ss.SSS", metaOperator);
                        break;

                    case "Float":
                        metaCriteria = new CustomMetaValueCriteria(metaField, (float)queryItem.MetaValue, metaOperator);
                        break;

                    case "String":
                        metaCriteria = new CustomMetaValueCriteria(metaField, queryItem.MetaValue as string, metaOperator);
                        break;

                    default:
                        throw new System.Exception("Unexpected query item data type; " + queryItem.MetaValue.GetType().Name);
                    }

                    metaCriterias.SetValue(metaCriteria, metaCount);
                    metaCount++;
                }

                if (MetaQueryLogic == QueryLogic.AllCriteriaMatch)
                {
                    metaQuery = CriteriaFactory.And(metaCriterias);
                }
                else
                {
                    metaQuery = CriteriaFactory.Or(metaCriterias);
                }
                schemasAndMetaData = CriteriaFactory.And(basedOnSchemaAndInPublication, metaQuery);
            }
            else
            {
                schemasAndMetaData = basedOnSchemaAndInPublication;
            }

            Criteria allConditions;

            if (KeywordValues.Count > 0)
            {
                Criteria[] keywordCriterias = new Criteria[KeywordValues.Count];
                int        keywordCount     = 0;
                foreach (KeywordItem keyCriteria in KeywordValues)
                {
                    TaxonomyKeywordCriteria keywordField = new TaxonomyKeywordCriteria(keyCriteria.CategoryUri, keyCriteria.KeywordUri, false);
                    keywordCriterias.SetValue(keywordField, keywordCount);
                    keywordCount++;
                }

                Criteria keyQuery;
                if (KeywordQueryLogic == QueryLogic.AllCriteriaMatch)
                {
                    keyQuery = CriteriaFactory.And(keywordCriterias);
                }
                else
                {
                    keyQuery = CriteriaFactory.Or(keywordCriterias);
                }
                allConditions = CriteriaFactory.And(schemasAndMetaData, keyQuery);
            }
            else
            {
                allConditions = schemasAndMetaData;
            }


            q = new Query(allConditions);
            if (maxmimumComponents != 0 && maxmimumComponents != int.MaxValue)
            {
                LimitFilter limitResults = new LimitFilter(maxmimumComponents);
                q.SetResultFilter(limitResults);
            }

            // Sort column should either be a standard or custom metaData field
            SortColumn paramSort;

            if (typeof(SortParameter).GetField(QuerySortField) != null)
            {
                paramSort = typeof(SortParameter).GetField(QuerySortField).GetValue(null) as SortColumn;
            }
            else
            {
                // Why do we need to tell Tridion what data type the field is! Its in the database already!
                /* FIX: Does not work (null pointer exception): var sType = typeof(MetadataType).GetField(SortType.ToString()).GetValue(null) as MetadataType;*/
                paramSort = new CustomMetaKeyColumn(QuerySortField, SortType);
            }
            SortDirection paramSortDirection = typeof(SortParameter).GetField(QuerySortOrder.ToString()).GetValue(null) as SortDirection;
            SortParameter sortParameter      = new SortParameter(paramSort, paramSortDirection);

            q.AddSorting(sortParameter);
            return(q);
        }
        public Tridion.ContentDelivery.DynamicContent.Query.Query ToTridionQuery()
        {
            string[] basedOnSchemas = QuerySchemas;
            DateTime lastPublishedDate = LastPublishedDate;
            IList<MetaQueryItem> metaQueryItems = MetaQueryValues;
            QueryLogic metaQueryLogic = MetaQueryLogic;
            int maxmimumComponents = MaximumComponents;

            Query q = null;
            //PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId);
            PublicationCriteria publicationAndLastPublishedDateCriteria = new PublicationCriteria(PublicationId);
            //format DateTime // 00:00:00.000
            ItemLastPublishedDateCriteria dateLastPublished = new ItemLastPublishedDateCriteria(lastPublishedDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), Criteria.GreaterThanOrEqual);
            //publicationAndLastPublishedDateCriteria.AddCriteria(dateLastPublished);

            Criteria basedOnSchemaAndInPublication;

            if (basedOnSchemas.Length > 0)
            {
                Criteria[] schemaCriterias = new Criteria[basedOnSchemas.Length];
                int i = 0;
                foreach (var schema in basedOnSchemas)
                {
                    TcmUri schemaUri = new TcmUri(schema);
                    schemaCriterias.SetValue(new ItemSchemaCriteria(schemaUri.ItemId), i);
                    i++;
                }
                Criteria basedOnSchema = CriteriaFactory.Or(schemaCriterias);
                basedOnSchemaAndInPublication = CriteriaFactory.And(publicationAndLastPublishedDateCriteria, basedOnSchema);
            }
            else
            {
                basedOnSchemaAndInPublication = publicationAndLastPublishedDateCriteria;
            }

            // Add filtering for meta data
            Criteria schemasAndMetaData;
            if (metaQueryItems.Count > 0)
            {
                Criteria metaQuery;
                Criteria[] metaCriterias = new Criteria[metaQueryItems.Count];
                int metaCount = 0;
                foreach (MetaQueryItem queryItem in metaQueryItems)
                {
                    CustomMetaKeyCriteria metaField = new CustomMetaKeyCriteria(queryItem.MetaField);
                    CustomMetaValueCriteria metaCriteria;
                    FieldOperator metaOperator = typeof(Criteria).GetField(queryItem.MetaOperator.ToString()).GetValue(null) as FieldOperator;

                    switch (queryItem.MetaValue.GetType().Name)
                    {
                        case "DateTime":
                            DateTime tempDate = (DateTime)queryItem.MetaValue;
                            metaCriteria = new CustomMetaValueCriteria(metaField, tempDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), "yyyy-MM-dd HH:mm:ss.SSS", metaOperator);
                            break;
                        case "Float":
                            metaCriteria = new CustomMetaValueCriteria(metaField, (float)queryItem.MetaValue, metaOperator);
                            break;
                        case "String":
                            metaCriteria = new CustomMetaValueCriteria(metaField, queryItem.MetaValue as string, metaOperator);
                            break;
                        default:
                            throw new System.Exception("Unexpected query item data type; " + queryItem.MetaValue.GetType().Name);
                    }

                    metaCriterias.SetValue(metaCriteria, metaCount);
                    metaCount++;
                }

                if (MetaQueryLogic == QueryLogic.AllCriteriaMatch)
                {
                    metaQuery = CriteriaFactory.And(metaCriterias);
                }
                else
                {
                    metaQuery = CriteriaFactory.Or(metaCriterias);
                }
                schemasAndMetaData = CriteriaFactory.And(basedOnSchemaAndInPublication, metaQuery);
            }
            else
            {
                schemasAndMetaData = basedOnSchemaAndInPublication;
            }

            Criteria allConditions;
            if (KeywordValues.Count > 0)
            {
                Criteria[] keywordCriterias = new Criteria[KeywordValues.Count];
                int keywordCount = 0;
                foreach (KeywordItem keyCriteria in KeywordValues)
                {
                    TaxonomyKeywordCriteria keywordField = new TaxonomyKeywordCriteria(keyCriteria.CategoryUri, keyCriteria.KeywordUri, false);
                    keywordCriterias.SetValue(keywordField, keywordCount);
                    keywordCount++;
                }

                Criteria keyQuery;
                if (KeywordQueryLogic == QueryLogic.AllCriteriaMatch)
                {
                    keyQuery = CriteriaFactory.And(keywordCriterias);
                }
                else
                {
                    keyQuery = CriteriaFactory.Or(keywordCriterias);
                }
                allConditions = CriteriaFactory.And(schemasAndMetaData, keyQuery);
            }
            else
            {
                allConditions = schemasAndMetaData;
            }


            q = new Query(allConditions);
            if (maxmimumComponents != 0 && maxmimumComponents != int.MaxValue)
            {
                LimitFilter limitResults = new LimitFilter(maxmimumComponents);
                q.SetResultFilter(limitResults);
            }

            // Sort column should either be a standard or custom metaData field
            SortColumn paramSort;
            if (typeof(SortParameter).GetField(QuerySortField) != null)
            {
                paramSort = typeof(SortParameter).GetField(QuerySortField).GetValue(null) as SortColumn;
            }
            else
            {
                // Why do we need to tell Tridion what data type the field is! Its in the database already!
                paramSort = new CustomMetaKeyColumn(QuerySortField, typeof(MetadataType).GetField(SortType.ToString()).GetValue(null) as MetadataType);
            }
            SortDirection paramSortDirection = typeof(SortParameter).GetField(QuerySortOrder.ToString()).GetValue(null) as SortDirection;
            SortParameter sortParameter = new SortParameter(paramSort, paramSortDirection);
            q.AddSorting(sortParameter);
            return q;
        }