Skip to content

Write to Web Forms Programmatically

Recently I had a requirement to capture a number of parameters from a users session and provide Sitecore administrators a way of reporting on this data and exporting the data to CSV for analysis.

The reporting within WFFM was exactly what I was looking to achieve. I wondered if I could tap into this programmatically and use WFFM as my analysis and exporting tool.

Form Reports

Form Reports

The idea is to create a new Form within WFFM and instead of adding the form to the page, instead create an instance of the form in memory, complete form and post the data into the WFFM database.

Below is a code sample (commented) of completing the Form and posting the data back programmatically.

public void SubmitToWFFM(ID formID, string username, string anotherString)
{
//Get form item by Item ID
Sitecore.Forms.Core.Data.FormItem formItem = Sitecore.Forms.Core.Data.FormItem.GetForm(formID);

//Get form fields of the WFFM
FieldItem[] formFields = formItem.Fields;

//Get all form fields of the form by name
FieldItem usernameField = formFields .FirstOrDefault(x => x.FieldDisplayName == "Username");

FieldItem anotherField = loggingFields.FirstOrDefault(x => x.FieldDisplayName == "AnotherField");

//Populate fields with values
ControlResult controlUsernameTerm = new ControlResult(usernameField.Name, username, null);
ControlResult controlAnotherField = new ControlResult(anotherField.Name, anotherString, null);

//Create collection of fields
AdaptedControlResult[] acr = new AdaptedControlResult[2];
acr[0] = new AdaptedControlResult(controlUsernameTerm, false);
acr[1] = new AdaptedControlResult(controlAnotherField, true);

AdaptedResultList arl = new AdaptedResultList(acr);

//Save form
SaveToDatabase dbSave = new SaveToDatabase();
try
{</pre>
<pre>dbSave.Execute(formId, arl, null);</pre>
<pre>}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error(ex.Message, ex, this);
}
}

Sitecore SPEAK Dialog Within Desktop

Here is a short useful tutorial for anyone wanting to add a SPEAK Dialog control within the Sitecore desktop view. Basically it uses a Sitecore sheer ui application as a proxy to point at the url of the SPEAK application.

SPEAK Dialog

SPEAK Dialog

  • Create a Sitecore dialog application using Sitecore rocks within the core database.

    SPEAK Dialog Application

    SPEAK Dialog Application

  • Create a standard application item under content / applications in the core database.Set the application field to an link pointing at the url at the SPEAK application.Example Raw Value: <link url=”/sitecore/client/MikeRobbins/Dialogs/RolePermissions” linktype=”internal” id=”” />

    Application Item

    Application Item

  • Create an application shortcut pointing at the application within the applications folder.<link linktype=”internal” url=”/applications/RolePermissions” querystring=”” target=”” id=”{5DD732CF-67CE-4EF3-9F76-D53E16225693}” bdgtk

    Application Shortcut

    Application Shortcut

An example of this method can be seen in my bulk user permissions module on GitHub https://github.com/sobek1985/SitecoreSPEAKBulkRolePermissions

The next stage is to use this method to create a command button in the content editor and pass in the context item using query string parameters.

Solr Search On Exact Phrase

Very brief code sample of how to perform a exact phrase match query in Solr.

Using the Solr Admin’s Query tool to perform a search on a field, you will notice that Solr splits each word into a token to search on.

Solr Phrase Match 1

Solr Admin Query

 

Most of the time this is the desired functionality, however there could be times where you want to return results where the exact phrase is matched. If you perform a search wrapping quotes around the phrase within the Solr Admin Query tool, Solr will perform a query looking for an exact match on the phrase (see screenshot below, notice the reduced number of results).

 

Solr Phrase Match 2

Solr Admin Phase Search With Quotes

 

Below is a code sample of how to perform a search using Sitecore 7’s Content Search API’s. The code checks whether the keywords searched upon start and end with quotes. If the quotes are present, the keywords are then passed into the search intact with the quotes present. If the quotes aren’t present the keywords are split into an array on a space and the search is performed as normal.

