Sitecore and ASP.Net - my expirence

- by Christian Kay Linkhusen

NAVIGATION - SEARCH

Update Items without changing theirs statistics

I have a small tip to you, when you have to update items in your Sitecore solution from a batch job or a script correcting data on several items in the solution. It is possible to accomplice this without changing the fields "Updated by" and "Updated", there by your script will not overwrite the original data about, who updated the item and when.
 
This is simply done by setting the updateStatistics flag to false when editing the Item.

    using (new SecurityDisabler())
    {
        var items = Sitecore.Context.Item.Children;
        foreach (var scItem in items)
        {
            using (new EditContext(scItem, false, false))
            {
                //update whatever data on your childitem you have to correct
                //without changing the statistics fields
            }
        }
    }

Thats all folks - hapii coding ;)


Receiving the same reminder twice from Sitecore

When using the reminder function in Sitecore the editor will receive the same reminder for every database the Item exists. In a normal setup with a master and a web database this will cause the editor to get the same reminder twice.

If you only want the editors to receive one email (eg. for the master database) it is possible to disable the sending of reminders for other databases by overriding the Sitecore.Tasks.TaskDatabaseAgent in Sitecore.

After reflecting the TaskDatabaseAgent I came up with this implementation, which only execute a EmailReminderTask if it's from the Master database. If it's from another database the task is marked as done, but without executing the Task. 


public class CustomTaskDatabaseAgent : TaskDatabaseAgent
    {
        public new void Run()
        {
            Task[] pendingTasks = Globals.TaskDatabase.GetPendingTasks();
            this.LogInfo("Processing tasks (count: " + (object)pendingTasks.Length + ")");
            foreach (Task task in pendingTasks)
            {
                try
                {
                    task.LogActivity = this.LogActivity;
                    //Test if the Task is the EmailReminderTask and the current database is not master database
                    if (task.GetType() == typeof (EmailReminderTask) && !task.DatabaseName.ToLower().Equals("master"))
                    {
                        //If its not the master database
                        //Only mark the Task as done, dont execute the remider
                        Globals.TaskDatabase.MarkDone(task);
                    }
                    else
                    {
                        task.Execute();    
                    }
                    TaskCounters.TasksExecuted.Increment();
                }
                catch (Exception ex)
                {
                    Log.Error("Exception in task", ex, task.GetType());
                }
            }
        }

        private void LogInfo(string message)
        {
            if (!this.LogActivity)
                return;
            Log.Info(message, (object)this);
        }
    }


The custom DatabaseAgent is enabled by changing the Sitecore.Tasks.DatabaseAgent in the web.config file.


//Default TaskDatabaseAgent
<agent type="Sitecore.Tasks.TaskDatabaseAgent" method="Run" interval="00:10:00"/>

//Custom TaskDatabaseAgent
<agent type="Netmester.SitecoreExtensions.CustomTaskDatabaseAgent" method="Run" interval="00:10:00"/>
	

Now the editor will only receive one email pr. reminder, which is the one from the Master database.

Problems with Reminders not being sent in Sitecore CM/CD setup

If you set up the reminder function in Sitecore, and you have a Multi server setup with Content Manager and Content Delivery instances, you may run into problems with reminders not being sent.

The problem is that the TaskDatabaseAgent are running on both the CM- and the CD instance in your Sitecore setup, and if the Master database is disabled on the CD instance, the sending of the reminder will fail. In the Sitecore logfiles on the CD instance you will find log entries like this one:


ManagedPoolThread #13 14:34:25 ERROR Exception in task
Exception: System.InvalidOperationException
Message: Could not find configuration node: databases/database[@id='master']
Source: Sitecore.Kernel
   at Sitecore.Diagnostics.Assert.IsTrue(Boolean condition, String message)
   at Sitecore.Configuration.Factory.GetConfigNode(String xpath, Boolean assert)
   at Sitecore.Configuration.Factory.CreateObject(String configPath, String[] parameters, Boolean assert)
   at Sitecore.Configuration.Factory.GetDatabase(String name, Boolean assert)
   at Sitecore.Tasks.EmailReminderTask.SendReminder()
   at Sitecore.Tasks.TaskDatabaseAgent.Run()

