Appendix B. Framework API

The Framework API is the newest member of the API set that Ektron includes with CMS400 .NET. It was added to promote discoverability and consistency. Discoverability means that a developer should be able to guess the namespace, object name, and method to use without having to read through huge amounts of documentation. With a well designed API, users should be able to intuit their way through a situation using context clues delivered by the naming of objects and IntelliSense.

For consistency, the goal was to make the API work in similar ways for similar functions. Therefore, it should be easy to understand how to work with a given object because it works in the same way as other objects you have already used. The final goal was to make the API simple to use. It should make easy things easy to do, while steering developers down the path of having easily maintainable code that follows best practices, without needing to keep a large number of rules in mind.

This appendix discusses some of the overall themes of the Framework API, and then jumps into a few snippets demonstrating how to achieve some typical simple goals.

WORKING WITH THE FRAMEWORK API

There are two points to understand before you can effectively work with the Framework API. The first is how the API interacts with the underlying permission model, and the second is the overall construction of the API. For the Framework API, the permissions flag is set when the API objects are initially created, by specifying a switch in the constructor for the object.

Framework Object Constructors

When creating a new Framework object to perform actions, the constructor has an optional parameter to specify the access mode for that object. For instance, when creating a new Content object, the code will look like the following snippet.

Ektron.Cms.Framework.Core.Content.Content content
  = new Ektron.Cms.Framework.Core.Content.Content(
                 Ektron.Cms.Framework.ApiAccessMode.Admin);

This snippet contains the optional parameter specifying the access mode, which in this case is set to Admin. The two valid options in the enumeration are:

  • Ektron.Cms.Framework.ApiAccessMode.Admin: The Admin mode specifies that permissions should be ignored for all actions undertaken via this object.

  • Ektron.Cms.Framework.ApiAccessMode.LoggedInUser: When set to LoggedInUser, the object will query and work within the permissions set for the current user.

There is no way to set the objects to work as a different user than the current logged-in user or an administrative user. However, this ability to switch between the two options allows you to easily circumvent the permissions model when you run into a task that requires it.

You can also skip this parameter, in which case the default behavior is to act as the currently logged in user. When objects are created as the current user, the system will properly maintain user attributions in the history for the item.

Where to Find the Framework API

The Framework API lives in the namespace Ektron.Cms.Framework. The namespace then contains a series of further namespaces, each of which contains objects for dealing with a particular data type in the CMS.

  • Ektron.Cms.Framework.Activity: The activity namespace contains objects needed to manage user and group activity streams.

  • Ektron.Cms.Framework.Analytics: The analytics namespace contains the object needed to manage business analytics, and reporting services that expose data about site visitors.

  • Ektron.Cms.Framework.Calendar: The calendar namespace is used to manage calendars and events on the WebCalendar stack.

  • Ektron.Cms.Framework.Core: The core namespace contains objects to manage content and folders, as well as custom properties, which allow you to add programmatically maintained data to any CMS object.

  • Ektron.Cms.Framework.Messaging: The messaging namespace is used to work with e-mail message definitions.

  • Ektron.Cms.Framework.Notifications: The notifications namespace is used to work with the notifications engine, which allows the sending of messages through a variety of mediums, including e-mail and SMS messages.

  • Ektron.Cms.Framework.SocialNetworking: The social networking namespace contains tools to work with the social networking feature, including MicroMessaging.

  • Ektron.Cms.Framework.Users: The users framework, appropriately, allows you to perform operations on users in the system.

Each namespace may contain one or more objects to work with data in a given area. The Framework API, as the newest member of the APIs made available to developers, will continue to expand and should be the first stop for people developing against the Ektron Framework.

CRUD OPERATIONS ON CONTENT

Now that you have looked at a high level at what is available in the Framework API, let's dive in for a deeper look at how to perform CRUD (create, retrieve, update, and delete) operations using the API. These examples work with content, but the methodology is the same across all the objects that the Framework API exposes.

Create

The creation methods in the framework API are called Add(). In each case the Add() method will take in a data object and store the data in the database. In the Content object, the data object is of type Ektron.Cms.ContentData, and is passed in by reference, which means the appropriate fields are updated in place after the content has been added to the system. The following snippet adds a new piece of content to the root folder with the language set to English.

//create the Content object set to observe permissions
Ektron.Cms.Framework.Core.Content.Content ContentAPI =
    new Ektron.Cms.Framework.Core.Content.Content();

//set up the contentdata object
Ektron.Cms.ContentData newContent = new Ektron.Cms.ContentData();
newContent.LanguageId = 1033;
newContent.FolderId = 0;
newContent.Title = "Content Added through the Framework API";
newContent.Teaser = "The summary for my content";
newContent.Html = "<p>The HTML for programmatically added content</p>";

