Using AutoMapper

Challenge:

We defined the Web-Service and were using it in an application. Web-Service returns an object or list of an object.

 

There was already code written to do the processing once we get the response object from web-service.But when we get the object via web-service, it append the Web Service Reference Name because of which we were not able to utilize the already written code for processing that object. We believe in Re-usability as much as possible and wanted to re-utilize that code which was also used by other functions. Web-Service was returning lot of data which includes list and list of object inside the list etc.

Share

Read More

Working with lac users in Sitecore

Challenge:

Recently we were working on a Project which includes lac of users. Our application includes Login/Logout Mechanism – which is the base for any Ecommerce Application. Yes it is an Ecommerce Application.

 

Initially while developing a Project we were having few records around 2000. So Sitecore was performing pretty well. But as we went Live with 300000 records. Sitecore went down. It took roughly 4+ Minutes to GetAllUsers. And 5 Minutes to process all the records, compare Custom Property which was needed in our case. So it was taking around 10 Minutes to Login. It was very critical time for us to get the things done as soon as possible. With the help of Sitecore – We came to know that only best approach in this case is to go with SQL Query. And Yes – Stored Procedure did our Job. We created a Stored Procedure that does the processing which we were doing via Code. So that reduced time to less than 6 Seconds – Hurray! (Refer: https://sitecorebasics.wordpress.com/2015/11/21/sitecore-login-time-from-10-minutes-to-seconds/ for more details on this.)
Sitecore internally uses the Membership Object of Asp.Net.

(more…)

Share

Read More

My First Sitecore Module – Google Product Feed

Hello Folks,I have been working on Google Product Feed in one of the Project.

What is Google Product Feed?

It is a feed in XML Format which we can submit to google for listing the products in Google Shopping Tab.

A google product feed xml file has list of product which use groupings of attributes that define each product in a unique way. Attributes, such as ‘condition’ and ‘availability’, can have standardized values, or accepted “answers,” or attributes can be open to fill with your own choice of value, such as ‘id’ or ‘title’. Accurately describing items using these attributes allows users to search and find your items more easily.
(more…)

Share

Read More

How to encode Trademark symbols ™ in C#

Hello Folks, Sorry was away for a while. As was very busy with Project.Challenge:
We were facing one strange issue with encoding Trademark symbol in C#. If you are also facing such similar issue then this post is for you.It was strange for us see that ™ isn’t encoding with HttpUtility.HttpEncode. As google is my best friend [so as yours], I tried to get the answer.

Before we dig into the solution, I tried on following websites which help us to get the result live for the Http.HtmlEncode()

http://htmlencode.org/
http://www.opinionatedgeek.com/dotnet/tools/htmlencode/encode.aspx

And we see that HTML Encode isn’t encoding ™.
(more…)

Share

Read More

How to escape line breaks while creating CSV or TSV file using C#

Hello C# friends.

Are you creating a CSV (Comma Separated Value) or TSV (Tab Separated Value) file in CSharp and you don’t know why your CSV file is breaking when you open it into Excel, then this post is for you.

Mr. Brian has written a very nice article on how to generate CSV file which we referred in our project.
http://www.csharptocsharp.com/generate-csv-from-generic-list

It was difficult to identify the problem. Values in object were html based and we were doing Html.Encode, so i thought that we need to do Html.Decode, but was unlucky.
To get better understanding of Html.Decode and Html.Encode refer: http://www.dotnetperls.com/httputility
Then thought that it can be  Encoding issue so tried to pass different encoding formats to StreamWriter but was unlucky in that too. I tried to identify in depth and reached to the problem and then solution.

Problem:
As my values were in Html format it were containing “t” or “n” or “r” which were creating an issue when writing a file using StreamWriter. As it get “t” or “n” or “r” it was breaking the file.

(more…)

Share

Read More

Migrate Visual Studio Solution from SVN to TFS

I love to work on Microsoft Technologies and while working with them recently we
got a challenge to migrate our project from SVN to TFS. So I would be
discussing about:
1) How to migrate visual studio solution from SVN to TFS.
2) Different approaches for migration
3) Challenges faced during migrating our solution.
As it is our basic approach to first always search on google and do some analysis
and research before doing any such task, similar way we did and found two
approaches – Manual and Automated.
SVN2TFS.EXE – (Automated) we didn’t dig much also read few
comments by which we got unhappy.

(more…)

Share

Read More

Sitecore Smart HTML Cache Clearance

Hello
Folks, I hope you are all doing well.

Smart
HTML Cache clearance – As the name suggest it clears HTML Cache for a site in a
smarter way.

In
Sitecore Content tree if you are building a site for two or more different
countries/regions and as you publish any content in any of the site than you can observe via Cache.aspx that HTML
Cache for each site gets cleared. Which should not happen.

So in
order to achieve this goal, I did analysis and found that we have to override
the default implementation of cache clearance when we publish any item in
content tree. But what if we publish any template or media item or anything
other than content, than at that time whole html cache should get clear. Also
when we publish the whole site at that time html cache for all the sites should
get clear. Smart HTML Cache Clearance will handle all the scenarios efficiently
and accurately when you publish any item on local or on remote site. Also when
it needs to clear the entire HTML Cache for all the sites than the default
implementation as written by Sitecore will handle our job.

So
whenever we face such challenges we google it, so similarly I did and found
blog of Mark Stiles(

https://markstiles.net/Blog/2011/05/26/partial-html-cache-clearing.aspx

), which helped me to achieve
objective but that was for remote publish, what if Sitecore is enabled on
production server? Or what if we want to test it in local? So for that I tried
to modify the code and got the solution.