ManagedPoolThread #13 14:34:25 INFO  Executing email reminder task
ManagedPoolThread #13 14:34:25 INFO  Parameters: <r><to>xyz@domain.net</to><txt>Påmindelse kl 13:41 - ckl</txt></r>
ManagedPoolThread #13 14:34:25 ERROR Exception in task
Exception: System.InvalidOperationException
Message: Could not find configuration node: databases/database[@id='master']
Source: Sitecore.Kernel
   at Sitecore.Diagnostics.Assert.IsTrue(Boolean condition, String message)
   at Sitecore.Configuration.Factory.GetConfigNode(String xpath, Boolean assert)
   at Sitecore.Configuration.Factory.CreateObject(String configPath, String[] parameters, Boolean assert)
   at Sitecore.Configuration.Factory.GetDatabase(String name, Boolean assert)
   at Sitecore.Tasks.EmailReminderTask.SendReminder()
   at Sitecore.Tasks.TaskDatabaseAgent.Run()

ManagedPoolThread #13 14:34:25 INFO  Job ended: Sitecore.Tasks.TaskDatabaseAgent (units processed: )

The solution to the problem is to disable the TaskDatabaseAgent on the CD instance. This is done by setting the interval to 00:00:00 on the TaskDatabaseAgent in the web.config file.

	
<agent type="Sitecore.Tasks.TaskDatabaseAgent" method="Run" interval="00:00:00"/>


Once you have solved the problem, you might receive a reminder mail from each database for every item where a reminder was created in Sitecore. How to solve this problem you can read about in this  blog: Receiving the same reminder twice :)

Overriding of the Sitecore.Context.Item

In a solution I'm working on, we have a newslist presentation where News are created in Sitecore as Items in different parts of the content tree, and they are all shown on the same list. When you follow a link on the list, the Newsitem is shown in the context of the list and not from the address reflecting where the Item is created in Sitecore.

Address of NewsItems that are presented in the context of the newslist would be on the form:
www.host.net/list/item?id={guid}

 
When you implement your webcontrols for presentation of the Item, it is not possible to reference Sitecore.Context.Item because the context will be the containeritem used as an "Presentation container Item" and not the newsitem you want to show. A solution could be to create a local variable "currentItem" where you get the item you want to render by caling something like Sitecore.Context.Database.GetItem(Request.QueryString["id"]); in the Page_Load() or similar. But this solution, will not work if you have Items with different layouts, eg. if you are using Web Forms For Marketiers and have created a webform for one of the Items you are trying to render in the context of the list.

A solution for the problem could be to override the Context Item by creating a httpRequestBegin processor and execute right after Sitecores Itemresolver.
More...







Single Sign On to the Sitecore desktop

When you use Sitecore Active Directory module it is possible to set up Single Sign On by protecting the file LDAPLogin.aspx with Windows Authentication. Then it is possible to login to Sitecore without entering username and password, when you enter the following URL: http://[yoursite]/sitecore/admin/LDAPLogin.aspx.
This will login the user to the Content Editor or the StartURL entered on the users User Profile in Sitecore.

But what if you want to make it possible for the users to decide for them self, if they would like to login to the Desktop or the Content Editor?

Syntax highlighting with highlight.js

Highlight.js supplies a javascript library for syntax highlighting your code snippets on the web. The solution is hosted and is setup with inserting 3 lines of code. 

Include the Javascript, stylesheet and initialize the highlighting, by inserting the following 3 lines to the head section of your page:

<link rel="stylesheet" href="http://yandex.st/highlightjs/8.0/styles/vs.min.css"> <script src="http://yandex.st/highlightjs/8.0/highlight.min.js"></script> <script type="text/javascript">hljs.initHighlightingOnLoad();</script>

All you have to do now, is to mark up your code snippet in the content with <pre><code>[content]</code</pre> and you are up and running.