Create a Site programmatically

Enonic version: 6.14.1
OS: Xubuntu (but I’m running Enonic through your Docker image)

I’m trying to create a Site programmatically, as I want the complete setup for my web page to be automated.

I have created a service, where I’m doing the following:

var somesite = {
    name: "somesite",
    parentPath: "/",
    contentType: 'portal:site',
    branch: 'draft',
    requireValid: true,
    displayName: "SomeSite",
    data: {
        siteConfig: {
            applicationKey: "myapp",
            config: {}
        }
    },
    page: {
        controller: "myapp:apis"
    }
};

var result = contentLib.create(somesite);

return {
    body: {
        time: new Date(),
        result: result
    },
    contentType: 'application/json'
};

The response I get is this:

// 20180326154229
// http://localhost:8080/_/service/myapp/setup

{
  "time": 1522071748615,
  "result": {
    "_id": "c916791e-bcea-4007-9e08-6ba1d72c6ea9",
    "_name": "somesite",
    "_path": "/somesite",
    "creator": "user:system:su",
    "modifier": "user:system:su",
    "createdTime": "2018-03-26T13:42:28.502Z",
    "modifiedTime": "2018-03-26T13:42:28.502Z",
    "owner": "user:system:su",
    "type": "portal:site",
    "displayName": "SomeSite",
    "hasChildren": true,
    "valid": true,
    "data": {
      "siteConfig": {
        "applicationKey": "myapp",
        "config": {
          
        }
      }
    },
    "x": {
      
    },
    "page": {
      
    },
    "attachments": {
      
    },
    "publish": {
      
    }
  }
}

Why hasn’t this new site been initialized with the applicationKey and siteConfig I specified in the create-call? Is there some other way to do this, so I don’t have to do it manually?

In another service I’m getting a Site which I created manually:

var result = contentLib.get({
    key: '/anotherpath',
    branch: 'draft'
});

From the result you can see that a applicationKey and controller are present (which is what I’m trying to achieve):

// 20180326154819
// http://localhost:8080/_/service/myapp/get

{
  "time": 1522072098877,
  "result": {
    "_id": "69b8b975-8a43-4677-a040-841e2f83dec6",
    "_name": "anothername",
    "_path": "/anotherpath",
    "creator": "user:system:su",
    "modifier": "user:system:su",
    "createdTime": "2018-03-26T06:26:38.469Z",
    "modifiedTime": "2018-03-26T13:11:41.701Z",
    "owner": "user:system:su",
    "type": "portal:site",
    "displayName": "Test",
    "hasChildren": true,
    "valid": true,
    "data": {
      "siteConfig": {
        "applicationKey": "myapp",
        "config": {
          
        }
      }
    },
    "x": {
      
    },
    "page": {
      "controller": "myapp:apis",
      "config": {
        
      },
      "regions": {
        
      }
    },
    "attachments": {
      
    },
    "publish": {
      "from": "2018-03-26T08:44:30.262Z",
      "first": "2018-03-26T06:27:12.226Z"
    }
  }
}

The create function from content-lib does not currently support setting siteConfig or page details.

You can do it using the lower level Node API.
After your call to contentLib.create, do something like this:

// var nodeLib = require('/lib/xp/node');

var repoConn = nodeLib.connect({
    repoId: 'cms-repo',
    branch: 'draft'
});
var siteNode = repoConn.modify({
    key: somesite._id,
    editor: function (node) {
        node.page = {
            "controller": app.name + ":apis",
            "customized": true,
            "config": {}
        };
        node.data = {
            "siteConfig": {
                "applicationKey": app.name,
                "config": {}
            }
        };
        return node;
    }
});

But I agree it should have worked the way you were trying. We will improve the create and modify functions in content-lib to allow setting the page and site config.

4 Likes

That worked perfectly, thanks!

This topic was automatically closed after 12 hours. New replies are no longer allowed.