How to send a requestdocument from a service

Yesterday I was sitting with a potential partner here at MDCC (Microsoft Development Center Copenhagen) to get them introduced to Microsoft Dynamics Mobile. All in all I think we had a good and productive day where we covered most of the major topics and gave the partner a good kick start on their development of a mobile application.

Looking back I can however see that there were a couple of things that we might explain better. I’ll try to write some posts about this as time progress.

The first thing I’d like to address is "How to send a requestdocument from a service"

Until now we, at Dynamics Mobile, have only had scenarios where we collect data from multiple tasklets and put this into a request document. Working with NaviParner I could see that there are several scenarios where you might wish to send data from a service.

Creating a request document in code 

A request document is wrapped in a RequestDocumentDefinition which takes name as parameter.

RequestDocumentDefinition definition =

   new RequestDocumentDefinition("requestDocumentFromCode" /* name */);

definition.Inherit = false; // Inherrit other request documents

definition.Stamps = 10; // Stamps to put on the document

definition.Text = "My request document"; // Userfriendly text show i.e. in request document status

Secondly we need to create the actual request to send.

RequestDocument document = new RequestDocument(definition);

document.AddRequest(new RequestFromCode("my data as a string"));

requestDocumentService.Submit(document);

First line creates the request document using the request document definition. In the 2nd line we add data to the request document using a custom object called RequestFromCode which takes a string in the constructor. And finally we submit the request document to the RequestDocumentService.

The RequestFromCode object is a simple data object that implements IRequestContributor.

public class RequestFromCode : IRequestContributor

{

   private readonly string data;

   public RequestFromCode(string data)

   {

      this.data = data;

   }

   public void CreateRequest(XmlWriter writer)

   {

      writer.WriteStartElement("requestDocumentFromCode");

      writer.WriteAttributeString("data", data);

      writer.WriteEndElement();

   }

   public string Name

   {

      get { return "RequestFromCode"; }

   }

}

The request document needed to be submitted to the requestDocumentService. In order access the service the following peace of code can be inserted in the tasklet or service where the request document from code is required

private IRequestDocumentService requestDocumentService;

[RolePadService]

public IRequestDocumentService RequestDocumentService

{

   get { return requestDocumentService; }

   set { requestDocumentService = value; }

}

Hope you found this post interesting.