private Expression<Func<SearchItem, bool>> GetKeywordFilters(string keywords)
 {
 var predicate = PredicateBuilder.True<SearchItem>();

 var keywordCollection = new List<string>();

 if (keywords.StartsWith("\"") && keywords.EndsWith("\""))
 {
 keywordCollection.Add(keywords);
 }
 else
 {
 keywordCollection = keywords.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
 }

 var filters = keywordCollection.Aggregate(predicate, (current, keyword) =>
current.Or(i => (i.Body.Equals(keyword)
 || i.Title.Equals(keyword)
);

 return filters;
 }
public SearchResult<SearchItem> Search(string keywords)
 {

 using (var context = Sitecore.ContentSearch.ContentSearchManager.GetIndex(_searchIndex).CreateSearchContext())
 {
 var queryable = context.GetQueryable<SearchItem>();

 Expression<Func<SearchItem, bool>> filters;

 filters = GetKeywordFilters(keywords);
 queryable = queryable.Where(filters);

 var results = queryable.GetResults();
 }

Text Based Content Search Based On Tagging

Out of the box Sitecore offers Content Editors the power to create tags, and assign them to content items using the Tagging field in standard values.

Tagging Standard Value

Tagging Standard Value

When analysing the indexes using LUKE, you can see that Sitecore indexes the ID’s of each tag. This makes it difficult to perform a text based content search that also matches on tags.

Luke Index View

Luke Index View

However Sitecore gives us the power to create computed fields in search indexes. This means we can create new fields within the index and programmatically create text into the index.

This example below uses a computed field that instead of indexing the ID of the tag, instead adds the item name of each tag seperated by a space. This then allows you to perform a text based search against the tag. (Sitecore by default indexes the __Semantics field, so here i’m creating a new field in the index called “tags” not to override any standard Sitecore functionality)

 

Computed Field Class

namespace MikeRobbins.CMS.ComputedFields
{
public class Tags : IComputedIndexField
{
public object ComputeFieldValue(Sitecore.ContentSearch.IIndexable indexable)
{
var tags = new StringBuilder();

Item obj = (Item)(indexable as SitecoreIndexableItem);

if (obj != null)
{
var tagField = (Sitecore.Data.Fields.MultilistField)obj.Fields["__Semantics"];

if (tagField != null)
{
var items = tagField.GetItems();

foreach (var item in items)
{
tags.Append(item.Name + " ");
}
}
}

return tags.Length != 0? tags.ToString():null;
}

public string FieldName { get; set; }
public string ReturnType { get; set; }
}
}

XML Config

Add this XML to the “AddComputedIndexField” section of your search index config.

<field fieldName="tags"                           >MikeRobbins.CMS.ComputedFields.Tags,MikeRobbins.CMS</field>

Sitecore Item Web API XML Maintaining Sitecore SPEAK

Recently I have had a client request asking to use Sitecore Item Web API in XML format rather than JSON. Luckily within the Item Web API documentation Sitecore provide a Pipeline Processor to output XML rather than JSON.  http://sdn.sitecore.net/upload/sdn5/modules/sitecore%20item%20web%20api/sitecore_item_web_api_developer_guide_sc66-71-a4.pdf

Sitecore SPEAK also makes use of the Sitecore Item Web API, but an unfortunate side effect of this code snippet is that it breaks Sitecore SPEAK as it expects JSON responses back from the web service. The code snippet below is an adapted version of the Sitecore code snippet. It allows bypassing the XMLSerializer by default unless a query string parameter of “type=xml” is present.

This allows you to maintain Sitecore SPEAK functionality while allowing the calling client to easily switch between JSON and XML. Add this XML to the include folder in app_config

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
 <pipelines>
 <itemWebApiRequest>
 <processor patch:before="*[@type='Sitecore.ItemWebApi.Pipelines.Request.SerializeResponse, Sitecore.ItemWebApi']" type="MikeRobbins.CMS.WebItemAPI.SwitchToXmlSerializer,MikeRobbins.CMS" />
 </itemWebApiRequest>
 </pipelines>
 </sitecore>
</configuration>
namespace MikeRobbins.CMS.WebItemAPI
{
 public class SwitchToXmlSerializer : RequestProcessor
 {
 public override void Process([NotNull] RequestArgs arguments)
 {
 if (System.Web.HttpContext.Current.Request.QueryString["type"] == "xml")
 {
 Context.Current.Serializer = new XmlSerializer();
 }
 }
 }
}
namespace MikeRobbins.CMS.WebItemAPI
{
 public class XmlSerializer : ISerializer
 {
 public string SerializedDataMediaType
 {
 get
 {
 return "text/xml";
 }
 }

 public string Serialize(object value)
 {
 return Serialize((Dynamic)value).ToString();
 }

 private XElement Serialize(Dynamic value)
 {
 var element = XElement.Parse("<object/>");

 foreach (var property in value)
 {
 var propertyName = string.Format("_{0}", property.Key);
 propertyName = propertyName.Replace("{", "");
 propertyName = propertyName.Replace("}", "");
 propertyName = propertyName.Replace(" ", "-");

 var child = XElement.Parse(string.Format("<{0}/>", propertyName));
 var array = property.Value as Dynamic[];

 if (array != null)
 {
 foreach (var item in array)
 {
 child.Add(Serialize(item));
 }
 }
 else if (property.Value is Dynamic)
 {
 child.Add(Serialize((Dynamic)property.Value));
 }
 else
 {
 child.Add(property.Value.ToString());
 }

 element.Add(child);
 }

 return element;
 }
 }
}

Workflow RSS Feed Custom Email Action

I have worked with a number of clients that have asked why the email notifications they receive from workflow didn’t reflect what is available from the RSS feed within the workbox.

This seems to have been a goal of a number of developers in the past, but nothing concrete has been created to replicate the feature. There is a awesome workaround from Nick Wesselman using Rss2email. http://www.techphoria414.com/Blog/2012/April/Sitecore_Workflow_Emails_with_rss2email

As part of a recent hacking session I decided to see if I could replicate the RSS feed within a more advance email action.

Solution
The solution is available as a Sitecore package on the marketplace or the source is available of GitHub at the bottom of this blog. Please feel free to extend and contribute.

Workflow history

Using the GetWorkflow().GetHistory(Item) API gives full access to the entire workflow history or a particular Sitecore Item.

   var workflowItemHistory = Item.State.GetWorkflow().GetHistory(Item);

However on testing this code threw up an unexpected error. Due to the email action being called from a command the workflow command hasn’t been fully executed, so the workflow history doesn’t contain the currently executing workflow command. This was solved by creating a function that gets the current workflow command that has fired, working out the target state of the action before appending this data to a collection of workflow history.

        private ItemWorkflowHistory GetCurrentWorkflowHistoryItem()
        {

            var history = new ItemWorkflowHistory()
            {
                ItemDateTime = DateTime.Now,
                User = Sitecore.Context.GetUserName(),
                PreviousState = workflowPipelineArgs.DataItem.State.GetWorkflowState().DisplayName,
                CurrentState = GetCorrectWorkflowState().DisplayName,
                Comment = workflowPipelineArgs.Comments
            };

            return history;
        }

private WorkflowState GetCorrectWorkflowState()
        {
            var emailAction = GetEmailAction();

            var command = emailAction.Parent;

            var nextStateId = command[&amp;amp;amp;quot;Next state&amp;amp;amp;quot;];

            var itemWorkflow = Sitecore.Data.Database.GetDatabase(&amp;amp;amp;quot;master&amp;amp;amp;quot;).WorkflowProvider.GetWorkflow(workflowPipelineArgs.DataItem);

            return itemWorkflow.GetState(nextStateId.ToString());
        }

Workflow commands
The biggest challenge was to build the command links to allow the user to fire the correct commands at the relevant stage of workflow.

This suffered from the same issue as the workflow history, the correct workflow commands weren’t shown because the currently executing command hasn’t completed. Again this was solved using the method above of getting the firing command and getting its target state of workflow. The correct workflow commands can then be taken from this target state of workflow.

private string GetCommandLinks(Item workflowItem, WorkflowState state)
        {
            var sb = new StringBuilder();

            var workflow = Sitecore.Data.Database.GetDatabase(&amp;amp;amp;amp;quot;master&amp;amp;amp;amp;quot;).WorkflowProvider.GetWorkflow(workflowItem);

            var commands = workflow.GetCommands(state.StateID);

            sb.Append(&amp;amp;amp;amp;quot;&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;&amp;amp;amp;amp;quot;);

            foreach (var command in commands)
            {
                var submit = Tools.GetContentEditorLink(ContentEditorMode.Submit, workflowItem, HostName, new ID(command.CommandID));
                var submitComment = Tools.GetContentEditorLink(ContentEditorMode.Submit, workflowItem,HostName, new ID(command.CommandID));

                sb.Append(&amp;amp;amp;amp;quot;&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;quot; + command.DisplayName + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt; or &amp;amp;amp;amp;lt;a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;quot; + command.DisplayName + &amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp;amp; comment&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;quot;);
            }

            string editLink = Tools.GetContentEditorLink(ContentEditorMode.Editor, workflowItem, HostName, ID.NewID);
            string previewLink = Tools.GetContentEditorLink(ContentEditorMode.Preview, workflowItem,HostName, ID.NewID);

            sb.Append(&amp;amp;amp;amp;quot;&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a&amp;amp;amp;amp;gt;Edit&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;quot;);
            sb.Append(&amp;amp;amp;amp;quot;&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a&amp;amp;amp;amp;gt;Preview&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;quot;);
            sb.Append(&amp;amp;amp;amp;quot;&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a /&amp;amp;amp;amp;gt;Workbox&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;quot;);

            sb.Append(&amp;amp;amp;amp;quot;&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;&amp;amp;amp;amp;quot;);

            return sb.ToString();
        }

Reviewing the command links in the Workbox RSS feed I was able to build a helper class to build the command links in the correct URL format for the links.

public static string GetContentEditorLink(ContentEditorMode contentEditorMode, Item item, string hostName, ID commandId)
        {
            switch (contentEditorMode)
            {
                case ContentEditorMode.Editor:
                    return (&amp;amp;amp;amp;quot;http://&amp;amp;amp;amp;quot; + hostName.Replace(&amp;amp;amp;amp;quot;http://&amp;amp;amp;amp;quot;, &amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;) + &amp;amp;amp;amp;quot;/sitecore/shell/Applications/Content%20editor.aspx?fo=&amp;amp;amp;amp;quot; + item.ID.ToString() + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;id=&amp;amp;amp;amp;quot; + item.ID.ToString() + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;la=&amp;amp;amp;amp;quot; + item.Language.Name + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;v=&amp;amp;amp;amp;quot; + item.Version.Number.ToString() + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;sc_bw=1&amp;amp;amp;amp;quot;);

                case ContentEditorMode.Preview:
                    return (&amp;amp;amp;amp;quot;http://&amp;amp;amp;amp;quot; + hostName.Replace(&amp;amp;amp;amp;quot;http://&amp;amp;amp;amp;quot;, &amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;) + &amp;amp;amp;amp;quot;/sitecore/shell/feeds/action.aspx?c=Preview&amp;amp;amp;amp;amp;id=&amp;amp;amp;amp;quot; + item.ID.ToString() + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;la=&amp;amp;amp;amp;quot; + item.Language.Name + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;v=&amp;amp;amp;amp;quot; + item.Version.Number.ToString());

                case ContentEditorMode.Submit:
                    return (&amp;amp;amp;amp;quot;http://&amp;amp;amp;amp;quot; + hostName.Replace(&amp;amp;amp;amp;quot;http://&amp;amp;amp;amp;quot;, &amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;) + &amp;amp;amp;amp;quot;/sitecore/shell/feeds/action.aspx?c=Workflow&amp;amp;amp;amp;amp;id=&amp;amp;amp;amp;quot; + item.ID.ToString() + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;la=&amp;amp;amp;amp;quot; + item.Language.Name + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;v=&amp;amp;amp;amp;quot; + item.Version.Number.ToString() + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;cmd=&amp;amp;amp;amp;quot; + commandId.ToString());

                case ContentEditorMode.SubmitComment:
                    return (&amp;amp;amp;amp;quot;http://&amp;amp;amp;amp;quot; + hostName.Replace(&amp;amp;amp;amp;quot;http://&amp;amp;amp;amp;quot;,&amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;) + &amp;amp;amp;amp;quot;/sitecore/shell/feeds/action.aspx?c=Workflow&amp;amp;amp;amp;amp;id=&amp;amp;amp;amp;quot; + item.ID.ToString() + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;la=&amp;amp;amp;amp;quot; + item.Language.Name + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;v=&amp;amp;amp;amp;quot; + item.Version.Number.ToString() + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;cmd=&amp;amp;amp;amp;quot; + commandId.ToString() + &amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;nc=1&amp;amp;amp;amp;quot;);
            }
            return &amp;amp;amp;amp;quot;&amp;amp;amp;amp;quot;;
        }
    }

Workflow History

20140521-194216-70936959.jpg

20140521-194216-70936793.jpg

Source

https://github.com/sobek1985/AdvancedEmailAction

Sitecore Marketplace

https://marketplace.sitecore.net/en/Modules/Sitecore_Advanced_Email_Action.aspx

Custom Tokens in Email Campaign Manager

Recently I had a requirement from a client where they wanted to use Sitecore Email Campaign Manager to send registration emails, containing registration details and other dynamic custom data. Here is how to create tokens within a Email Campaign message and replace them with variables in a manually triggered email.

  1. Add your token(s) into the body text of the campaign message in the format $key$, ensure that the key text is lower case.

    Sitecore Custom Tokens

    Sitecore Email Campaign Manager Custom Tokens

  2. Get the message using the Sitecore API, replace the variables using the CustomPersonTokens dictionary and then trigger the send pipeline. The CustomPersonToken dictionary requires the key to be in lower case and requires the surrounding $$ to be removed.
//Get the message using the Sitecore API
var mi = Sitecore.Modules.EmailCampaign.Factory.GetMessage(messageId);

//Use the custom person token dictionary to replace the key with a string variable. Remember that the token key has to be lower case and remove the $$ surrounding the key
 mi.CustomPersonTokens.Add("username", "MikeTest");

//Send the message
new AsyncSendingManager(mi).SendStandardMessage(contactFromName);

Trigger Email and Replace tokens

Here is a reusable function that allows you to send an email campaign message from code and replace custom tokens.

public void SendEmailCampaignMessage(ID messageId, string userName, Dictionary<string, string> customTokens = null)
        {
            var mi = Sitecore.Modules.EmailCampaign.Factory.GetMessage(messageId);

            if (customTokens != null)
            {
                foreach (var token in customTokens)
                {
                    mi.CustomPersonTokens.Add(token.Key, token.Value);
                }
            }

            var contactFromName = Contact.FromName(userName);

            new AsyncSendingManager(mi).SendStandardMessage(contactFromName);
        }

Publish And Send For Translate Workflow Action

Here is a useful Custom Workflow action I’ve written allowing a content editor to publish a content item to live while also sending its alternative language version to a translate workflow state. This allows a content item to be published to live in a single language without being held up waiting on a translation to be completed.

  • 1. Create a workflow command to publish the content to the published state in workflow.
  • 2. Create a new custom workflow action underneath the publish command.
  • 3. Use the code below to automatically create a new language version and set the workflow state to translation state.
  • 4. Replace the parameters in the screenshot of the custom workflow action. Replace the targetstate parameter with the ID of the target language state. Replace language with the name of the target language

Publish And Translate Action

using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Web;
using Sitecore.Workflows.Simple;

namespace MikeRobbins.SitecoreComponents.WorkFlow
{
    public class PublishAction
    {
        public void Process(WorkflowPipelineArgs args)
        {
            Item workFlowItem = args.DataItem;

            var targetLanguage = GetTargetLanguage(args.ProcessorItem.InnerItem);
            var targetVersion = Sitecore.Context.ContentDatabase.GetItem(workFlowItem.ID, targetLanguage);

            if (targetVersion.Versions.Count == 0)
            {
                targetVersion = targetVersion.Versions.AddVersion();
            }

            var targetWorkflowState = GetWorkflowState(args.ProcessorItem.InnerItem);

            SetWorkflowState(targetVersion, targetWorkflowState.ID);
        }

        private static void SetWorkflowState(Item welshVersion, ID targetWorkflowState)
        {
            welshVersion.Editing.BeginEdit();
            welshVersion.Fields[FieldIDs.WorkflowState].Value = targetWorkflowState.ToString();
            welshVersion.Editing.EndEdit();
        }

        private static Item GetWorkflowState(Item actionItem)
        {
            ID translateId = null;

            return ID.TryParse(WebUtil.ParseUrlParameters(actionItem["parameters"])["targetstate"], out translateId) ? Sitecore.Context.ContentDatabase.GetItem(translateId) : null;
        }

        public Language GetTargetLanguage(Item actionItem)
        {
            var language = WebUtil.ParseUrlParameters(actionItem["parameters"])["language"];

            Assert.IsNotNullOrEmpty(language, "Language needs to be specified as a parameter (&language=cy-gb)");

            return LanguageManager.GetLanguage(language);
        }
    }
}

Sitecore 7 Search ID Array Type Converter

This is a simple type converter that allows you to convert a collection of Sitecore IDs stored in a Lucene search index into a List.

public class ListIDConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(List<ID>))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(List<string>))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var scIds = new List<ID>();

            var ids = (List<string>) value;

            foreach (var id in ids)
            {
                ID scId;
                if (ID.TryParse(id, out scId))
                {
                    scIds.Add(scId);
                }
            }

            return scIds;
        }
    }

To use the Type converter decorate your public property with the TypeConverter in your Search Result class.

    [TypeConverter(typeof(ListIDConverter))]
        [IndexField("_templates")]
        public List<ID> Templates { get; set; }

Sitecore 7 Search by base templates

Quick tip, when using Sitecore 7’s search API to build a general site search, you may want to return only pages rather than any other items within Sitecore. All my page items inherit from a base Item to implement page title, meta data and so on. Using linq we are able to check whether an item is based on a specific base template, to do this we need to tell Sitecore to index the base templates.

By default in Sitecore 7 indexing base templates is disabled to improve the indexing speed. This is very simple to switch on.

Inside the section within your index uncomment the line below.

 <field fieldName="_templates" storageType="yes" indexType="untokenized">Sitecore.ContentSearch.ComputedFields.AllTemplates, Sitecore.ContentSearch</field>

Rebuild your index and these base templates are now accessible in your search result class.

Follow

Get every new post delivered to your Inbox.

Join 180 other followers

%d bloggers like this: