API example for Crystal Reports

Discussions concerning general integration topics.

Moderator: Phil Barton

Post Reply
BobRichards
Posts: 1377
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

API example for Crystal Reports

Post by BobRichards »

This post will provide a generalized method to handle creating system reports that are generated by Crystal. It will demonstrate how to resolve the "CONTEXTID#" render prompt by using a report where a CDF context is required. You will handle the user prompts the same way as any report - through the IPrompt mechanism demonstrated in the RenderPrompts class.

The method to save the generated system document is not duplicated here and is in another post: 4.1 API example for docs/reports
Bob Richards, Senior Software Developer, SoftPro
BobRichards
Posts: 1377
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: API example for Crystal Reports

Post by BobRichards »

The Crystal report has a context of {{Order.CDFs}}. When Select renders the document from the UI screen, it will provide a dialog allowing the user to select the CDFs you wish to print - should there be more than one CDF. I this example, we are just going to render the report for the first CDF. Here we determine the values for the report required order and CDF (context) ids.

Code: Select all

// Open the Order.
IOrderStore os = ss.GetService<IOrderStore>();
IOrderInfo search = os.Orders.Where(x => x.Number == "2016090342").FirstOrDefault();
IOrder order = os.OpenOrder(search, true);

// Get document.
IDocumentManager dm = ss.GetService<IDocumentManager>();
IDocumentInfo docInfo = dm.Documents
   .Where(t => t.Title == "Closing Disclosure Form, Pages 1-5")
   .FirstOrDefault();

// Create renderer responses.
Dictionary<string, object> _responses = new Dictionary<string, object>();
_responses.Add("ROOTID#", Int32.Parse(((IOrder)order).GetTag("ROOT_ID")));

IList cdfs = (IList)order["CDFs"];
IOrderItem cdf = (IOrderItem)cdfs[0];
_responses.Add("CONTEXTID#", Int32.Parse(cdf.GetTag("PERSIST_ID")));  // Use this tag for all contexts other than the Order.

// Pass response to response handler and render document.
IRendererFactory rendererFactory = ss.GetService<IRendererFactory>();
IRenderer renderer = rendererFactory.Create();
RenderPrompts prompts = new RenderPrompts(_responses);
using (IRendering rendering = renderer.Render(docInfo, null, prompts))
{
	// Save new doc...
}
Now we pass the IDs to the renderer engine:

Code: Select all

class RenderPrompts : IPrompt<PromptEventArgs>
{
	private Dictionary<string, object> _responses;

	// Pass in desired responses for requested prompt IDs.
	public RenderPrompts(Dictionary<string, object> responses)
	{
		_responses = responses;
	}

	public void Request(PromptEventArgs value)
	{
		foreach (IRequest prompt in value.Requests)
		{
			if (_responses.ContainsKey(prompt.ID))
			{
				object answer = _responses[prompt.ID];

				IValueRequest response = (IValueRequest)prompt;
				response.Handled = true;
				response.Value = answer;
			}
		}
	}
}
Bob Richards, Senior Software Developer, SoftPro
Post Reply