Step 1: Create a class called SmartHtmlCacheClearer in your library.

public class SmartHtmlCacheClearer
    {
        #region Fields
        private readonly ArrayList _sites = new ArrayList();
        #endregion
        #region Properties
        public ArrayList Sites
        {
            get
            {
                return this._sites;
            }
        }
        #endregion
        #region Methods
                
        /// This method is called when publish:end or publish:end:remote events are triggered.
        public void ClearCache(object sender, EventArgs args)
        {
            Assert.ArgumentNotNull(sender, "sender");
            Assert.ArgumentNotNull(args, "args");
            try
            {
                Database database = null;
                Guid emptyGuid = Guid.Empty;
                //This will run on the local targets
                if (args.GetType().ToString().Equals("Sitecore.Events.SitecoreEventArgs",StringComparison.InvariantCultureIgnoreCase))
                {
                    SitecoreEventArgs eventArgs = (SitecoreEventArgs)args;
                    if (eventArgs.Parameters != null)
                    {
                        var publishOptions = ((Sitecore.Publishing.Publisher)(eventArgs.Parameters[0])).Options;
                        database = Factory.GetDatabase(publishOptions.TargetDatabase.Name);
                        ClearHtmlCache(database, publishOptions.RootItem != null ? publishOptions.RootItem.ID.ToGuid() : emptyGuid);
                    }
                }
                //THIS WILL RUN ON THE REMOTE TARGETS
                if (args.GetType().ToString().Equals("Sitecore.Data.Events.PublishEndRemoteEventArgs", StringComparison.InvariantCultureIgnoreCase))
                {
                    PublishEndRemoteEventArgs remoteEventArgs = (PublishEndRemoteEventArgs)args;
                    database = Sitecore.Configuration.Factory.GetDatabase(remoteEventArgs.TargetDatabaseName);
                    ClearHtmlCache(database, remoteEventArgs.RootItemId != null ? remoteEventArgs.RootItemId : emptyGuid);
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error while Clearing HTML Cache : " + ex.Message, this);
            }
        }

        /// This method will verify that whether to clear the HTML cache for any particular site or for all sites and it will also clear the HTML Cache for particular site if verfication is for particular site.
        private void ClearHtmlCache(Database database, Guid ItemGuid)
        {
            if (database != null)
            {
                Item currentItem = database.GetItem(new ID(ItemGuid));
                if (currentItem != null)
                {
                    Item startItem = currentItem.HomeItem();
                    //If startItem is not Site Root item then we will clear full HTML caches
                    if (startItem != null && startItem.TemplateID.ToString().Equals(TemplateIdHelper.TemplateIds.SiteRoot, StringComparison.InvariantCultureIgnoreCase))
                    {
                        bool isSiteHTMLCacheCleared = false;
                        var siteInfo = currentItem.GetSiteInfo();
                        SiteContext siteContext = Factory.GetSite(siteInfo.Name);
                        Log.Info("SmartHtmlCacheClearer started clearing HTML cache of " + siteInfo.Name + " site.", this);
                        if (siteContext != null)
                        {
                            HtmlCache htmlCache = CacheManager.GetHtmlCache(siteContext);
                            if (htmlCache != null)
                            {
                                htmlCache.Clear(true);
                                isSiteHTMLCacheCleared = true;
                            }
                        }
                        if (isSiteHTMLCacheCleared)
                            Log.Info("SmartHtmlCacheClearer successfully cleared HTML cache of " + siteInfo.Name + " site.", this);
                        else
                            Log.Info("SmartHtmlCacheClearer failed while clearing HTML cache of " + siteInfo.Name + " site.", this);
                    }
                    else
                    {
                        //If startItem is anything other than Content Item (e.g. Template Item or Media Library Item) than clear HTML Cache for all sites.       
                        ClearFullCache();
                    }
                }
                else
                {
                    //If currentItem is null. It means user selected Publish Site option.
                    //Clear Full HTML Cache
                    ClearFullCache();
                }
            }
            else
            {
                Log.Warn("SmartHtmlCacheClearer Failed as Target Database is null.", this);
            }
        }

        /// This method will clear the Full HTML Cache
        private void ClearFullCache()
        {
            Log.Info("SmartHtmlCacheClearer clearing HTML caches for all sites (" + this._sites.Count + ").", this);
            for (int iIndex = 0; iIndex < this._sites.Count; iIndex++)
            {
                string siteName = this._sites[iIndex] as string;
                if (siteName != null)
                {
                    SiteContext site = Factory.GetSite(siteName);
                    if (site != null)
                    {
                        HtmlCache htmlCache = CacheManager.GetHtmlCache(site);
                        if (htmlCache != null)
                        {
                            htmlCache.Clear();
                        }
                    }
                }
            }
            Log.Info("SmartHtmlCacheClearer done.", this);
        }
        #endregion
    }

Step 2: Replace publish:end and publish:end:remote handler to refer the SmartHtmlCacheClearer class in web.config

Now we have
to add the reference of this class to the events section in web.config file and
allow Sitecore to execute this code instead of Sitecore default code.

We just have
to modify the handler for publish:end and publish:end:remote.

 <handler method="ClearCache" type="ABC.WCMS.Extensions.Pipelines.PublishItem.SmartHtmlCacheClearer, ABC.WCMS.Extension"></handler>  
 <handler method="ClearCache" type="ABC.WCMS.Extension.Pipelines.PublishItem.SmartHtmlCacheClearer, ABC.WCMS.Extension"></handler>  

That’s it.

Note:
Please specify the exact method name in handler which should be executed.

Happy
Sitecoring.! 
J

Share

Read More