Create a new item in a process with context data



I am trying to programmatically raise new items in a process with a context from c#. I have used the example code from the documentation in resources with a couple of small changes. I persuaded the request to use my default credentials with the following three lines

       httpWebReq.UseDefaultCredentials = true;
        httpWebReq.PreAuthenticate = true;
        httpWebReq.Credentials = CredentialCache.DefaultCredentials;

This gets me past the authentication messages I got earlier but now I get a response with some redirection errors and no item is created.

\nRedirection error\n\n\n\n Redirection error The requested page cannot be found

And here is my code

      ContextParameters myCPs = new ContextParameters();
        ContextParameter myParam = new ContextParameter();
        myParam.Value = "Simon Breeze";
        myParam.Direction = ContextParameter.Directions.In;
        string myContext = myCPs.GetXml();

        string apURL = Url + "&L=" + Language + "&ProcessName=" + ProcessName
            + "&RequesterName=simon.breeze&Test=" + Test;
        ////System.Net.NetworkCredential mycred = new System.Net.NetworkCredential
        ////        (UserName, Password, UserDomain);
        //System.Net.NetworkCredential mycred = new System.Net.NetworkCredential
        //       (UserName, Password, UserDomain);
        //System.Net.CredentialCache myCache = new System.Net.CredentialCache();
        //myCache.Add(new Uri(Url),"Windows",mycred);

        //Prepare request
        System.Net.HttpWebRequest httpWebReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(apURL);
        httpWebReq.Method = "Post";

        ////Set Authentication
        //httpWebReq.Credentials = myCache;

        httpWebReq.UseDefaultCredentials = true;
        httpWebReq.PreAuthenticate = true;
        httpWebReq.Credentials = CredentialCache.DefaultCredentials;

        //Set Context info
        string postData = "CONTEXT=" +  WebUtility.UrlEncode(myContext);
        System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
        Byte[] Buffer = encoding.GetBytes(postData);

        //Set the contenttype of the data being posted
        httpWebReq.ContentType = "application/x-www-form-urlencoded";
        httpWebReq.ContentLength = postData.Length;

        //Send the context
        System.IO.Stream myStream = httpWebReq.GetRequestStream();
        myStream.Write(Buffer, 0, Buffer.Length);
        HttpWebResponse httpWebResp = null ;

             httpWebResp = (System.Net.HttpWebResponse)httpWebReq.GetResponse();
        catch(WebException ex)

        string respCode = "OK"; // response code variable

        // Check the response status code is OK
        if (respCode != httpWebResp.StatusCode.ToString())
            // Display the Error details
            MessageBox.Show("Error:" + httpWebResp.StatusCode);
            // Gets the stream associated with the response
            httpWebResp = (System.Net.HttpWebResponse)httpWebReq.GetResponse();
            System.IO.StreamReader myReader = new System.IO.StreamReader(httpWebResp.GetResponseStream());
            string response = myReader.ReadToEnd();

            // Display response message

Thanks for any help.


Hi @simon.breeze,

Which documentation did you refer to? In the apURL string, the “L” query parameter is not necessary and the “RequesterName” should not be there. The requester should be identified in the network credentials defined in httpWebReq.Credentials.



The documentation I referred to was at I didn’t see your reply in time and the solution I have used is to move it all into WorkflowGen and call StartProcess instead. That works but was a bit of a headache as I lost all my lovely Linq code to read data from our old system and had to mess around with Commands and DataReaders instead.

Not a waste of time though as I feel I could try using more of the APIs to boost the power of our workflows.