Tuesday, 27 December 2016

Sitecore - Enable preview mode for Anonymous user


Recently we had a requirement to enable preview mode for anonymous user so that content authors can share the preview url to others for review. Since it was just for review purpose we wanted them to see the preview of the page without logging into sitecore. Of course there is another way to just point CM server database to master but we did not want to change our architecture for this. We contacted Sitecore support(support id 471776) and they helped us in achieving this. Please note the fix is for Sitecore 8.0 version but should work with other version as well.

Configuration- you need to patch your configuration in such a way so that your class will come as below in showconfig page.



<httpRequestBegin>
… … …
<processor type="Sitecore.Pipelines.HttpRequest.UserResolver, Sitecore.Kernel"/>
<processor type="[Your namespace].AllowAnonymousPreview, YourAssemblyName"/>
<processor type="Sitecore.Pipelines.HttpRequest.DatabaseResolver, Sitecore.Kernel"/>
</httpRequestBegin>


Class code :- 

namespace [Namespace]
{
    public class AllowAnonymousPreview : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            var activeUser = AuthenticationManager.GetActiveUser();
            Assert.IsNotNull(activeUser, "User cannot be null.");
            var userIsExtranetAnonymous = activeUser.Name == "extranet\\Anonymous";
            var userIsSitecoreAnonymous = activeUser.Name == "sitecore\\Anonymous";
            if (!userIsExtranetAnonymous && !userIsSitecoreAnonymous)
            {
                return;
            }
            var isRibbonRequest = args.Url != null && args.Url.ItemPath == "/sitecore/content/home/applications/webedit/webeditribbon";
            if (Sitecore.Context.PageMode.IsPreview || isRibbonRequest)
            {
                PerformAutoLogin();
            }
         
        }

        private void PerformAutoLogin()
        {
            string userName = "extranet\\Preview Anonymous User";
            AuthenticationManager.Login(userName);
            string ticket = Sitecore.Web.Authentication.TicketManager.CreateTicket(userName, @"/sitecore/shell");
            HttpContext current = HttpContext.Current;
            if (current != null)
            {
                HttpCookie cookie = new HttpCookie(Sitecore.Web.Authentication.TicketManager.CookieName, ticket)
                {
                    HttpOnly = true
                };
                current.Response.AppendCookie(cookie);
            }
        }

    }
}

extranet\\Preview Anonymous User is a custom user which we have created for preview access and have assigned "sitecore\Sitecore Minimal Page Editor" role to him. We have revoked write access for this user and tested and confirmed that user should not be able to go back to edit mode and edit the page with this access.


Happy Coding :)

Wednesday, 19 October 2016

Sitecore - unable to update item display name


Recently one of my friend was unable to update display name of an item. It was working fine for other items though. We checked  item:saving event handler but could not find anything which could have caused this issue. Then i found this stackoverflow link and tried checking directly in database

http://stackoverflow.com/questions/29850067/unable-to-change-display-name-of-item-why

I ran below sql query  where fieldid is of "__Display Name" field  and itemid is the item which was giving trouble.

SELECT *
FROM [dbo].[UnversionedFields]
WHERE ItemId = '{39F976FA-0ADC-4CE3-B600-449D33FE30FA}'
  AND FieldId = '{B5E02AD9-D56F-4C41-A065-A133DB87BDEB}'

It gave me two results ,


everytime i was updating my display name it was updating the row which is having "en" language in column. but content tree was always showing the value from the row which has language value as blank.

i checked if i have anyother blank language values . but could not find anything.

SELECT *
FROM [dbo].[UnversionedFields]
WHERE Language=''



  SELECT *
FROM [dbo].[VersionedFields]
WHERE Language=''

we deleted the item and reinstalled that item package from another server and it started working working fine.

if you have lot of items you can try running below sql query after taking your database backup. It should fix the issue

delete
FROM [dbo].[UnversionedFields]
WHERE Language=''



Monday, 10 October 2016

Error loading hook: hook type="Sitecore.ContentSearch.Hooks.Initializer - Sitecore 8.1 CD setup

Recently while setting up our cd server we got below error

ERROR Error loading hook: <hook type="Sitecore.ContentSearch.Hooks.Initializer, Sitecore.ContentSearch" patch:source="Sitecore.ContentSearch.config" xmlns:patch="http://www.sitecore.net/xmlconfig/" />
Exception: System.InvalidOperationException
Message: Could not find configuration node: campaignManagementSearch/indexConfigurations/campaignManagementLuceneIndexConfiguration
Source: Sitecore.Kernel

