Documentation > Best Practices and Examples > AccessAPI

Getting started with the Crownpeak Access API

This is re-purposed from Richard Hamlyn's blog, Learn Crownpeak DXM.

Richard is a Crownpeak Sales Engineer


In this blog, you will see how you can easily create a small application to add an asset to your Crownpeak DXM instance.

Before you start, there are some precursory steps you need to take that are individual to your account, these are outlined in Steps 1 and 2.

Step1


Contact support@crownpeak.com and request a developer public token for your instance.  You will need this to access the Content Management System's Access API.

Step 2


Take a note of the following information, which you will need to perform the actions through the Access API.

  • Your instance name i.e. https://cms.crownpeak.net/yourinstancename/v3.
  • Your public key (see step 1).
  • Your username that you will use to log in with.
  • Your password that is linked to the login username.
  • The folder ID where you will create the asset.

Step 3


Download the Access API Helper classes.  Unzip the compressed solution and save for later.

Step 4


In Visual Studio, open the AccessApiHelper.sln found in the package from Step 3.

You should see the following

Step 5


Create a new Console Project name 'AccessApiConsole' in the solution above.  You can use all the defaults just for this example.

Step 6


Add the following references to your Console project.

Step 7


Update the Console Project's App.config to hold your Crownpeak Instance credentials.
Where:
  • 'instance' is your own instance of Crownpeak DXM i.e. https://cms.crownpeak.net/yourinstance/
  • 'publicKey' is your Access PI token obtained from Crownpeak Support.
  • 'username' and 'password' are an account that has the necessary permissions to perform the actions you intend on running through the Access API (in this example, create an asset).

Step 8


Open the class 'Program.cs' and add the following references:
using CrownPeak.AccessApiHelper;
using CrownPeak.AccessApiHelper.ApiAccessor;
using System.Configuration;
using CrownPeak.AccessAPI;
Now add the variables of your settings from your App.config:
class Program
    {
        //Variables for connecting to your CMS
        private static CmsApi _cms;
        private readonly static string Server = ConfigurationManager.AppSettings["server"];
        private readonly static string Instance = ConfigurationManager.AppSettings["instance"];
        private readonly static string PublicKey = ConfigurationManager.AppSettings["publicKey"];
        private readonly static string Username = ConfigurationManager.AppSettings["username"];
        private readonly static string Password = ConfigurationManager.AppSettings["password"];
Now add the code to create a new Asset in Crownpeak DXM inside the Main method of your Program.cs.
//Sign in to the CMS
    _cms = new CmsApi(new SimpleApiAccessor());
    _cms.Init(Server, Instance, PublicKey);
    //Now login
    var result = _cms.Login(Username, Password);
    Console.WriteLine("User logged in!");
    // _Asset Folder in CPUK
    var folderId = 69912;
    // Create a new Developer Asset
    var assetName = "BlackSheep Test 102";
    WorklistAsset asset;
    if (result)
      {
       try
          {
            _cms.Asset.Create(assetName, folderId, 0, 2, 0, 0, 0, out asset);
            Console.WriteLine("Asset with name: " + assetName + " created! Press ESC to exit");
            //Pause to let user ready console
            Console.ReadKey();
           }
       catch (Exception e)
           {
            Console.WriteLine("No asset created! You seem to have an error: " + e);
            //Pause to let user ready console
            Console.ReadKey();
            }
       }
Note.  Add your own folderId where you want the asset to be created in your own instance of Crownpeak DXM.  Also, change the name of the asset if you wish.
Set your Console Project as the startup Project for this solution.  Right-click on the solution in Solution Explorer -> Select 'Set Startup Projects'.  Set as shown:
Now, run the project by hitting the Start button in Visual Studio.
You should see the above message in the console.
Now, if you go to the folder you chose in your instance of Crownpeak DXM you will see the asset.
Crownpeak DXM New Asset
In this example, the new asset was given the name 'BlackSheep Test 102'.
Note.  This example demonstrated how to create an Asset in Crownpeak DXM that implemented the Developer Template.  You can take your code to the next level by defining the model of a template and then you can add field values programmatically, as required.  The Helper class has a lot of functionality that you can implement and you should take a look to build your use of the Access API to fit your requirements.
Download Program.cs

Connect with CrownPeak