Get space key - how to get it

Hi :slight_smile:

I have a problem, and I’m upset because of that.
I hope that you will help me.

Ok, I’m creating blueprint. Using javascript and xmlhttp request I send a rest request to my rest controller. In rest controller I call method from services. And here is the problem. I need to have space key there, but I have no idea to do it.

I tried use Conversioncontext, but it always was null. Any ideas? DefaultConversionContext always return null as space key.

Here is my js code:

AJS.toInit(function () {
    function fileUpload(e, state) {
        var url = Confluence.getContextPath() + "/rest/resources/1.0/files";
        var xhr = new XMLHttpRequest();
        var formData = new FormData();

        xhr.open("POST", url, false);
        xhr.setRequestHeader("X-Atlassian-Token", "no-check");
        xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
        formData.append("file", document.getElementById("fileupload").files[0]);
        xhr.send(formData);

        if (xhr.status !== 200) {
            var myFlag = AJS.flag({
                type: 'error',
                title: 'Something went wrong during file uploading',
                body: '<p>You have to choose csv file.</p>'
            });
        }

        return xhr.status === 200;
    }

    function makeCheckboxes(e, state) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", Confluence.getContextPath() + "/rest/resources/1.0/epics", false);
        xmlhttp.send();
        var json = xmlhttp.responseText;
        var obj = JSON.parse(json);
        var array = [];
        for (var i = 0; i < obj.length; i++) {
            array[i] = obj[i].name;
        }

        var a = document.getElementById("checkbox-container");
        var arr = array;
        var returnStr = "";
        for (i = 0; i < arr.length; i++) {
            returnStr += '<div> <input type="checkbox" id="' + arr[i] + '" value="' + arr[i] + '"> <label for="coding">' + arr[i] + '</label> </div>';
        }
        a.innerHTML = returnStr;
    }

    function fillDropdownForColumnNames(e, state) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", Confluence.getContextPath() + "/rest/resources/1.0/columns", false);
        xmlhttp.send();
        var json = xmlhttp.responseText;
        var obj = JSON.parse(json);

        var epicSelect = document.getElementById("epic-column-name");

        var userStorySelect = document.getElementById("user-story-column-name");

        for (var i = 0; i < obj.length; i++) {
            var opt = obj[i];
            var el = document.createElement("option");
            el.textContent = opt;
            el.value = opt;
            var el2 = document.createElement("option");
            el2.textContent = opt;
            el2.value = opt;
            userStorySelect.appendChild(el);
            epicSelect.appendChild(el2);
        }
    }

    function changeFinalUrl(e, state) {
        //state.finalUrl = Confluence.getContextPath();
        //return false;
    }

    function createPages(e, state) {
        var url = Confluence.getContextPath() + "/rest/resources/1.0/spaces/pages";
        var xhr = new XMLHttpRequest();
        xhr.open("POST", url, false);
        xhr.setRequestHeader("X-Atlassian-Token", "no-check");
        xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
        xhr.setRequestHeader("Content-type", "application/json");
        xhr.send();
    }

    Confluence.Blueprint.setWizard('requirement-plugin:template-web-item', function (wizard) {
        wizard.on('submit.page1Id', fileUpload);
        wizard.on('post-render.page2Id', fillDropdownForColumnNames);
        wizard.on('post-render.page3Id', makeCheckboxes);
        wizard.on('submit.page3Id', changeFinalUrl);
        wizard.on('submit.page3Id', createPages);
    });
});

Here is my service:
(sorry for the code - but now I’m testing a lot of way to create pages


import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import com.atlassian.confluence.content.render.xhtml.DefaultConversionContext;
import com.atlassian.confluence.labels.Label;
import com.atlassian.confluence.labels.LabelManager;
import com.atlassian.confluence.pages.Page;
import com.atlassian.confluence.pages.PageManager;
import com.atlassian.confluence.pages.templates.PageTemplateManager;
import com.atlassian.confluence.renderer.PageContext;
import com.atlassian.confluence.spaces.Space;
import com.atlassian.confluence.spaces.SpaceManager;
import com.atlassian.confluence.util.LabelUtil;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.renderer.RenderContext;
import com.atlassian.spring.container.ContainerManager;
import com.soft.pgs.requirements.dto.EpicDTO;
import org.osgi.service.component.annotations.Component;

import java.util.List;

/**
 */
@Scanned
@Component
public class PageServiceImpl implements PageService {

    private PageTemplateManager pageTemplateManager = (PageTemplateManager) ContainerManager.getComponent("pageTemplateManager");
    private PageManager pageManager = (PageManager) ContainerManager.getComponent("pageManager");
    private SpaceManager spaceManager = (SpaceManager) ContainerManager.getComponent("spaceManager");
    private LabelManager labelManager = (LabelManager) ContainerManager.getComponent("labelManager");
    private RenderContext renderContext = new RenderContext();
    private ConversionContext conversionContext = new DefaultConversionContext(renderContext);
//

//    public PageServiceImpl(@ComponentImport RenderContext renderContext) {
//        this.renderContext = renderContext;
//    }

    @Override
    public void createPagesForChosenEpics(List<EpicDTO> epicDTOs) {
        List<Space> allSpaces = spaceManager.getAllSpaces();
        pageTemplateManager.getPageTemplates(allSpaces.get(0));

        PageContext pageContext = new PageContext();
        pageContext.getSpaceKey();

        List<Page> pages = pageManager.getPages(allSpaces.get(0), true);
        Page parentPage = pages.get(0);

        conversionContext.getSpaceKey();

        Page page = new Page();
        page.setTitle("Próba generalna 243");
        page.setParentPage(parentPage);
        page.setBodyAsString("<ac:structured-macro ac:name=\"content-report-table\">\n" +
                "<ac:parameter ac:name=\"spaces\">\n" +
                "    <ri:space ri:space-key=\"DOC\"/>\n" +
                "</ac:parameter>\n" +
                "<ac:parameter ac:name=\"labels\">my-index2</ac:parameter>\n" +
                "<ac:parameter ac:name=\"analytics-key\">meeting-notes</ac:parameter>\n" +
                "<ac:parameter ac:name=\"maxResults\">5</ac:parameter>\n" +
                "</ac:structured-macro>");
        page.setVersion(1);
        page.setSpace(allSpaces.get(0));

        Label label = LabelUtil.addLabel("my-index2", labelManager, page);

        parentPage.addChild(page);

        pageManager.saveContentEntity(page, null);

    }

    private void createPageWithContentReportTable(Space space) {

    }

    private void createChildPage() {

    }

}

I’ve found solution!
In javascript but it’s enough for me.

Confluence.Blueprint.Dialog.getSpaceKey()
1 Like

Also:
AJS.Meta.getAllAsMap() => returns all the metadata available on this webpage
AJS.Meta.get("space-key") => returns the space key