Wednesday, November 19, 2014

Sitecore :- How to update build data through excel sheet, How to create template through code, How to add formating to HTML

using Sitecore.Data.Items;
using BusinessModules;
using Sitecore.Data.Fields;
using System.Text.RegularExpressions;
using System.Text;
using Sitecore.Data.Managers;
using Sitecore.Security.Accounts;
using System.Data.OleDb;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;
namespace ABC.layouts
{
   
    public partial class ScriptsOnDemand : System.Web.UI.Page
    {
        private void Import()
        {
            Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master", false);
            OleDbCommand command = null;
            string con = string.Empty;

            //connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\EpicCV.xls;Extended Properties='Excel 4.0;HDR=Yes;'";

            try
            {
                Sitecore.Data.Items.Item rootItem = master.GetItem("/sitecore/content/Global Content/People/A");

                List BiosItem = new List(rootItem.Axes.GetDescendants().Where(m => m.TemplateID.ToString() == "{EB65BFA3-5122-43F4-A1B0-02F430F645CA}"));

                StringBuilder sb = new StringBuilder();

                using (OleDbConnection connection = new OleDbConnection(con))
                {
                    connection.Open();
                    command = new OleDbCommand("select * from [Report1$]", connection);
                    sb.Append("");
                   
                    sb.Append(" ");
                    using (OleDbDataReader dr = command.ExecuteReader())
                    {
                        int counter = 0;
                        while (dr.Read())
                        {
                            counter = counter + 1;

                            string emailID = dr["BusinessEmail"].ToString().Trim();
                            string bioVersionName = bioVersionName = dr["lastName"].ToString().Trim() + " " + dr["FirstName"].ToString().Trim();
                            string mainBiographyNarrative = formatHTML(  dr["Bio Narrative"].ToString().Trim() );
                            string experienceNarrative = formatHTML( dr["Experience Narrative"].ToString().Trim());

                            List itemMainBioList = BiosItem.Where(x => x.Fields["Email"].ToString() == emailID).ToList();
                            foreach (Item itemMainBio in itemMainBioList)
                            {
                                sb.Append("
");
                                sb.Append(" ");
                                if (!String.IsNullOrEmpty(emailID))
                                {
                                    sb.Append(" ");
                                }
                                try
                                {
                                    if (itemMainBio.Fields["Email"] != null)
                                    {
                                        sb.Append(" ");

                                        sb.Append(" ");

                                        sb.Append(" ");
                                       
                                        TemplateItem template = master.GetTemplate("{8FFC3E2B-C6F5-4A80-913D-E2A54D02FC20}");
                                        User currentUser;
                                        string domainUser = @"sitecore\UserA";
                                        currentUser = Sitecore.Security.Accounts.User.FromName(domainUser, false);

                                        Item oldVersionItem = master.GetItem(itemMainBio.Paths.FullPath + "/Versions");

                                        if (oldVersionItem != null)
                                        {

                                            oldVersionItem.Add(bioVersionName, template);
                                            using (new UserSwitcher(currentUser))
                                            {
                                                Item versionItem = master.GetItem(itemMainBio.Paths.FullPath + "/Versions/" + bioVersionName);
                                                if (versionItem != null)
                                                {
                                                  

                                                    versionItem.Editing.BeginEdit();
                                                    versionItem.Fields["VersionName"].Value = dr["Title"].ToString().Trim();
                                                    versionItem.Fields["MainBiographyNarrative"].Value = mainBiographyNarrative;
                                                    versionItem.Fields["ExperienceNarrative"].Value = experienceNarrative;
                                                    versionItem.Fields["__Workflow"].Value = "{E534CB0A-B877-49EA-BBB1-927EBD7853FE}";                                                   
                                                    versionItem.Fields["__Workflow state"].Value = "{E2CF5329-65A0-40EE-8971-88BDE0F68574}";                                                   
                                                    versionItem.Fields["__Default workflow"].Value = "{E534CB0A-B877-49EA-BBB1-927EBD7853FE}";                                                   
                                                    versionItem.Editing.AcceptChanges();
                                                    versionItem.Editing.EndEdit();
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        sb.Append("
                                        sb.Append(" ");

                                        sb.Append(" ");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    string msg = ex.Message;
                                }

                                sb.Append(" ");

                            }

                        }

                    }

                    sb.Append("
Sr. No. Name Email Id on File Sitecore Email ID Version Name Match Found
" + counter + "" + dr["lastName"].ToString().Trim() + " " + dr["FirstName"].ToString().Trim() + "" + emailID + "" + itemMainBio.Fields["Email"].ToString() + "" + bioVersionName + "" + "Yes" + "    " + "No" + "
");
                    divItemList.InnerHtml = sb.ToString();
                   
                }

            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }

        }

        private string formatHTML(string inputHTML)
        {
             try
            {
                var document = new HtmlDocument();
                document.LoadHtml(inputHTML);
                StringWriter writer = new StringWriter();

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.ConformanceLevel = ConformanceLevel.Fragment;
                settings.CloseOutput = false;

                var xmlWriter = XmlWriter.Create(writer, settings);
                document.OptionOutputAsXml = true;                   
                document.Save(xmlWriter);
                 var newHtml = writer.ToString();

                 return Convert.ToString(newHtml);
            }
             catch (Exception ex)
             {
                 string msg = ex.Message;
                 return msg;
                
             }

        }}

Tuesday, November 18, 2014

Sitecore :- How to solve Microsoft.Jet.OLEDB.4.0 and Microsoft.ACE.OLEDB.12.0 problem IIS 7, IIS 8

OLE DB is a set of COM-based interfaces that expose data from a variety of sources.

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.
     

Scenario: You are a web server administrator and you got a web site or an application given by developer which is working fine on the developer machine now you have hosted the web site on the Web Server IIS 7 or IIS 8 and its working fine but a little while later a developer call to you and says he is unable to upload the excel files.

See the comprehensive Video Tutorial on YouTube to solve this problem: https://www.youtube.com/watch?v=m4BwtCjs3z8

Error 1: Access to the path 'C:\inetpub\wwwroot\OLEDB-TEST-SITE\UploadedExcel\24032013_013545.xls' is denied.

This error comes because the folder where the file is uploaded doesn't have a write permission to solve this give the IIS_IUSRS write permission on the folder where the files are uploaded. 


 Error 2:
When uploading .xls file your getting this error ( The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.



To solve this problem you have to download and install "2007 Office System Driver: Data Connectivity Components" on the web server the link is given at the bottom to this blog.  




Also you have to "Enable 32-bit Applications to True" in IIS Application Pool for the web application if you want to upload the .xls files.


See the comprehensive Video Tutorial on YouTube to solve this problem: https://www.youtube.com/watch?v=m4BwtCjs3z8

 Link to Download:-  
Microsoft Access Database Engine 2010
http://www.microsoft.com/en-us/download/details.aspx?id=13255


2007 Office System Driver: Data Connectivity Components
http://www.microsoft.com/en-us/download/details.aspx?id=23734

Also you can download the test web application to test the OLEDB provider is registered on the local machine or not.
https://docs.google.com/file/d/0B8J5QeSXwQfbVlFiX3YxZXZyS3M/edit?usp=sharing


See the comprehensive Video Tutorial on YouTube to solve this problem: https://www.youtube.com/watch?v=m4BwtCjs3z8

For more information :- 
http://www.itzoz.com/2013/03/how-to-solve-microsoftjetoledb40-and.html

Monday, November 17, 2014

Sitecore :- Step to setup cache in sitecore application

Below are the steps



1.  Go to “web.config” and set “DisableBrowserCaching” to false.
2. Increase and tune the size of the data, items, and prefetch caches. Bigger caches = better performance. We can change size of cache of whole Sitecore instance using below settings in web.config.

'
      300MB
      300MB
      5MB
      5MB
'



3. Tune the prefetch cache settings under the App_Config/Prefetch/ folder. Sample /App_Config/Prefetch/Web.Config is given below.

 


"
  300MB
 
 
 


 
  {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX }
  {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX }


 
  {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
"



4. Go to sub layout item in sitecore and identify the items which are not changed frequently, apply cache as per below screenshot.
List of necessary sub layouts are available at “Stats” page. http://invistab2cuat.edynamic.co.in:91/sitecore/admin/stats.aspx
Example Item path:  /sitecore/layout/Sublayouts/INVISTA/Apparel/Lycra B2C/Common/Footer
After doing below changes publish website and check the checkbox as per below screenshot.

For search, blog listing you need to check “Cacheable”, “Clear on Index Update”, “VaryByData”  and “VaryByParm” checkbox