Return page titles with specific labels using REST API

Jason Pelletier March 26, 2015

This might be a beginner question (and that I am) and its a 2-part question that might have the same results. We're trying to use an API call to gather page titles from Confluence of pages in a specific space with specific labels and then parse those results with Java Script to show one page link per line. Something like this:

Page Title 1

Page Title 2

...

 

 

The REST API doesn't seem to be complete enough to do this type of search. Is the Advanced search with CQL the only way to go? We're not at 5.7 yet but are working to get there in the next few weeks.

 

The other question is that we're looking to either add a Confluence search box onto a web page outside of Confluence OR create a custom search box using CQL. Can the first be done and if not, I imagine CQL is the best option with a little javascript. This would also mean getting to 5.7 at least?

Thanks for the help and sorry for the beginner questions.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2015

As far as the REST API is concerned, there is an undocumented search API (but you can see it using the REST browser plugin).  The problem with the standard Confluence search is that it has trouble with labels that have a "-".  For example, if you search for pages with the label "meeting" it will return all labels that contain "meeting-notes" and vice versa.  Here is an example in JS that can run in your browser console (after being logged in to Confluence):

var showPageTitlesWithLabel = function(label, spaceKey, index) {
  if (!index) {
    index = 0;
  }
  var spaceString = "";
  if (spaceKey) {
    spaceString = "&where=" + spaceKey;
  }
  jQuery.ajax({
    url: contextPath + "/rest/searchv3/1.0/search?queryString=labelText%3A" + label + "&startIndex=" + index + "&includeArchivedSpaces=true&highlight=false" + spaceString,
    success: function(response) {
      jQuery(response.results).each(function() {
          console.log(this.title);
      });
      index += 10;
      if (index < response.total) {
        showPageTitlesWithLabel(label, spaceKey, index);
      }
    }
  });
}
//USAGE: showPageTitlesWithLabel("meeting-notes")

CQL does not have this problem, but as you stated, can only be used in 5.7 or higher.  Here is an example using CQL:

var showPageTitlesWithLabel = function(label, spaceKey, index) {
  if (!index) {
    index = 0;
  }
  var spaceString = "";
  if (spaceKey) {
    spaceString = "%20AND%20space%3D" + spaceKey;
  }
  jQuery.ajax({
    url: contextPath + "/rest/api/content/search?cql=label%3D%22" + label + "%22" + spaceString + "&start=" + index,
    success: function(response) {
      if ( response.results.length ) {
        jQuery(response.results).each(function() {
            console.log(this.title);
        });
        showPageTitlesWithLabel(label, spaceKey, index + 25);
      }
    }
  });
}
//USAGE: showPageTitlesWithLabel("meeting-notes")

 

As to the second question, it certainly seems possible, but you would have to worry about security issues, as you would have to pass the credentials for login somehow.  I have never tried it, but maybe someone with more experience could give you more advice.  Just off the top of my head, you could write a service that captures the rest request from the user and passes it along to Confluence, and then once it gets a response passes it back to the user.

If, however, you are using it on an internal webpage and you are not worried about the credentials being exposed, you could probably pass them via URL or headers.

EDIT: updated to be filterable by space

Jason Pelletier March 27, 2015

First one works and I imagine the second will too. Perfect, thanks!

Jason Pelletier March 27, 2015

One additional question and I believe I know the answer, can either search be confined to a space or will it simply search all spaces that it has permission to search?

Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2015

I updated the scripts now: if you include the spaceKey, e.g. showPageTitlesWithLabel("meeting-notes", "ds"), then it will only show the results from the space with key ds. Otherwise it will simply search all spaces it has permission for.

Jason Pelletier March 27, 2015

Great, thanks!

Ibrahim Farhan February 10, 2016

Hello,

 

Can I user And operator for label in URL?

I mean I want to search for many lables.

 

Regards

TAGS
AUG Leaders

Atlassian Community Events