Approach
1. Create an ASPX page to do the below
a. Get the EntityId
b. Select the notes for the EntityId
c. Get the document for the notes
d. Send the document to the default printer. [make sure that the default printer is set]
Code Example – Please edit the code as per the need
public partial class _Default : System.Web.UI.Page
{
CrmService service;
protected void Page_Load(object sender, EventArgs e)
{
CrmServiceInititate();
//string entityId = Request.QueryString["id"];
//[TESTING]
string entityId = "E1FEE803-1FE9-DE11-8B94-02BF92D77DD6";
ReadAnnotationsforEntity(entityId);
}
//READ THE ANNOTATIONS FOR AN ENTITY
public void ReadAnnotationsforEntity(string entityId)
{

string result = service.Fetch(fetch);
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(result);
XmlNodeList xndl = xDoc.SelectNodes("resultset/result");
string annotationId = string.Empty;
foreach (XmlNode xnd in xndl)
{
annotationId = xnd.SelectSingleNode("annotationid").InnerText;
ReadAttachedFilesinAnnotation(annotationId, entityId);
//break;
}
}
//ANNOTATION - NOTE
public void ReadAttachedFilesinAnnotation(string annotationId, string entityId)
{
//get the attached files for a note in an entity.
// Define the columns to retrieve from the annotation record.
ColumnSet cols1 = new ColumnSet();
cols1.Attributes = new string[] { "filename", "documentbody" };
// Retrieve the annotation record.
annotation annotationAttachment = (annotation)service.Retrieve(EntityName.annotation.ToString(), new Guid(annotationId), cols1);
// Download the attachment in the current execution folder.
string url = Server.MapPath(".\\Attachments");
string newPath = System.IO.Path.Combine(url, entityId);
System.IO.Directory.CreateDirectory(newPath);
url = newPath + "\\" + annotationAttachment.filename;
//using (FileStream fileStream = new FileStream(annotationAttachment.filename, FileMode.OpenOrCreate))
using (FileStream fileStream = new FileStream(url, FileMode.OpenOrCreate))
{
byte[] fileContent = new UTF8Encoding(true).GetBytes(annotationAttachment.documentbody);
fileStream.Write(fileContent, 0, fileContent.Length);
}
//SendFileDefaultPrinter(url);
}
public void SendFileDefaultPrinter(string url)
{
//THIS WILL PRINT ALL THE FORMATS
//BUT MAKE SURE THAT THE DEFAULT PRINTER IS SELECTED
Process printJob = new Process();
printJob.StartInfo.FileName = url;
printJob.StartInfo.UseShellExecute = true;
printJob.StartInfo.Verb = "print"; //THIS SHOULD ALWAYS BE PRINT AS PRINT IS THE ACTION THAT HAS TO BE DONE NOW
printJob.Start();
}
public void CrmServiceInititate()
{
// Set up the CRM Service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = AuthenticationType.AD;
token.OrganizationName = <
//******************UAT in same domain ********************************************************************
service = new CrmService();
service.CrmAuthenticationTokenValue = token;
//service.Credentials = System.Net.CredentialCache.DefaultCredentials;
service.Credentials = new System.Net.NetworkCredential(<
}
}
2. Host the above application Host the Custom Application (Ex: ISV\CHALLITS\CRMExtended\WebApplications\SendDoctoPrinter\Default.aspx)
3. Create an ISV button on the entity to call the ASPX Page [you can do this on Button URL]
No comments:
Post a Comment