Article summary

Summary
Discusses how to create a C# .NET client application that submits data to the Adobe Experience Manager JCR.  
Digital Marketing Solution(s) Adobe Experience Manager (Adobe CQ)
Audience
Developer (intermediate)
Required Skills
C#, .NET framework, HTML
Tested On Experience Manager 5.5, 5.6

Introduction

You can create a .NET client application that can modify Experience Manager JCR nodes and properties by using a Restful Sling request. The .NET application can create a node, modify an existing node, and delete a node. For example, assume that you responsible for building an application for an insurance company interested in persisting claim data in the Experience Manager JCR. When an end user fills in the form and clicks the submit button, the .NET application data is submitted to Experience Manager. A new node is created and the data is stored as node properties.

Dot Net Sling

When the user fills in this .NET application and clicks the Submit Claim to JCR button, claim data is submitted to the CQ JCR.

JCR


Because Experience Manager accepts Restful requests, you are not limited to the type of client technology to use to submit data. That is, you can use any client technology that supports Restful requests. This development article walks you through how to build this .NET client application that can submit data to the Experience Manager JCR. 

It is recommended that an application that posts data to an Experience Manager publish instance (as opposed to an Author instance), post the data to /content/usergenerated/content/<site>/. This also allows for reverse replication and handled by a workflow on the Author instance. The Experience Manager location /content/usergenerated/ allows content creation from an anonymous CRX session. Other Experience Manager repository locations require an admin session (or an account that has admin privileges). 

Note:

Although this recommendation suggets placing the data in /content/usergenerated/content/ for production applications, to keep this example application simple, the data in this development article is posted to /content/claim/. To authenticate each request, the values admin, admin are used for both username and password (the default CQ username and password).   

Note:

For information about modifying the Experience Manager JCR from a mobile application created by using JQuery Mobile API, see Using Sling Post Servlets to submit mobile data to Experience Manager

Create the Visual Studio Windows Forms Application

The first step is to create a C# .NET Windows Forms Application. The following illustration shows the client application developed in this development article.

 

Dot Net Sling2

The following table lists the controls that are part of this client application.

Control name Description
textBoxClaim Displays the claim number when the data is submitted to the AEM JCR.
comboBoxStatus Specifies whether the claim is open or closed. This value is changed when the Update Claim button is clicked. When a new claim is posted, the default value is Open.
textBoxFirst Specifies the first name of the user who made the claim.
dateTimePicker1 Specifies the date on which the claim is made.  
textBoxMiddle Specifies the middle name of the user who made the claim.
textBoxLast Specifies the last name of the user who made the claim.
comboBoxCat Specifies the type of claim. For this development article, the values are either Home, Boat, or Auto.  
textBoxAddress Specifies the address of the person whom made the claim.
textBoxCity Specifies the city in which the person whom made the claim lives.
comboBoxState Specifies the state in which the person whom made the claim lives.
textBoxDetail Specifies the additional details about the claim.
buttonPost Allows the user to post data to AEM JCR.
buttonUpdate Allows the status of the claim to be modified. In this development article, you can change the claim from Open to Close by selecting Close from the comboBoxStatus.
buttonDelete Deletes the claim. Enter the cliam number in the Claim Number box and click this button. The claim node is removed from the AEM JCR.
Button Close Closes the client application.  

To create the Visual Studio windows forms application, perform these tasks:

  1. Start Visual Studio (the version used in this development article is Visual Studio 2010).
  2. Click File, New, Project.
  3. Under Visual C#, select Windows Forms Application.
  4. Modify the client application to reflect the previous illustation.

 

Post data to the Experience Manager JCR using the .NET client application

You can post data from the .NET client application to the Experience Manager JCR by using a Sling request. To post data from the .NET client, use a HttpWebRequest instance. First off,  specify the URI to where data is posted.In this example, data is posted to /content/claim.

The following code shows how to create a HttpWebRequest instance using a System.Uri instance. The System.Uri instance defines the Experience Manager JCR location to where the data is posted.  

 //Define the Experience Manager JCR location to where the data is posted
 String postURL = "http://localhost:4502/content/claim/newClaim_" + claimID;
 System.Uri uri = new System.Uri(postURL);

 //Create the HttpWebRequest instance
 HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(uri);

When a Post request is sent to Experience Manager, the request must be authenticated. To authenticate the Post request from the .NET client application, you can use a System.Net.NetworkCredential instance. When creating this object, use the constructor that lets you specify the user name and corresponding password.

The System.Net.NetworkCredential instance is assigned to the Credentials property of the HttpWebRequest instance, as shown in the following code example.

//Specify authentication information
NetworkCredential nc = new NetworkCredential("admin","admin");

//Specify properties of the HttpWebRequest instance 
//including authentication information 
httpWReq.Method = "POST";
httpWReq.Credentials = nc;
httpWReq.ContentType = "application/x-www-form-urlencoded";

Once you create the HttpWebRequest instance and set its properties, you can prepare the data to send as part of the Post request. In this client application example, the data is read from the user-defined values. After prepare the data, you can post the data, as shown in the followiing code example.   

//Get user-defined values from the client
 String postData = "id=" +claimID ;
 postData += "&firstName=" + firstName;
 postData += "&lastName=" + lastName;
 postData += "&address=" + address;
 postData += "&middle=" + middleName;
 postData += "&status=Open";
 postData += "&cat=" + cat;
 postData += "&city=" + city;
 postData += "&state=" + state;
 postData += "&date=" + date;
 postData += "&details=" + details;
  byte[] data = encoding.GetBytes(postData);

 //Specify the content lenth of the Post
 httpWReq.ContentLength = data.Length;

           
 using (System.IO.Stream stream = httpWReq.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }

  HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