This came because We were disabling Sitecore.Marketing.Lucene.IndexConfiguration.config file as per Sitecore instructions for setting up CD server in this version. But this configuration is being referred in Sitecore.Marketing.Lucene.Index.Web.config file which was still enabled and hence throwing exception. Enable Sitecore.Marketing.Lucene.IndexConfiguration.config file to fix this error

Monday, 26 September 2016

Index Sitecore_Analytics_index was not found in Experience profile dashboard

If you are facing this issue follow below steps to fix this

- Check if analytics is enabled . you can check if "Analytics.enabled" is true in sitecore.analytics.config.

-check if Sitecore.ContentSearch.Lucene.Index.Analytics.config is enabled. 

- Follow this blog to rebuild your reporting database. It should fix the issue. 

https://blog.horizontalintegration.com/2015/01/10/rebuilding-sitecore-8-analytics-indexes/



- If you are not using a fresh analytcis database you may get below error since Sitecore may take some time to clear your storage.

"Exceeded the cut off date before clearing storage"

you can change the TimeToClearStorage setting to avoid this.


You can change  it to 20 minutes in Sitecore.Analytics.Processing.Aggregation.config file

<reportingStorageManagertype="Sitecore.Analytics.Aggregation.History.ReportingStorageManager"singleInstance="true">

  <TimeToClearStorage>0.00:01:00</TimeToClearStorage>


Wednesday, 21 September 2016

Sitecore Exception on item:saved event while publishing

Recently we found below exception while publishing . I am not sure about the root cause but it was fixed by clearing c:/windows/temp folder and doing an IISreset.

Job started: Publish to 'web'|#Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.AggregateException: One or more exceptions occurred while processing the subscribers to the 'item:saved' event.
   at Sitecore.Events.Event.EventSubscribers.RaiseEvent(String eventName, Object[] parameters, EventResult result)
   at Sitecore.Events.Event.RaiseEvent(String eventName, Object[] parameters)
   at Sitecore.Events.Event.RaiseItemSaved(Object sender, ItemSavedEventArgs args)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Sitecore.Data.Engines.EngineCommand`2.RaiseEvent[TArgs](EventHandler`1 handlers, Func`2 argsCreator)
   at Sitecore.Data.Engines.EngineCommand`2.Execute()
   at Sitecore.Data.Engines.DataEngine.SaveItem(Item item)
   at Sitecore.Data.Managers.ItemProvider.SaveItem(Item item)
   at Sitecore.Data.Managers.PipelineBasedItemProvider.ExecuteAndReturnResult[TArgs,TResult](String pipelineName, String pipelineDomain, Func`1 pipelineArgsCreator, Func`1 fallbackResult)
   at Sitecore.Data.Managers.PipelineBasedItemProvider.SaveItem(Item item)
   at Sitecore.Data.Items.ItemEditing.AcceptChanges(Boolean updateStatistics, Boolean silent)
   at Sitecore.Data.Items.EditContext.Dispose()
   at Sitecore.Publishing.PublishHelper.CopyToTarget(Item sourceVersion)
   at Sitecore.Publishing.PublishHelper.PublishVersionToTarget(Item sourceVersion, Item targetItem, Boolean targetCreated)
   at Sitecore.Publishing.Pipelines.PublishItem.PerformAction.Process(PublishItemContext context)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Publishing.Pipelines.PublishItem.PublishItemPipeline.Run(PublishItemContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context, List`1& referrers, List`1& children)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessEntries(IEnumerable`1 entries, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessEntries(IEnumerable`1 entries, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessEntries(IEnumerable`1 entries, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessEntries(IEnumerable`1 entries, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessEntries(IEnumerable`1 entries, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessEntries(IEnumerable`1 entries, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessPublishingCandidate(PublishingCandidate entry, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessEntries(IEnumerable`1 entries, PublishContext context)
   at Sitecore.Publishing.Pipelines.Publish.ProcessQueue.Process(PublishContext context)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Publishing.Pipelines.Publish.PublishPipeline.Run(PublishContext context)
   at Sitecore.Publishing.Publisher.PublishWithResult()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Sitecore.Reflection.ReflectionUtil.InvokeMethod(MethodInfo method, Object[] parameters, Object obj)
   at Sitecore.Jobs.JobRunner.RunMethod(JobArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Jobs.Job.ThreadEntry(Object state)