Pages

19 March, 2012

Wildcard Module is not working for newly created page in Sitecore – 9

Wildcard Module allows us to work with Dynamic URL and Sitecore wildcard items.

Problem: In Sitecore instance, Wildcard module was working for a page when we initially configured for the first time. When we deleted that page and recreated that page again, wildcard module was not working. And it was not working for the newly created page in Sitecore. In Code-behind, it was not able to match the tokens in the particular URL.


Solution: In Sitecore \System\Modules\Wildcards\Routes\<BlogRoute>, we need to add items to create the rule for wildcard replacement. In that, we will be adding the pages to which wildcard to apply.
When we add pages to Data Items, wildcard module will replace with tokens only to pages which match this Items Collection (GUIDs). So in order to use this wildcard route in other page or newly created page, we need to add the page explicitly in this data items as shown in below diagram.
By doing this, Sitecore Wildcard module started to work for newly created page.


For every newly created page, we cannot add the page to this Item collection automatically. If someone finds the solution to resolve this issue, it would be a great help. !!!

16 March, 2012

How to use extension in the Sitecore Media Request URL – 8

Go to Web.config file and find a setting named “Media.RequestExtension” and remove the extension value.
<!--  MEDIA - REQUEST EXTENSION                
The extension to use in media request URLs. If the value is not set, the Extension field of the individual media items will be used (ie. JPG, GIF, etc.)
Default value: "ashx"-->
      <setting name="Media.RequestExtension" value="ashx" />

Remove ashx from the value.
      <setting name="Media.RequestExtension" value="" />


Sample:
Before change      : /~/media/Images/logo.ashx
After change         : /~/media/Images/logo.png (ashx also will work.)



How to allow dots(.) in Sitecore Item names – 7

Go to Web.config file and find a setting named “ItemNameValidation” and change the validation to allow dots. 
      <!--  ITEM NAME VALIDATION
            Regular expression for validating item names   -->
      <setting name="ItemNameValidation" value="^[\w\*\$][\w\s\-\$]*(\(\d{1,}\)){0,1}$" />
Modified Regular Expression.
      <setting name="ItemNameValidation" value="^[\w\*\$][\w\.\s\-\$]*(\(\d{1,}\)){0,1}$" /> 


15 March, 2012

How to use SecurityDisabler and UserSwitcher in Sitecore

In Sitecore, if the current context user doesn’t have permission to access this item, Sitecore will return null or throw exception.

SecurityDisabler:
SecurityDisabler will elevate the context user to have administrative privilege and so context user will be able to do anything on the system.

new Sitecore.SecurityModel.SecurityDisabler();

UserSwitcher:
UserSwitcher allows a segment of code to run under a specific user instead of current context user. 

new Sitecore.Security.Accounts.UserSwitcher(Sitecore.Security.Accounts.User.FromName("username",false));

Note: It is recommended to provide context user with appropriate rights than using SecurityDisabler or UserSwitcher.

Sample Code:


        /// <summary>
        /// Code snippets explaining SecurityDisabler and UserSwitcher
        /// </summary>
        private void SecuritySample()
        {
            //Getting Master Database
            Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
            //Getting a Sitecore Item
            Sitecore.Data.Items.Item home = masterDB.GetItem("/sitecore/content/Home"); 
            //Using Begin, End, Cancel Edit and Security Disabler
            BeginEditAndSecurityDisabler(home); 
            //Using EditContext and Security User Switcher
            EditContextAndSecurityUserSwitcher(home);
        }
 
        /// <summary>
        /// Using Begin, End, Cancel Edit and Security Disabler
        /// </summary>
        /// <param name="home"></param>
        private void BeginEditAndSecurityDisabler(Sitecore.Data.Items.Item home)
        {
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                home.Editing.BeginEdit();
                try
                {
                    home["Title"] = "Title from Code"; 
                    //Commit the changes
                    home.Editing.EndEdit();
                }
                catch (Exception)
                {
                    //Revert the Changes
                    home.Editing.CancelEdit();
                }
            }
        }
 
        /// <summary>
        /// Using EditContext and Security User Switcher
        /// </summary>
        /// <param name="home"></param>
        private void EditContextAndSecurityUserSwitcher(Sitecore.Data.Items.Item home)
        {
            //User which is already created in Sitecore User Manager
            string testUser = @"sitecore\testuser"; 
            //User existing or not
            if (Sitecore.Security.Accounts.User.Exists(testUser))
            {
                //Getting Sitecore User Object with UserName
                Sitecore.Security.Accounts.User scUser = Sitecore.Security.Accounts.User.FromName(testUser, false); 
                //Switching Context User
                using (new Sitecore.Security.Accounts.UserSwitcher(scUser))
                {
                    //Using EditContext to edit an Item
                    using (new Sitecore.Data.Items.EditContext(home))
                    {
                        home["Text"] = "Modified Text from Code";
                    }
                }
            }
        }
Before UserSwitcher:
After UserSwitcher:


How to edit an Item in Code Behind (Sitecore v6.5) – 5

Sitecore will throw exception when editing an item if the item is not in editing mode. 
There are two ways where you can put an item in editing mode.
1.       Sitecore.Data.Items.ItemEditing
2.       Sitecore.Data.Items.EditContext
Scenario
Sitecore.Data.Items.ItemEditing
Sitecore.Data.Items.EditContext
To begin editing
Item.Editing.BeginEdit()
Using(new Sitecore.Data.Items.EditContext(item))
To Commit/End Changes
Item.Editing.EndEdit()
If previous statement passes, automatically item changes will be committed.
To Cancel Edit
Item.Editing.CancelEdit()
If previous statement fails, automatically item changes will be rolled back.
 