//add the content
ContentAPI.Add(newContent);

//output the new content ID
Response.Write(newContent.Id.ToString());

Retrieve

Retrieving content is equally simple. The method used for retrieving a single item is called GetItem(), and is named that way across the entire Framework API. It will return a data object, in this case an instance of Ektron.Cms.ContentData. The following snippet retrieves the content item with ID 30, in the current language. Remember that this will respect the permissions of the logged-in user, so if they don't have permission to the content, the object returned will be empty unless you instantiate the object in ApiAccessMode.Admin.

//create the Content object set to observe permissions
Ektron.Cms.Framework.Core.Content.Content ContentAPI =
    new Ektron.Cms.Framework.Core.Content.Content();

//retrieve the content
Ektron.Cms.ContentData contentData;
contentData = ContentAPI.GetItem(30);

//output the retrieved item's content
Response.Write(contentData.Html);

Retrieving a List of Data

In addition to retrieving single pieces of data, sometimes you'll need to retrieve a list of items matching a constraint. In the framework API, this is done through the use of Criteria objects. Every API object in the Framework API provides a method called GetList(), which accepts a Criteria object. Criteria objects in turn accept items from an appropriate enumeration containing fields to search against, along with an operator and operand. The method to add these tuples is called AddFilter().

There are several additional properties on the Criteria object that allow you to define output parameters. These properties are listed here:

  • Filters: Allows you to examine or modify the filters currently applied.

  • OrderByField: The resulting list is sorted by the field specified in this property.

  • OrderByDirection: Specifies whether to sort the output in ascending or descending order.

  • PagingInfo: Contains properties allowing for control over paging of the results.

The following code snippet shows how you might retrieve a list of content from the root folder that is published. It also specifies a filter to retrieve items only with the word "TitleSearch" in the title of the content.

//create the Content object set to observe permissions
Ektron.Cms.Framework.Core.Content.Content ContentAPI
  = new Ektron.Cms.Framework.Core.Content.Content();

//create the criteria object
Ektron.Cms.Common.Criteria<Ektron.Cms.Common.ContentProperty> myCriteria
  = new Ektron.Cms.Common.Criteria<Ektron.Cms.Common.ContentProperty>();

//add a filter to specify the root folder
myCriteria.AddFilter(
  Ektron.Cms.Common.ContentProperty.FolderId,
  Ektron.Cms.Common.CriteriaFilterOperator.EqualTo,
  0);

//add a filter to only retrieve published items
myCriteria.AddFilter(
  Ektron.Cms.Common.ContentProperty.IsPublished,
  Ektron.Cms.Common.CriteriaFilterOperator.EqualTo,
  true);

//add a filter to only retrieve items that contain the word "TitleSearch"
//in the title
myCriteria.AddFilter(
  Ektron.Cms.Common.ContentProperty.Title,
  Ektron.Cms.Common.CriteriaFilterOperator.Contains,
  "TitleSearch");

//create the output object
List<Ektron.Cms.ContentData> resultList;

//retrieve the results
resultList = ContentAPI.GetList(myCriteria);

Update

Updating is, as one would expect, done through the Update() method. This method takes in the data item and updates the database with the modified fields. This action requires that the developer first retrieve the existing item, and then they can call the Update() method after modifying some properties. The following example retrieves the content item with ID 30 and updates the Title property before saving it back to the database.

//create the Content object set to observe permissions
Ektron.Cms.Framework.Core.Content.Content ContentAPI =
    new Ektron.Cms.Framework.Core.Content.Content();

//get the content item
Ektron.Cms.ContentData myContent;
myContent = ContentAPI.GetItem(30);

//update a field on the content item
myContent.Title = "This was updated";

//save the updates
ContentAPI.Update(myContent);

Delete

The final piece of CRUD operations is to delete an item from the database. The method used when deleting items is called Delete(), and just like the rest of the methods in the Framework API, it will respect the logged-in user's permissions unless the API object was instantiated in Admin mode. The following snippet deletes the item with ID 30.

//create the Content object set to observe permissions
Ektron.Cms.Framework.Core.Content.Content ContentAPI =
    new Ektron.Cms.Framework.Core.Content.Content();

//delete the content item
ContentAPI.Delete(30);

TAKE HOME POINTS

There are cases where the Framework API will not perform the exact function you need in a particular instance. In those cases, you will need to look at some of the other APIs exposed by Ektron. However, if you can perform a given function with the Framework API, that should always be your first stop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.24.134