3

I've to create a new document in the back end but can not find any useful information on how to do this in Kentico 9.

So far I've got

 UserInfo user = UserInfoProvider.GetUserInfo("administrator");

// Creates a tree provider instance using administrator context
TreeProvider tree = new TreeProvider(user);

// Prepare parameters
string siteName = CMS.SiteProvider.SiteContext.CurrentSiteName;
string aliasPath = "/News";
string culture = "en-GB";
bool combineWithDefaultCulture = false;
string classNames = TreeProvider.ALL_CLASSNAMES;
string where = null;
string orderBy = null;
int maxRelativeLevel = -1;
bool selectOnlyPublished = false;
string columns = null;

// Get the example folder
TreeNode parentNode = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

if (parentNode != null)
{
  // Create a new node
  TreeNode node = TreeNode.New("CMS.News", tree);

  // Set the required document properties
  node.DocumentName = "Test";
  node.DocumentCulture = "en-GB";

  // Insert the document
  try
  {
    DocumentHelper.InsertDocument(node, parentNode, tree);
  }
  catch (Exception ex)
  {
    EventLogProvider.LogException("Create New News", "EXCEPTION", ex);
  }
}

This however throws an error:

Message: [WebFarmTaskManager.CanCreateTask]: Task type 'DICTIONARYCOMMAND' is not supported. The task needs to be registered with WebFarmHelper.RegisterTask method.

Has anyone had experience with doing this in Kentico 9?

Spitfire2k6
  • 308
  • 4
  • 17
  • Also, based on the error that you are seeing, seems like you have some webfarm setup and its not able to send the document over to the other location. Does it create the document still? – Josh May 25 '16 at 15:24
  • No Web farm set up. single server. – Spitfire2k6 May 27 '16 at 07:39
  • The following may be helpful: https://docs.kentico.com/display/API9/Pages – Clint Jul 02 '18 at 22:13

2 Answers2

4

Sorry, may be my two cents comes too late but I solved the same issue invoking the following initialization method not mentioned on Kentico docs:

CMS.DataEngine.CMSApplication.Init();
Marcello R.
  • 249
  • 1
  • 5
2

You can create page in content tree like:

// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets the current site's root "/" page, which will serve as the parent page
TreeNode parentPage = tree.SelectSingleNode(SiteContext.CurrentSiteName, "/", "en-us");

if (parentPage != null)
{
// Creates a new page of the "CMS.MenuItem" page type
TreeNode newPage = TreeNode.New(SystemDocumentTypes.MenuItem, tree);

// Sets the properties of the new page
newPage.DocumentName = "Articles";
newPage.DocumentCulture = "en-us";

// Inserts the new page as a child of the parent page
newPage.Insert(parentPage);

You can find more examples in API Examples documentation for version 9.

Martin Makarsky
  • 2,580
  • 1
  • 17
  • 28