The following C# code example represents the buttonPost_Click method that is invoked when the user clicks the Submit claim to JCR button.

 //Post data to Adobe CQ JCR
 private void buttonPost_Click(object sender, EventArgs e)
 {

    //Create the unique claim identifier value
    String claimID = System.Guid.NewGuid().ToString();

    //Get the user-defined values
    String firstName = textBoxFirst.Text;
    String lastName = textBoxLast.Text;
    String cat = comboBoxCat.Text;
    String address = textBoxAddress.Text;
    String city = textBoxCity.Text;
    String state = comboBoxState.Text;
    String details = textBoxDetail.Text;
    String middleName = textBoxMiddle.Text;
    String date = dateTimePicker1.Text; 

    //Define the Adobe CQ JCR location to where the data is posted
    String postURL = "http://localhost:4502/content/claim/newClaim_" + claimID;
    System.Uri uri = new System.Uri(postURL);

    //Create the HttpWebRequest instance
    HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(uri);
    ASCIIEncoding encoding = new ASCIIEncoding();

    //Specify authentication information
    NetworkCredential nc = new NetworkCredential("admin","admin");

    //Specify properties of the HttpWebRequest instance 
    //including authentication information 
    httpWReq.Method = "POST";
    httpWReq.Credentials = nc;
    httpWReq.ContentType = "application/x-www-form-urlencoded";
           
    //Get user-defined values from the client
    String postData = "id=" +claimID ;
    postData += "&firstName=" + firstName;
    postData += "&lastName=" + lastName;
    postData += "&address=" + address;
    postData += "&middle=" + middleName;
    postData += "&status=Open";
    postData += "&cat=" + cat;
    postData += "&city=" + city;
    postData += "&state=" + state;
    postData += "&date=" + date;
    postData += "&details=" + details;
    byte[] data = encoding.GetBytes(postData);

    //Specify the content lenth of the Post
    httpWReq.ContentLength = data.Length;

   
    using (System.IO.Stream stream = httpWReq.GetRequestStream())
    {
            stream.Write(data, 0, data.Length);
    }

    HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

    //Get the response - for debugging purposes
    String responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();

    comboBoxStatus.Text = "Open";
    textBoxClaim.Text = claimID; 
}

Modify data located in the Experience Manager JCR using the .NET client application 

You can modify data located in the Experience Manager JCR using the .NET client application. In this example, the status propety of the claim node is modified by changing its value from Open to Close. To modify the value of a node property, send another Post request. Specify the same endpoint (for example, the same node with the correct identifer value) and specify only the property to modify along with the new value.

The following C# code represents the method that is called when the Update Claim button is clicked.

private void buttonUpdate_Click(object sender, EventArgs e)
{

//This method updates the specific claim and changes the status
String status = comboBoxStatus.Text;
String claimID = textBoxClaim.Text;

//Define the Adobe CQ JCR location to where the data is posted
String postURL = "http://localhost:4502/content/claim/newClaim_" + claimID;
System.Uri uri = new System.Uri(postURL);

//Create the HttpWebRequest instance
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(uri);
ASCIIEncoding encoding = new ASCIIEncoding();

//Specify authentication information
NetworkCredential nc = new NetworkCredential("admin", "admin");

//Specify properties of the HttpWebRequest instance 
//including authentication information 
httpWReq.Method = "POST";
httpWReq.Credentials = nc;
httpWReq.ContentType = "application/x-www-form-urlencoded";

//Get the status change from the client
String postData = "status=" + status;
byte[] data = encoding.GetBytes(postData);

//Specify the content lenth of the Post
httpWReq.ContentLength = data.Length;
            
using (System.IO.Stream stream = httpWReq.GetRequestStream())
{
        stream.Write(data, 0, data.Length);
}

HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
              
//Get the response - for debugging purposes
String responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
MessageBox.Show("You have changed claim " +claimID +" to "+status); 
    }

Delete data located in the Adobe JCR using the .NET client application  

The final task covered in this development article is how to delete data from the Experience Manager JCR using the .NET client application. To delete data, you specify another request. The following C# code represents the buttonDelete_Click method that is invoked when the Delete Claim button is clicked.

private void buttonDelete_Click(object sender, EventArgs e)
   {
   //This method deletes a claim from the Adobe JCR
    String claimID = textBoxClaim.Text;

    String postURL = "http://localhost:4502/content/claim/newClaim_" + claimID;
    System.Uri uri = new System.Uri(postURL);
    HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(uri);
           
    ASCIIEncoding encoding = new ASCIIEncoding();
    NetworkCredential nc = new NetworkCredential("admin", "admin");

    String postData = "name=':operation'&type='hidden'&value='delete'";
    byte[] data = encoding.GetBytes(postData);

    httpWReq.Method = "DELETE";
    httpWReq.Credentials = nc;
    httpWReq.ContentType = "application/x-www-form-urlencoded";
    httpWReq.ContentLength = data.Length;

    using (System.IO.Stream stream = httpWReq.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

    HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
    MessageBox.Show("Claim "+claimID +" is deleted");
    }

Note:

When viewing claim data (the data created in this development article) in CRXDE Lite, refresh the parent node to see the changes. To refresh the node, simply right click on the node and select Refresh. WHen deleting a node, always refresh the parent node.   

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy