aspx page :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="download.aspx.cs" Inherits="pdfdownload.download" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Namelabel" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:HyperLink ID="hyperdownload" runat="server">Download</asp:HyperLink>
<asp:Button ID="btndownload" runat="server" OnClick="btndownload_Click" Text="Download" />
</div>
</form>
</body>
</html>
C# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Xrm.Sdk;
//using pdfdownload.ServiceReference1;
using Xrm;
using Microsoft.Xrm.Sdk.Client;
using System.Net;
using System.ServiceModel.Description;
//using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Text;
using System.IO;
//using Microsoft.Crm.Sdk.Samples;
namespace pdfdownload
{
public partial class download : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btndownload_Click(object sender, EventArgs e)
{
ClientCredentials Credentials = new ClientCredentials();
Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
//This URL needs to be updated to match the servername and Organization for the environment.
Uri OrganizationUri = new Uri("http://win-qctbmlihah0:5555/CRM2013Vanilla/XRMServices/2011/Organization.svc");
Uri HomeRealmUri = null;
//OrganizationServiceProxy serviceProxy;
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
{
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
IOrganizationService service = (IOrganizationService)serviceProxy;
//Instantiate the contact object and populate the attributes.
string annotationID = "A9FE2781-FB3F-E411-971A-0800274C12EC";
// byte[] fileData = DownloadAttachment(annotationID);
string firstname = txtName.Text;
byte[] fileData = DownloadAttachment(firstname);
}
}
public byte[] DownloadAttachment(string AnnotationId)
{
byte[] newFile1 = new byte[10];
ClientCredentials Credentials = new ClientCredentials();
Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
//This URL needs to be updated to match the servername and Organization for the environment.
Uri OrganizationUri = new Uri("http://win-qctbmlihah0:5555/CRM2013Vanilla/XRMServices/2011/Organization.svc");
Uri HomeRealmUri = null;
Guid ObjectreferenceID = new Guid();
string referencenumber = AnnotationId;
//OrganizationServiceProxy serviceProxy;
using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
{
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
IOrganizationService service = (IOrganizationService)serviceProxy;
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "firstname";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add(referencenumber);
FilterExpression filter1 = new FilterExpression();
filter1.Conditions.Add(condition);
QueryExpression query = new QueryExpression("contact");
query.ColumnSet.AddColumns("firstname", "lastname","contactid");
query.Criteria.AddFilter(filter1);
EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
foreach (Entity entities in result1.Entities)
{
//EntityReference var1 = (EntityReference)entities.Attributes["contactid"];
ObjectreferenceID = entities.Id;
}
ConditionExpression AnnotationCondition = new ConditionExpression();
AnnotationCondition.AttributeName = "objectid";
AnnotationCondition.Operator = ConditionOperator.Equal;
AnnotationCondition.Values.Add(ObjectreferenceID);
FilterExpression AnnotationFilter = new FilterExpression();
AnnotationFilter.Conditions.Add(AnnotationCondition);
QueryExpression AnotationQuery = new QueryExpression("annotation");
AnotationQuery.ColumnSet.AddColumns("filename", "documentbody", "annotationid");
AnotationQuery.Criteria.AddFilter(AnnotationFilter);
EntityCollection AnnotationResult = serviceProxy.RetrieveMultiple(AnotationQuery);
Guid annotationId = new Guid();
foreach (Entity entities in AnnotationResult.Entities)
{
string filename = entities["filename"].ToString();
annotationId = entities.Id;
using (FileStream fileStream = new FileStream(filename, FileMode.OpenOrCreate))
{
byte[] newFile = Convert.FromBase64String(entities["documentbody"].ToString());
//EntityReference var1 = (EntityReference)entities.Attributes["annotationId"];
string nameoffile = filename;
// fileStream fs = fileStream.Read(newFile, 0, newFile.Length);
fileStream.Write(newFile, 0, newFile.Length);
fileStream.Read(newFile, 0, newFile.Length);
Response.ClearContent();
Response.Clear();
Response.BinaryWrite(newFile);
Response.AddHeader("Content-Disposition", "attachment; filename=" + nameoffile);
Response.End();
return newFile;
}
}
return newFile1;
}
}
}
}
in this we will see the byte coversion also.