1.       Sitecore.Data.Items.ItemEditing
Unless and until, we call EndEdit() or CancelEdit(), Sitecore does not commit the changes.
 
            //Getting Master Database
            Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
            //Getting a Sitecore Item
            Sitecore.Data.Items.Item home = masterDB.GetItem("/sitecore/content/Home");
            //Begin Editing Sitecore Item
            home.Editing.BeginEdit();
            try
            {
                home["Title"] = "Title from Code";
                //Commit the changes
                home.Editing.EndEdit();
            }
            catch (Exception)
            {
                //Revert the Changes
                home.Editing.CancelEdit();
            }
 
2.       Sitecore.Data.Items.EditContext
Closure of Using in EditContext will implicitly commit any changes within that segment of code and also calls dispose().
 
            //Getting Master Database
            Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
            //Getting a Sitecore Item
            Sitecore.Data.Items.Item home = masterDB.GetItem("/sitecore/content/Home");
            //Using EditContext to edit an Item
            using (new Sitecore.Data.Items.EditContext(home))
            {
                home["Text"] = "Modified Text from Code";
            }

13 March, 2012

How to access Sitecore Items in Code Behind (Sitecore v6.5) – 4

To get a Sitecore Content Item, use Sitecore.Data.Database.GetItem(Path)
­Sitecore Content Item Class: Sitecore.Data.Items.Item 
(Get Sitecore Item from “/sitecore/content/Home/myItem”)
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item myItem = master.GetItem("/sitecore/content/Home/myItem"); 
If item does not exist or current context user doesn’t have permission to access this item, Sitecore will return null or throw exception. 
Case is Insensitive while using path to get the items. 

To get a Sitecore Template Item, use Sitecore.Data.Database.GetTemplate(ID)
­Sitecore Template Item Class: Sitecore.Data.Items.TemplateItem 
(Get Template Item: Folder Template)
Sitecore.Data.Items.TemplateItem item = master.GetTemplate(Sitecore.TemplateIDs.Folder); 

To get a Sitecore Media Item, use Sitecore.Data.Database.GetItem(Path)
­Sitecore Media Item Class: Sitecore.Data.Items.MediaItem 
(Get Media Item from “/sitecore/content/Media Library/Images/Logo.png”)
Sitecore.Data.Items.Item myItem = master.GetItem("/sitecore/content/Media Library/Images/Logo.png");
 
To get a Sitecore System Item, use Sitecore.Data.Database.GetItem(ID)
­Sitecore Item Class: Sitecore.Data.Items.Item 
(Get System Item “Layouts”)
Sitecore.Data.Items.Item layoutsItem = master.GetItem(Sitecore.ItemIDs.Layouts);  

To get Sitecore Context Item, use Sitecore.Context.Item
­Sitecore Template Item Class: Sitecore.Data.Items.Item 
Sitecore.Data.Items.Item contextItem = Sitecore.Context.Item;
Note: Add Sitecore.Kernel.dll reference to project.

How to access Sitecore Content Database in Code Behind (Sitecore v6.5) - 3

Content Database:
Sitecore Content Editor primarily interact with Content database i.e. Master Database(by default).
To get the content database, use the following property.
Sitecore.Data.Database contentDB = Sitecore.Context.ContentDatabase;
Note: Add Sitecore.Kernel.dll reference to project.

12 March, 2012

How to access Sitecore Database in Code Behind (Sitecore v6.5) - 2

There are three methods to work with Sitecore Database. To obtain it, you have to use Factory class Sitecore.Configuration.Factory 

GetDatabase("Database Name"): Get Sitecore Database by Name 
Sitecore.Data.Database database = Sitecore.Configuration.Factory.GetDatabase("DBName");
 You need to pass Database name as a parameter and it will return Sitecore Database Object.
 Note: “DBName” should match with ID of any /Configuration/Sitecore/Databases/Database and it is case sensitive. 

GetDatabaseNames(): Get all the Sitecore Database names 
string[] databases = Sitecore.Configuration.Factory.GetDatabaseNames(); 
GetDatabaseNames method returns a string array with list of Database Names added in /Configuration/Sitecore/Databases/Database. 

GetDatabases(): Get all the Sitecore Databases Object in a List
List<Sitecore.Data.Database> databases = Sitecore.Configuration.Factory.GetDatabases();
GetDatabases will return a list with all the Sitecore Database Objects configured in /Configuration/Sitecore/Databases/Database.
Note: Add Sitecore.Kernel.dll reference to project.

How to access Context Database in Sitecore v6.5 - 1

Sitecore has 3 databases: Core, Master and Web. Core has Sitecore Components; Master has all versioned files and unversioned content; Web has only latest (published) content.
Current Client
Database
When using Sitecore Client or any CMS client
Core Database
When using Page Editor
Master Database
When browsing Published Site
Web Database

Sitecore.Data.Database context = Sitecore.Context.Database;

Note: Add Sitecore.Kernel.dll reference to project.

blockquote { margin: 0; } blockquote p { padding: 15px; background: #eee; border-radius: 5px; } blockquote p::before { content: '\201C'; } blockquote p::after { content: '\201D'; }