Create Site
Overview¶
You can use the API to create a website.
Supported Versions¶
Pleasanter 1.3.4.0 and later
Preparations¶
Please Create an API Key before performing API operations. Also, this function can only be performed by the tenant manager, so please set up the tenant manager from user management.
Request¶
Send json data in the following request format:
| Setting column | Value |
|---|---|
| HTTP Method | POST |
| Content-Type | application/json |
| Character Code | UTF-8 |
| URL | http://{server name}/api/items/{parent site ID}/createsite (*1) |
| Body | Refer to the json data below |
(*1) Please edit the {server name} and {parent site ID} parts to suit your environment as appropriate.
For Pleasanter.net, the format is as follows:
https://pleasanter.net/fs/api/items/{parent site ID}/createsite
For {parent site ID}, specify the parent site for which the site is to be created.
JSON¶
{
"ApiVersion": "1.1",
"ApiKey": "345yuAjA6789dA09d8uj6...",
"TenantId": 1,
"Title": "Site name",
"ReferenceType": "Issues",
"InheritPermission": 99999,
"SiteSettings": {
"Version": 1.017,
"ReferenceType": "Issues",
"GridColumns": [
"IssueId",
"TitleBody",
"Comments",
"StartTime",
"CompletionTime",
"WorkValue",
"ProgressRate",
"RemainingWorkValue",
"Status",
"Manager",
"Owner",
"Updator",
"UpdatedTime"
],
"EditorColumnHash": {
"General": [
"IssueId",
"Ver",
"Title",
"Body",
"StartTime",
"CompletionTime",
"WorkValue",
"ProgressRate",
"RemainingWorkValue",
"Status",
"Manager",
"Owner",
"Comments"
]
}
}
}
*For the parameters after TenantID, please set the same value as the "Site" parameter when exporting the site package.
The following example creates a Wiki.
JSON¶
{
"ApiVersion": 1.1,
"ApiKey": "345yuAjA6789dA09d8uj6...",
"Title": "Wiki test",
"ReferenceType": "Wikis",
"ParentId": 1,
"InheritPermission": 1,
"SiteSettings": {
"ReferenceType": "Wikis"
}
}
Code Samples¶
Modify {{ ... }} in the code as necessary.¶
1. Read a site package and create a site with its information
Place a site package in a folder of your choice, read it with the program, and create a site with its contents.
Although a site package can contain multiple exported sites, this sample code creates the site with the information of the first site in the package (created from Site[0]).
This sample also requires a site name to be specified when running Python. If it is not specified, an error occurs.
api_site_create_p1.py¶
# Library for getting arguments
import argparse
# Library for JSON, system, and path operations
import json
# Library for system operations
import sys
# Library for path operations
from pathlib import Path
# Library for sending HTTP requests
import requests
# Server name and API key
BASE_URL = "{{URL}}"
API_KEY = "{{API key}}"
# Parent site under which the site is created
PARENT_SITE_ID = {{Site ID}}
# Folder where the site package is placed
PACKAGE_DIR = Path(r"{{File path}}")
# File name of the site package to use
PACKAGE_FILE_NAME = "{{Site package file name}}.json"
# Read the site package JSON.
def load_site_package() -> dict:
package_path = PACKAGE_DIR / PACKAGE_FILE_NAME
if not package_path.exists():
raise FileNotFoundError(f"Site package not found: {package_path}")
with package_path.open("r", encoding="utf-8-sig") as f:
return json.load(f)
# Build the body for the Create Site API based on Sites[0] of the site package.
def build_create_site_body(site_package: dict, title: str) -> dict:
sites = site_package.get("Sites", [])
if not sites:
raise ValueError("'Sites' does not exist in the site package, or it is empty.")
body = dict(sites[0]) # Copy Sites[0]
# Required/overwritten items for the Create API
body["ApiVersion"] = "1.1"
body["ApiKey"] = API_KEY
body["TenantId"] = 1
body["Title"] = title
body["SiteName"] = title
# Create under the parent site and inherit permissions from the parent
body["ParentId"] = PARENT_SITE_ID
body["InheritPermission"] = PARENT_SITE_ID
# SiteId is not needed when creating a new site, so remove it
body.pop("SiteId", None)
return body
# Call the Create Site API.
def create_site(body: dict) -> dict:
url = f"{BASE_URL.rstrip('/')}/api/items/{PARENT_SITE_ID}/createsite"
headers = {"Content-Type": "application/json"}
response = requests.post(
url,
headers=headers,
data=json.dumps(body, ensure_ascii=False),
)
if not response.ok:
raise RuntimeError(f"API Error: {response.status_code}\n{response.text}")
return response.json()
def main():
parser = argparse.ArgumentParser(description="Pleasanter Create Site API sample")
parser.add_argument("title", help="Name of the site to create")
args = parser.parse_args()
site_package = load_site_package()
body = build_create_site_body(site_package, args.title)
result = create_site(body)
print(json.dumps(result, ensure_ascii=False, indent=2))
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
Run¶
Specify any site name as the argument.
Execution Result¶
Please refer to the following manual for how to use it in scripts.
Developer's Guide: Scripts: $p.apiCreateSite