Monday, September 14, 2015

Sitecore, ADAM, Basic API Example

My project uses Sitecore as a Front-End and ADAM as a Photo-Storage, the task is - display a list of specific records(Name, Description and Picture). Because ADAM's documentation is hard to find, I'm going to provide the code :)
First of all we need to create a Template, that will have just one TextFiled with the Record Guids that we need to display.
Now we need to get those records from ADAM:

            var app = new Application();
            if (app.LogOn(Configuration.Settings.Registration, Configuration.Settings.AdamName, Configuration.Settings.AdminPassword) == LogOnStatus.LoggedOn)
                    foreach (var assetId in AssetList.Records.Split(','))
                        var record = new Record(app);
                        record.Load(new Guid(assetId));
                        Records.Add(record.Files.Master.GetPreview().GetPath(), record.Fields["Name"]);


And just display it in a View:

    if (!Model.Records.Any())
        [No content found. Verify data service is available.]

<ul class="record-list">
        @foreach (var item in Model.Records)
          <li> <img href="@item.Key"></img> </li>
          <li> @item.Value </li>


That is it!  Looks very easy! ADAM and Sitecore now are partners and there is a plugin 'ADAM CONNECTOR' in the Sitecore Marketplace. I'm going to research this plugin and probably my next post will be about the plugin.

Thursday, June 11, 2015

Sitecore and GlassMapper - how much a GlassCast<> costs

We are using the GlassMapper on many projects, I just want to know how it affects performance.
I have about 20000 Items for testing (Articles) with about 20 fields, in first code I use the GlassCast to get Titles:

items.Select(item => item.GlassCast<Article>()).Select(art => art.Title).ToList();

In second part I will use the field directly:

items.Select(item => item[Article.TitleFieldName]).ToList();

In first case, when we use the GlassCast average time was 00:00:55.23 In second case average time was 00:00:00.034

It's almost 2000 times faster, so we should use it carefully!

Friday, June 5, 2015

Starting with Sitecore Analytics MongoDB API

I see a lot of questions about how to start using it, so this is a short post about.
For example let's get all Visits data for the last month (last 30 days) for our particular site. First of all we need to get the MongoDB collection named "Interactions" it has all Visits data:

//Connecting to the Analytics DB
var driver = Sitecore.Analytics.Data.DataAccess.MongoDb.MongoDbDriver.FromConnectionString("analytics");

//Building our query
var builder = new QueryBuilder();
var filter = builder.And(builder.GTE(_ => _.StartDateTime, DateTime.Now.AddDays(-30)), builder.EQ(_ => _.SiteName, siteName.ToLower())); 

//Retrieving data from the "Interactions" collection
var interactions = driver.Interactions.FindAs(filter)

In similar way you can get other data.

That is it! 

Friday, May 15, 2015

Resolving old legacy URL’s with Sitecore

For example you need to move an old PHP website to Sitecore and you want to serve old links that user may keep in the Favorites, so when user click on…/some_product.php it gets sitecore Item ‘Some Product’.

  1.  Add .php extension to web.config – just find  <param desc="Allowed extensions (comma separated)"> in two places and add php extension to them.
  2. Add new Field “Old Url” to the item’s template
  3. Now we need to combine the ‘Some Product’ item ID's and OldUrl’s, you can use IDTable for example or put it to the cash.
    var items = SitecoreUtils.WebDb.SelectItems("fast:/../MyItems//*[@@templatename='MyItem']");   
                var dictionary = new Dictionary();
                foreach (var item in items.Where(_ => !string.IsNullOrWhiteSpace(_.Fields["OldUrl "].Value)))
                    string path = new Uri(item.Fields["OldUrl"].Value) .AbsolutePath.ToLowerInvariant();
                    if (dictionary. EndsWith (path))
                        //log duplicate legacy_url value
                        Sitecore.Diagnostics.Log.Warn("In App_Start ConfigureUrlMapping: Found duplicate OldUrl value, skipping dictionary add. Item ID: " +item.ID +", URL: " +item.Fields["OldUrl "] +".", new object());
                        dictionary.Add(path, item.ID);
    HttpRuntime.Cache.Insert("UrlMapper", dictionary);
  4. Ok, next step just to resolve .php url
    public class OldUrlResolver : HttpRequestProcessor
            public override void Process(HttpRequestArgs args)
                if (Sitecore.Context.Item != null) return;
                string url = args.Context.Request.Url.AbsolutePath.ToLowerInvariant();
                if (url.EndsWith(".php"))
                    var items = HttpRuntime.Cache.Get("UrlMapper") as IDictionary<string, ID>;
                    if (items != null && items.ContainsKey(url))
                       Sitecore.Context.Item = items[url];

    Let’s register our custom processor in the web.config file
             <processor patch:after="*[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" type="Pipeline.OldUrlResolver,  Pipeline" />

That is it!

Sitecore Query in the MVC Rendering Datasource

I found many articles about resolving queries for the ASP.NET Webforms and nothing for the MVC. Sitecore doesn't support it OOTB, so I decide to write this small post.
First of all we need to create a new processor in the Sitecore pipeline:

using Sitecore.Data.Items;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
namespace CustomSolution.Common.Pipelines
    public class RenderingDatasourceQueryResolver : RenderRenderingProcessor
        public override void Process(RenderRenderingArgs args)
            string dataSource = args.Rendering.DataSource;
            if (!dataSource.StartsWith("query:")) return;
            Item queryItem = args.PageContext.Item.Axes.SelectSingleItem(dataSource.Substring(6));
            if (queryItem != null)
                args.Rendering.DataSource = queryItem.Paths.FullPath;

Now we needs to register the Processor:

     <processor patch:before="*[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']"  
       type="CustomSolution.Common.Pipelines.RenderingDatasourceQueryResolver, CustomSolution"/>  

Now we can use a Sitecore query, for example to get the Site:

That is it! 

Friday, April 24, 2015

Sitecore SPEAK Upload Media Dialog doesn't Upload any files

It looks like that the image had been uploaded but it never finished. First of all I've found an exception in the Sitecore:

 No component for supporting the service Sitecore.Controllers.MediaController was found

It was fixed by registering the component in the code:


But unfortunately it didn't help me (: That is mean my current Sitecore should be updated, Sitecore CMS 7.5 rev. 141003 Hotfix 431794-1 should fix it. But if we can't do any updates for some reason, we can switch to the old style Upload dialog.
Just go to  /App_Config/Include/Sitecore.Speak.Applications.config file and comment out the following line:

   <override xmlControl="Sitecore.Shell.Applications.Media.MediaBrowser" 
          with="/sitecore/client/applications/Dialogs/SelectMediaDialog" />

That is it! 

Tuesday, April 14, 2015

Injecting javascript and css to Sitecore Content Editor Page

Sometimes we need to use javascript in our custom fields, to do that we should inject them into Content Editor Page: 

    public class InjectScripts
        public void Process(PipelineArgs args)
            if (Sitecore.Context.ClientPage.IsEvent) return;

            HttpContext current = HttpContext.Current;
            if (current == null) return;

            Page page = current.Handler as Page;
            if (page == null) return;

            string[] strArray = {
            foreach (string str in strArray) page.Header.Controls.Add(new LiteralControl(string.Format("", str)));
            page.Header.Controls.Add(new LiteralControl(""));

And in config file:

<configuration xmlns:patch="">
        <processor patch:before="*[1]" type="CustomField.InjectScripts, CustomField">

Very Easy!