User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 
This is basically a webservice that will allow you to create bugs, tasks, and features in OpenProjects via the API call.
The approach here is to pass javascript object to a json call on your local website domain then the web services relays the request to the open projects server


change out:
project_to_add_to -- to you project
10.10.1.3 -- to your OpenProjects Web Server
basically you should be able to go to the baseAddress URL and see a json of all the work packages


Client Side Javascript


var userOpRequest = {username : '',userId : 0, subject :'', content : '', requestType : 7 };

//bind the form
userOpRequest.subject = $("#opSubject").val();
//etc..

var s = encodeURIComponent(JSON.stringify(userOpRequest ));
$.getJSON("OPWebService.asmx/fileBug?s=" + s, function (data) {

});


C# Web Service Code


using System.Text;
using System.Net;
using System.Web.Script.Services;
using System.Web.Services;
using System;
using System.Collections.Generic;

public class OPWebService : System.Web.Services.WebService
{

[WebMethod(EnableSession = false)]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
    public void fileBug(string s)
    {
        user_openproject_request openprojectsRequest = JsonConvert.DeserializeObject(s);

        string WorkPackageSubject = openprojectsRequest.subject;
        string WorkPackageDetails = openprojectsRequest.content;

        string TypeId = openprojectsRequest.requestType.ToString() ;
        string TypeName = openprojectsRequest.getTypeName() ;

        string ParentId = "9999";
        string ParentTitle = "Tile of parent";


//this next line is one line if your copying from web

string bugJson = "{ \"subject\": \""+ WorkPackageSubject + "\",\"description\": {\"format\": \"textile\",\"raw\": \""+ WorkPackageDetails + "\" }, \"_links\": {\"type\": { \"href\": \"/api/v3/types/" + TypeId + "\",\"title\": \"" + TypeName + "\" },\"status\": { \"href\": \"/api/v3/statuses/1\"}, \"priority\": {\"href\": \"/api/v3/priorities/8\",\"title\": \"Normal\" }, \"parent\":{\"href\":\"/api/v3/work_packages/"+ParentId+ "\",\"title\":\"" + ParentTitle + "\"} }}";//,\"assignee\": {\"href\": \"/api/v3/users/3\"}

		//start web connection
		var baseAddress = "http://10.10.1.5/api/v3/projects/project_to_add_to/work_packages/";
        var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));

        string userName = "apikey";
        string apiKey = "YOUR KEY"; // YourOpenProjectServer/my/access_token
        var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(userName + ":" + apiKey);
        string userCreds =  System.Convert.ToBase64String(plainTextBytes);
        http.Headers.Add("Authorization: Basic " + userCreds);
        http.Accept = "application/json";
        http.ContentType = "application/json";
        http.Method = "POST";

        string parsedContent = bugJson;
        ASCIIEncoding encoding = new ASCIIEncoding();
        Byte[] bytes = encoding.GetBytes(parsedContent);
        Stream newStream = http.GetRequestStream();
        newStream.Write(bytes, 0, bytes.Length);
        newStream.Close();
        
        System.Net.WebResponse response;
        var content = "";
        try
        {
            response = http.GetResponse();
            var stream = response.GetResponseStream();
            var sr = new StreamReader(stream);
            content = sr.ReadToEnd();
        }catch
        {

        }

        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.Write(content);
       
    }
	
}//end web serverice class
	
	
	
public class user_openproject_request
{
    public string username = "";
    public int userId = 0;
    public string subject = "";
    public string content = "";
    public int requestType = 7;
    public string urlOn = "";
    public string userURL = "";

    public string getTypeName()
    {
        string retVal = "Bug";
        if(requestType == 4) { retVal = "Feature"; }
        if (requestType == 1) { retVal = "Task"; }
        return retVal;
    }
}