Map sitecore 8 general link field from Index

2019-08-12 19:37发布

问题:

I am using sitecore content search API to get the general link field from sitecore index. Below is my model

public class GrantItem : BaseSearchItem
{
    [IndexField("grant_name")]
    public string GrantName { get; set; }

    [IndexField("grant_description")]
    public string GrantDescription { get; set; }

    [IndexField("grant_categories_sm")]
    public CategoryItem[] GrantCategories { get; set; }

    [IndexField("grant_status_s")]
    public CategoryItem GrantStatus { get; set; }

    [IndexField("grant_application_type_sm")]
    [TypeConverter(typeof(IndexFieldIDValueConverter))]
    public IEnumerable<ID> ApplicationType { get; set; }

    [IndexField("grant_url")]
    public string GrantURL { get; set; }
}

When I am using sitecore content search API. I am getting the following xml in the GrantURL.

 <link linktype=\"external\" url=\"http://www.google.com.au\" anchor=\"\"    
 target=\"\" /> 

Is there any default converter in sitecore to map this to a Link field or do I need to parse it manually or create a custom converter ?

回答1:

As far as I am aware there no type converters for this.

It's quite easy to create your own though. This blog post has code that worked for me and you just have to implement the ConvertFrom and ConvertTo methods

http://reasoncodeexample.com/2014/01/30/indexing-datetime-fields-sitecore-7-content-search/



回答2:

@foreach (var resultItem in Model.SearchRequest.SearchResults)
                            {
                                <tr>
                                    @{
                                        XmlDocument doc = new XmlDocument();
                                        doc.LoadXml(resultItem.CrossSiteUrl);
                                        XmlElement root = doc.DocumentElement;
                                        string path = String.Empty;
                                        string target = String.Empty;
                                        if (!string.IsNullOrEmpty(resultItem.CrossSiteUrl))
                                        {
                                            if (!string.IsNullOrEmpty(root.Attributes["url"].Value))
                                            {
                                                path = root.Attributes["url"].Value;
                                            }
                                            if (!string.IsNullOrEmpty(root.Attributes["target"].Value))
                                            {
                                                target = root.Attributes["target"].Value;
                                            }
                                        }
                                    }
                                    <td><a href="@path" target="@target">@resultItem.PageTitle</a></td>
                                    <td>@resultItem.DownloadTypeName</td>
                                    <td>@resultItem.ReleaseDate.ToShortDateString()</td>
                                    <td>@Convert.ToInt64(resultItem.FileSize).ConvertToSize()</td>
                                </tr>
                                <tr>
                                    <td colspan="4">@resultItem.ShortDescription</td>
                                </tr>
                            }