Hi, It's a short tutorial with regard to search operations. Search operations is used for performing actions on collections of items. By default you can copy, move, serialize, clone, delete, publish the results. On the other hand, you can extend the list of commands with your own commands. It is available from Sitecore 7.0.
So let's add a simple command, which generates a sitecore package from the search result.
That's all! It's really easy to customize search operations.
So let's add a simple command, which generates a sitecore package from the search result.
Step 1: Create command item and register it.
Create (or duplicate an existing ) command to here: /sitecore/system/Settings/Buckets/Settings/Search Operations/Fields. Fill the "Type", "List Name", "Enable" field.
Finally, register your command in commands.config or create a new config.
Finally, register your command in commands.config or create a new config.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> | |
<sitecore> | |
<commands> | |
<command name="bucket:packager" type="Sitecore.OneClickPackager.Business.Packager,Sitecore.OneClickPackager"/> | |
</commands> | |
</sitecore> | |
</configuration> | |
Step 2: Create code base for command
Your CommandContext contains the search query and the context item. So you can perform the same search and add items to the package. Here is the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sitecore.Buckets.Commands; | |
using Sitecore.Configuration; | |
using Sitecore.ContentSearch; | |
using Sitecore.ContentSearch.Linq.Common; | |
using Sitecore.ContentSearch.SearchTypes; | |
using Sitecore.ContentSearch.Security; | |
using Sitecore.ContentSearch.Utilities; | |
using Sitecore.Data; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Install; | |
using Sitecore.Install.Framework; | |
using Sitecore.Install.Items; | |
using Sitecore.Install.Zip; | |
using Sitecore.Security.Accounts; | |
using Sitecore.Shell.Framework.Commands; | |
using Sitecore.Web.UI.Sheer; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Sitecore.OneClickPackager.Business | |
{ | |
[Serializable] | |
internal class Packager : Command, IItemBucketsCommand | |
{ | |
public override void Execute(CommandContext context) | |
{ | |
this.GeneratePackage(context); | |
} | |
public void GeneratePackage(params object[] parameters) | |
{ | |
var context = (CommandContext)parameters[0]; | |
List<SearchStringModel> searchQuery = SearchStringModel.ExtractSearchQuery(context.Parameters.GetValues("url")[0].Replace("\"", string.Empty)); | |
if (context.Items.Length <= 0) | |
return; | |
SitecoreIndexableItem sitecoreIndexableItem = (SitecoreIndexableItem)context.Items[0]; | |
if (sitecoreIndexableItem == null) | |
{ | |
Log.Error("Package Generator - Unable to cast current item - " + context.Items[0].GetType().FullName, (object)this); | |
} | |
if (sitecoreIndexableItem == null) | |
{ | |
Log.Error("Package Generator - Unable to cast current item - " + context.Items[0].GetType().FullName, (object)this); | |
} | |
else | |
{ | |
using (IProviderSearchContext searchContext = ContentSearchManager.GetIndex((IIndexable)sitecoreIndexableItem).CreateSearchContext(SearchSecurityOptions.Default)) | |
{ | |
IQueryable<SitecoreUISearchResultItem> query = LinqHelper.CreateQuery<SitecoreUISearchResultItem>(searchContext, (IEnumerable<SearchStringModel>)searchQuery, (Item)sitecoreIndexableItem, (IEnumerable<IExecutionContext>)null); | |
List<Item> list = new List<Item>(); | |
foreach (SitecoreUISearchResultItem searchResultItem in (IEnumerable<SitecoreUISearchResultItem>)query) | |
{ | |
if (searchResultItem.GetItem() != null) | |
list.Add(searchResultItem.GetItem()); | |
} | |
string fullName = Context.User.Profile.FullName; | |
using (new UserSwitcher(User.FromName("sitecore\\admin", false))) | |
{ | |
Database contentDatabase = Context.ContentDatabase; | |
PackageProject solution = new PackageProject(); | |
solution.Metadata.PackageName = context.Items[0].Name; | |
solution.Metadata.Author = fullName; | |
ExplicitItemSource explicitItemSource = new ExplicitItemSource(); | |
explicitItemSource.Name = context.Items[0].Name; | |
Item[] objArray = list.ToArray(); | |
if (objArray != null && objArray.Length > 0) | |
list.AddRange((IEnumerable<Item>)objArray); | |
foreach (Item obj in list) | |
{ | |
explicitItemSource.Entries.Add(new ItemReference(obj.Uri, false).ToString()); | |
} | |
solution.Sources.Add((ISource<PackageEntry>)explicitItemSource); | |
solution.SaveProject = true; | |
string fileName = context.Items[0].Name + "_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss_fffffff") + ".zip"; | |
string str = Settings.PackagePath; | |
using (PackageWriter packageWriter = new PackageWriter(str + fileName)) | |
{ | |
Context.SetActiveSite("shell"); | |
((BaseSink<PackageEntry>)packageWriter).Initialize(Installer.CreateInstallationContext()); | |
PackageGenerator.GeneratePackage(solution, (ISink<PackageEntry>)packageWriter); | |
Context.SetActiveSite("website"); | |
} | |
SheerResponse.Download(str + fileName); | |
} | |
} | |
} | |
} | |
} | |
} |
Comments
Post a Comment