2016-04-29 3 views
2

Im juste face au fait, que je veux créer une page dans un certain espace en tant qu'enfant d'un parent défini.Confluence Rest API: Puis-je créer une page par espace et parent?

Y at-il une possibilité de faire cela par ces moyens? Je ne l'ai pas encore trouvé.

Merci! :)

+0

double possible de [Comment créer une nouvelle page dans Confluence en utilisant leur API REST?] (Http://stackoverflow.com/questions/23523705/how-to-create -new-page-in-confluence-using-leur-rest-api) –

Répondre

0

Create a new page as a child of another page

Ceci est une API REST. Étant donné que l'API REST est basée sur des normes ouvertes, vous pouvez utiliser n'importe quel langage de développement Web pour accéder à l'API.

En C#, vous pouvez utiliser l'une des options suivantes:

  • HttpWebRequest/HttpWebResponse
  • WebClient
  • HttpClient (disponible à partir de 4,5 .NET sur)

I'D recommande fortement d'utiliser la classe HttpClient, car elle est conçue pour être bien meilleure (du point de vue de l'utilisabilité) que les deux précédentes.

0

toutes les pièces dont vous aurez besoin:

la fonction createpage
la fonction post http
la classe stupide pour rendre l'échantillon JSON qualifié.
la base de l'échantillon JSON & url

internal static string createAPage(string space, string title, string body, string objectType, int ancestorId = -1) 
     { 
      string json = string.Empty; 
      try 
      { 

       CreateContentWithParentJson createContentWithParentJson = JsonConvert.DeserializeObject<CreateContentWithParentJson>(_jsonCreatePageWithParentSample); 
       createContentWithParentJson.space.key = space; 
       createContentWithParentJson.title = title; 
       body = body.Replace("&", "&amp;"); 
       createContentWithParentJson.body.storage.value = "<p>" + body + "</p>"; 
       Ancestor a = new Ancestor(); 
       a.id = ancestorId; 
       createContentWithParentJson.ancestors = new List<Ancestor>(); 
       createContentWithParentJson.ancestors.Add(a); 
       json = JsonConvert.SerializeObject(createContentWithParentJson, Utils.getJsonSettings()); 

     } 
     catch (Exception) { // handle exception 
     } 
     return Utils.contentPost(json, _baseUrl); 

    } 


    internal static string contentPost(string postData, string url, string method = "POST") 
     { 
      string encodedCred = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(ConfAPI._confUser + ":" + ConfAPI._confPass)); 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
      request.Headers.Add("Authorization", "Basic " + encodedCred); 
      request.Headers.Add("X-Atlassian-Token", "no-check"); 
      request.Method = method; 
      request.Accept = "application/json"; 
      request.ContentType = "application/json"; 

      // request.KeepAlive = false; 
      if (postData != null) 
      { 
       byte[] data = Encoding.UTF8.GetBytes(postData); 
       request.ContentLength = data.Length; 
       using (var stream = request.GetRequestStream()) 
       { 
        stream.Write(data, 0, data.Length); 
       } 
      } 
      WebResponse response; 

      try 
      { 
       response = (HttpWebResponse)request.GetResponse(); 
      } 
      catch (WebException e) 
      { 
       return e.Message + " " + e.StackTrace.ToString(); 
      } 

      var responsestring = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
      return responsestring; 
     } 

    public class CreateContentWithParentJson 
    { 
     public string type { get; set; } 
     public string title { get; set; } 
     public List<Ancestor> ancestors { get; set; } 
     public Space space { get; set; } 
     public Body body { get; set; } 
    } 

internal static string _baseUrl = "http://localhost:8090/rest/api/content"; 
internal static string _jsonCreatePageWithParentSample = @"{'type':'page','title':'new page', 'ancestors':[{'id':456}], 'space':{'key':'TST'},'body':{'storage':{'value':'<p>This is a new page</p>','representation':'storage'}}}";