Wednesday, August 20, 2014

Working with Sitecore Items

You can use the Sitecore.Data.Items.Itemclass to access any item. Sitecore provides specialized classes to represent specific types as items, such as Sitecore.Data.Items.TemplateItemto represent a data template and Sitecore.Data.Items.MediaItemto represent a media item.

1) Retrieve items

Retrieve item from context
Item current = Sitecore.Context.Item

Retrieve item from master database using item path
Database master = Sitecore.Configuration.Factory.GetDatabase (“master”);
Item current =master.GetItem (“/sitecore/content/Home/Page1”);

Retrieve item from master database using item id
Database master = Sitecore.Configuration.Factory.GetDatabase (“master”);
Item current =master.GetItem (“{BC5E60E8-25A0-4E04-BD60-D919F5E9121F}”);

Retrieve item from master database using fast query

Basics of fast query

Sitecore fast query is designed for retrieving and filtering items from the Sitecore database. Sitecore Fast Query uses the database engine to execute queries. Sitecore Fast Query has the following benefits compared to the standard Sitecore Query
  •   Improved performance – queries are executed by the SQL engine and as a result the scalability and performance of the SQL engine is not limited by .NET or by Sitecore.
  •  Consumes less memory – Sitecore Query loads every item that it touches into memory (cache) and this can fill the cache with unnecessary information. Sitecore Fast Query only loads the items from the result set and this minimizes the pressure on the cache.


Sitecore Fast Query is similar to XPath and Sitecore Query statements in many ways. The most notable difference in using Fast Query is that you use the fast: keyword.
Sitecore Fast Query can be used:
  •  In the Content Editor as a source for List Types fields.
  •  In the Developer Center, in the XPath Builder tab.
  •  In custom .NET code by using the SelectSingleItem () method and the SelectItems () methods of the Sitecore.Data.Database class.
In Content Editor Sitecore Query is used for the items that contain a Source field.
  • Path to the root item:
/sitecore/content/Home/Shapes
This expression returns all the items that are children of the Shapes item.
  • Sitecore Query:

query: /sitecore/content/Home/Shapes
This expression uses Sitecore Query to return the Shapes item.

query: /sitecore/content/Home/Shapes/*
This expression uses the Sitecore Query to return all the items that are children of the Shapes item.
  •          Sitecore Fast Query

Query: fast: /sitecore/content/Home/Shapes
This expression uses Sitecore Fast Query to return the Shapes item.

Query: fast: /sitecore/content/Home/Shapes/*
This expression uses Sitecore Fast Query to return all the items that are children of the Shapes item.

Using API in custom .NET code

Database master = Sitecore.Configuration.Factory.GetDatabase (“master”);
Item [] items = master.SelectItems (“fast: /sitecore/content/Home/*[@@templatename=’Page’]”);


If (items! = null && items. Length > 0)
{
                foreach (Item current in items)
{
                // do work
}
}

2) Create an item
Sitecore.Data.Items.Item.Add () method can be used to create an item. The parent item and the data template for the new item must exist before you create the item.
Database master = Sitecore.Configuration.Factory.GetDatabase (“master”);
Item parentItem = master.GetItem (“/sitecore/content/Home”);
Template templateItem = master.GetTemplate (“User Defined/Page”);
parentItem.Add (“Page2”,templateItem);

3)Publish an item
Sitecore.Publishing.PublishManager.PublishItemcan be used to publish an item.
Database master = Sitecore.configuration.Factory.GetDatabase (“master”);
Database target = Sitecore.configuration.Factory.GetDatabase (“web”);
Database [] targetDatabases = {target};
Item current = master.GetItem (“/sitecore/content/Home”);
Sitecore.Globalization.Language [] languages = master.Languages;
bool deep = true;
bool compareRevisions = true;
Sitecore.Publishing.PublishManager.PublishItem (current, targetDatabases, languages, deep, compareRevisions);

deep = true publishes an item and all of its publishable descendants.
deep = false publishes only the current item.
compareRevisions = true does a smart publish. Sitecore compares the value of the Revision field in the versions of the item in the source and the target database during the publishing operation. If the values of the Revision fields are the same, the version is not published.
compareRevisions = falsedoes a full publish. Sitecore publishes items independently of the Revision field values.

By default sitecore context user would be Anonymous, so add / edit / delete / publish would throw an exception due to access rights. We have to use any one of the following way to give access rights
  •  Using Sitecore.Security.Accounts.UserSwitcher : An instance of the UserSwitcher class executes the block of code in the context of a specific user. The user can be an Administrator and can do everything or we can create a “service user” with only specific permissions like giving only add or edit access.

Using (new Sitecore.Security.Accounts.UserSwitcher (Sitecore.Security.Accounts.user.FromName (@”sitecore\admin”,false)))
{
                //do work
}

  •       Using Sitecore.SecurityModel.SecurityDisabler:Sitecore will not do any permission checks when SecurityDisabler is used. In fact, the result will be the same as if you use the UserSwitcher () with an administrator, but using this will not give us any control on the context.

In case of publishing, we can set Publishing.CheckSecurity setting in the Web.config file to false to disable security checks while publishing.

4) Edit an item
Sitecore APIs that update items may throw exceptions if the item is not in editing mode. You can place an item in editing mode using methods of the Sitecore.Data.Items.Item.Editing property, or by using the Sitecore.Data.Items.EditContext class.

  •         Editing property

                 Item current = master.GetItem (“/sitecore/content/Home/Page2”);
                 try
                 {
                              current.Editing.BeginEdit ();
                              //do work
                             current.Editing.EndEdit ();// commits changes
                   }
                 catch (Exception ex)
                  {
                             Current.Editing.CancelEdit (); // does not commit changes
                  }

        When using Editing property, EndEdit () would commit the changes and CancelEdit () does not commit the changes.

  •          EditContext class

                 Item current = master.GetItem (“/sitecore/content/Home/Page2”);
                 using (new Sitecore.Data.Items.EditContext (current))
                 {
                               //do work
                 }

The closure of the using statement invokes the Sitecore.Data.Items.EditContext.Dispose () method, which commits any changes made within that segment of code.

5)Delete an item
                Database master = Sitecore.Configuration.Factory.getDatabase (“master”);
Item current = master.GetItem (“/sitecore/content/Home/Page2”);

                Current.Delete ();

No comments:

Post a Comment