1.Keep this file inside ISV forlder
Download.aspx.cs
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 Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Query;
public partial class Download : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string lclAnnotationId = string.Empty;
Guid lclAnnotationGuid = Guid.Empty;
IOrganizationService lclCrmService = null;
Entity lclAnnotation = null;
string lclFileName;
if (Request.QueryString["fid"] != null)
{
lclAnnotationGuid = new Guid(Request.QueryString["fid"]);
}
lclCrmService = GetCRMServiceObject("administrator", "arif@1234", "mol", "OrgName", "http://win-dfk6m8db6gg:5555");
if (lclAnnotationGuid != Guid.Empty && lclAnnotationGuid != null)
{
lclAnnotation = lclCrmService.Retrieve("annotation", lclAnnotationGuid, new ColumnSet(new string[] { "filename", "documentbody" }));
if (lclAnnotation.Contains("filename") && lclAnnotation.Contains("documentbody"))
{
lclFileName = lclAnnotation["filename"].ToString();
//FileStream fileStream = new FileStream(lclFileName, FileMode.OpenOrCreate);
Byte[] fileContent = Convert.FromBase64String(lclAnnotation["documentbody"].ToString());
//fileStream.Write(fileContent, 0, fileContent.Length);
// Clear all content output from the buffer stream
Response.Clear();
// Add a HTTP header to the output stream that specifies the default filename
// for the browser's download dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + lclFileName);
// Add a HTTP header to the output stream that contains the
// content length(File Size). This lets the browser know how much data is being transfered
Response.AddHeader("Content-Length", fileContent.Length.ToString());
// Set the HTTP MIME type of the output stream
// Response.ContentType = "application/octet-stream";
///=/ Write the data out to the client.
Response.BinaryWrite(fileContent);
}
}
}
private IOrganizationService GetCRMServiceObject(string prmUserName, string prmPassword, string prmDomain, string prmOrgName, string prmServergUrl)
{
OrganizationServiceProxy lclCrmServiceProxy = null;
IOrganizationService lclCrmService = null;
ClientCredentials lclClientCredentials = new ClientCredentials();
Uri lclOrganizationUri = null;
string lclCRMServiceUrl = string.Empty;
try
{
lclClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(prmUserName, prmPassword, prmDomain);
lclCRMServiceUrl = string.Concat(prmServergUrl, "/", prmOrgName, "/XRMServices/2011/Organization.svc");
lclOrganizationUri = new Uri(lclCRMServiceUrl);
lclCrmServiceProxy = new OrganizationServiceProxy(lclOrganizationUri, null, lclClientCredentials, null);
lclCrmService = (IOrganizationService)lclCrmServiceProxy;
}
catch (Exception ex)
{
Response.Write("Error details : " + ex.Message);
}
return lclCrmService;
}
}
Download.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Download.aspx.cs" Inherits="Download" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Download.aspx.cs
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 Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Query;
public partial class Download : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string lclAnnotationId = string.Empty;
Guid lclAnnotationGuid = Guid.Empty;
IOrganizationService lclCrmService = null;
Entity lclAnnotation = null;
string lclFileName;
if (Request.QueryString["fid"] != null)
{
lclAnnotationGuid = new Guid(Request.QueryString["fid"]);
}
lclCrmService = GetCRMServiceObject("administrator", "arif@1234", "mol", "OrgName", "http://win-dfk6m8db6gg:5555");
if (lclAnnotationGuid != Guid.Empty && lclAnnotationGuid != null)
{
lclAnnotation = lclCrmService.Retrieve("annotation", lclAnnotationGuid, new ColumnSet(new string[] { "filename", "documentbody" }));
if (lclAnnotation.Contains("filename") && lclAnnotation.Contains("documentbody"))
{
lclFileName = lclAnnotation["filename"].ToString();
//FileStream fileStream = new FileStream(lclFileName, FileMode.OpenOrCreate);
Byte[] fileContent = Convert.FromBase64String(lclAnnotation["documentbody"].ToString());
//fileStream.Write(fileContent, 0, fileContent.Length);
// Clear all content output from the buffer stream
Response.Clear();
// Add a HTTP header to the output stream that specifies the default filename
// for the browser's download dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + lclFileName);
// Add a HTTP header to the output stream that contains the
// content length(File Size). This lets the browser know how much data is being transfered
Response.AddHeader("Content-Length", fileContent.Length.ToString());
// Set the HTTP MIME type of the output stream
// Response.ContentType = "application/octet-stream";
///=/ Write the data out to the client.
Response.BinaryWrite(fileContent);
}
}
}
private IOrganizationService GetCRMServiceObject(string prmUserName, string prmPassword, string prmDomain, string prmOrgName, string prmServergUrl)
{
OrganizationServiceProxy lclCrmServiceProxy = null;
IOrganizationService lclCrmService = null;
ClientCredentials lclClientCredentials = new ClientCredentials();
Uri lclOrganizationUri = null;
string lclCRMServiceUrl = string.Empty;
try
{
lclClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(prmUserName, prmPassword, prmDomain);
lclCRMServiceUrl = string.Concat(prmServergUrl, "/", prmOrgName, "/XRMServices/2011/Organization.svc");
lclOrganizationUri = new Uri(lclCRMServiceUrl);
lclCrmServiceProxy = new OrganizationServiceProxy(lclOrganizationUri, null, lclClientCredentials, null);
lclCrmService = (IOrganizationService)lclCrmServiceProxy;
}
catch (Exception ex)
{
Response.Write("Error details : " + ex.Message);
}
return lclCrmService;
}
}
Download.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Download.aspx.cs" Inherits="Download" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
FileUpload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUpload.aspx.cs" Inherits="FileUpload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<head>
<base>
<meta charset="utf-8">
<script language="javascript">
function DownloadFile() {
var lclUploadControl = document.getElementById("jj_fileupload");
frmFileUpload.reset();
}
function OpenFile() {
var Url, lclFileGuidField;
lclFileGuidField = parent.Xrm.Page.data.entity.attributes.get(frmFileUpload.gits_FieldName.value);
Url = "download.aspx?fid=" + lclFileGuidField.getValue();
window.open(Url, "File_Download");
}
function loadUrl(url) { window.open(url, '_blank', 'scrollbar=yes'); }
function ValidateFileStatus() {
var lclFileGuidField, jj_FormType;
jj_FormType = parent.Xrm.Page.ui.getFormType();
if (jj_FormType == "1") {
if (document.all.gits_DownloadButton != null) {
//document.all.gits_DownloadButton.style.display = "none";
}
}
lclFileGuidField = parent.Xrm.Page.data.entity.attributes.get(frmFileUpload.gits_FieldName.value);
if (lclFileGuidField != null && frmFileUpload.gits_UploadedfileGUID.value != "" &&
frmFileUpload.gits_UploadedfileGUID.value != null) {
if (lclFileGuidField.getValue() == null || lclFileGuidField.getValue == "") {
lclFileGuidField.setValue(frmFileUpload.gits_UploadedfileGUID.value);
document.all.gits_DownloadButton.style.display = "block";
}
else if (lclFileGuidField.getValue() != frmFileUpload.gits_UploadedfileGUID.value) {
lclFileGuidField.setValue(frmFileUpload.gits_UploadedfileGUID.value);
}
}
if (frmFileUpload.gits_UploadedfileGUID.value == "" && lclFileGuidField.getValue() != null) {
frmFileUpload.gits_UploadedfileGUID.value = lclFileGuidField.getValue();
}
if (lclFileGuidField.getValue() == null) {
document.all.gits_DownloadButton.style.display = "none";
}
}
function ScanDocument() { alert('Scanner not found, please make sure that scanner is attached to the system.'); }
function UploadFile() {
var lclFileUploadGuid = frmFileUpload.gits_UploadedfileGUID;
if (frmFileUpload.jj_fileupload.value != null && frmFileUpload.jj_fileupload.value != "") {
if (lclFileUploadGuid.value != null && lclFileUploadGuid.value != "") {
if (!confirm("Overwrite exsisting file?")) {
document.all.LinkTD.innerText = "";
return;
}
}
else {
if (!confirm("Upload File?")) {
document.all.LinkTD.innerText = "";
return;
}
}
if (document.all.LinkTD != null) {
document.all.LinkTD.innerText = "Uploading...";
}
frmFileUpload.submit();
}
}
</script>
<style type="text/css">
.table
{
background-color: #f6f8fa;
margin: 0px;
padding: 0px;
border: 0px;
}
.link
{
font-family: Segoe UI, Tahoma, Arial;
font-size: 11px;
vertical-align: middle;
font-weight: bold;
text-decoration: underline;
color: Red;
cursor: auto;
}
.image
{
vertical-align: middle;
}
.fileUpload
{
width: 211px;
height: 21px;
margin: 0px;
padding: 0px;
vertical-align: middle;
left: -3px;
position: relative;
top: 0px;
}
.Url
{
cursor: hand;
}
</style>
</head>
</head>
<body onload="ValidateFileStatus();" class="table">
<form id="frmFileUpload" runat="server">
<table style="position: relative; top: -3px; height: 15px;" class="table">
<tbody class="table">
<tr id="newform">
<td>
<asp:FileUpload ID="jj_fileupload" class="fileUpload" value="..." type="file" runat="server" />
</td>
<td class="Url">
<img class="image" onclick="UploadFile();" alt="Document" src="http://mol-testits3:88/ISV/File_Upload/Upload_Icon.png"
title="Click here to upload file." />
</td>
<td class="Url">
<asp:ImageButton class="image" ID="gits_DownloadButton" runat="server" alt="Document"
ToolTip="Click here to download file." src="http://mol-testits3:88/ISV/File_Upload/Download_Icon.png"
OnClick="gits_DownloadButton_Click" />
</td>
<td class="Url">
<asp:ImageButton class="image" ID="gits_OpenFileButton" runat="server" alt="Document"
ToolTip="Click here to open file." src="http://mol-testits3:88/ISV/File_Upload/Open_Icon.png" />
</td>
<td id="LinkTD" class="link" runat="server" valign="middle">
</td>
</tr>
</TR></tbody></table>
<input type="hidden" runat="server" id="gits_UploadedfileGUID" />
<input type="hidden" runat="server" id="gits_FieldName" />
</form>
</body>
</html>
FileUpload.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel.Description;
using System.Security.Principal;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Text;
using System.IO;
public partial class FileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region Query String Variables
string lclOrgId = string.Empty;
string lclOrgLcid = string.Empty;
string lclOrgName = string.Empty;
string lclEntityId = string.Empty;
string lclEntityTypeCode = string.Empty;
string lclEntityTypeName = string.Empty;
string lclUserLcid = string.Empty;
string lcldfidfieldName = string.Empty;
#endregion
IOrganizationService lclCrmService = null;
LoadQueryStringParameters(ref lclOrgLcid, ref lclOrgName, ref lclEntityId, ref lclEntityTypeCode
, ref lclEntityTypeName, ref lclUserLcid, ref lcldfidfieldName);
lclCrmService = GetCRMServiceObject("administrator", "arif@1234", "mol", "TrainingInstitutionsAffairs", "http://win-dfk6m8db6gg:5555");
jj_fileupload.Attributes.Add("onchange", "UploadFile();");
gits_DownloadButton.Attributes.Add("onclick", "DownloadFile()");
gits_OpenFileButton.Attributes.Add("onclick", "OpenFile()");
if (IsPostBack)
{
if (jj_fileupload.HasFile)
{
Entity lclAnnotation = new Entity("annotation");
lclAnnotation.Attributes.Add("subject", "Test File");
lclAnnotation.Attributes.Add("filename", jj_fileupload.FileName);
lclAnnotation.Attributes.Add("documentbody", Convert.ToBase64String(jj_fileupload.FileBytes));
//lclAnnotation.Attributes.Add("mimetype", "text/plain");
if (!string.IsNullOrEmpty(lclEntityId))
lclAnnotation.Attributes.Add("objectid", new EntityReference(lclEntityTypeName, new Guid(lclEntityId)));
Guid notesId = lclCrmService.Create(lclAnnotation);
if (notesId != Guid.Empty)
{
LinkTD.InnerText = "Uploaded";
gits_UploadedfileGUID.Value = notesId.ToString();
}
}
}
else
{
if (!string.IsNullOrEmpty(lclEntityId))
{
Entity lclfileEntity = null;
lclfileEntity = lclCrmService.Retrieve(lclEntityTypeName, new Guid(lclEntityId)
, new Microsoft.Xrm.Sdk.Query.ColumnSet(new string[] { lcldfidfieldName }));
if (lclfileEntity.Contains(lcldfidfieldName))
{
gits_DownloadButton.Visible = true;
gits_OpenFileButton.Visible = true;
}
else
{
gits_DownloadButton.Visible = false;
gits_OpenFileButton.Visible = false;
}
}
}
}
private void LoadQueryStringParameters(ref string lclOrgLcid, ref string lclOrgName, ref string lclEntityId, ref string lclEntityTypeCode, ref string lclEntityTypeName, ref string lclUserLcid, ref string lcldfidfieldName)
{
if (Request.QueryString["fidfieldname"] != null)
{
lcldfidfieldName = Request.QueryString["fidfieldname"];
}
//else
//{
// lcldfidfieldName = "new_1_otherdocument_id";
//}
if (Request.QueryString["id"] != null)
{
lclEntityId = Request.QueryString["id"];
}
//else
//{
// lclEntityId = "{95C1EDC6-6B9F-E011-936A-E6FEC45CFF9A}";
//}
if (Request.QueryString["orglcid"] != null)
{
lclOrgLcid = Request.QueryString["orglcid"];
}
if (Request.QueryString["orgname"] != null)
{
lclOrgName = Request.QueryString["orgname"];
}
//else
//{
// lclOrgName = "KPMGFakhro";
//}
if (Request.QueryString["type"] != null)
{
lclEntityTypeCode = Request.QueryString["type"];
}
//else
//{
// lclEntityTypeCode = "10005";
//}
if (Request.QueryString["typename"] != null)
{
lclEntityTypeName = Request.QueryString["typename"];
}
//else
//{
// lclEntityTypeName = "gits_application";
//}
if (Request.QueryString["userlcid"] != null)
{
lclUserLcid = Request.QueryString["userlcid"];
}
gits_FieldName.Value = lcldfidfieldName;
}
private IOrganizationService GetCRMServiceObject(string prmUserName, string prmPassword, string prmDomain, string prmOrgName, string prmServergUrl)
{
OrganizationServiceProxy lclCrmServiceProxy = null;
IOrganizationService lclCrmService = null;
ClientCredentials lclClientCredentials = new ClientCredentials();
Uri lclOrganizationUri = null;
string lclCRMServiceUrl = string.Empty;
try
{
lclClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(prmUserName, prmPassword, prmDomain);
lclCRMServiceUrl = string.Concat(prmServergUrl, "/", prmOrgName, "/XRMServices/2011/Organization.svc");
lclOrganizationUri = new Uri(lclCRMServiceUrl);
lclCrmServiceProxy = new OrganizationServiceProxy(lclOrganizationUri, null, lclClientCredentials, null);
lclCrmService = (IOrganizationService)lclCrmServiceProxy;
}
catch (Exception ex)
{
Response.Write("Error details : " + ex.Message);
}
return lclCrmService;
}
protected void gits_DownloadButton_Click(object sender, ImageClickEventArgs e)
{
string lclAnnotationId = string.Empty;
Guid lclAnnotationGuid = Guid.Empty;
string lclOrgId = string.Empty;
string lclOrgLcid = string.Empty;
string lclOrgName = string.Empty;
string lclEntityId = string.Empty;
string lclEntityTypeCode = string.Empty;
string lclEntityTypeName = string.Empty;
string lclUserLcid = string.Empty;
string lcldfidfieldName = string.Empty;
IOrganizationService lclCrmService = null;
Entity lclEntity = null;
Entity lclAnnotation = null;
string lclFileName;
LoadQueryStringParameters(ref lclOrgLcid, ref lclOrgName, ref lclEntityId, ref lclEntityTypeCode
, ref lclEntityTypeName, ref lclUserLcid, ref lcldfidfieldName);
lclCrmService = GetCRMServiceObject("administrator", "arif@1234", "mol", "OrgName", "http://win-dfk6m8db6gg:5555");
if (!string.IsNullOrEmpty(gits_UploadedfileGUID.Value))
{
lclAnnotationId = gits_UploadedfileGUID.Value;
lclAnnotationGuid = new Guid(lclAnnotationId);
}
else
{
lclEntity = lclCrmService.Retrieve(lclEntityTypeName, new Guid(lclEntityId), new ColumnSet(new string[] { lcldfidfieldName }));
if (lclEntity.Contains(lcldfidfieldName))
{
lclAnnotationId = lclEntity[lcldfidfieldName].ToString();
lclAnnotationGuid = new Guid(lclAnnotationId);
}
}
if (lclAnnotationGuid != Guid.Empty && lclAnnotationGuid != null)
{
lclAnnotation = lclCrmService.Retrieve("annotation", lclAnnotationGuid, new ColumnSet(new string[] { "filename", "documentbody" }));
if (lclAnnotation.Contains("filename") && lclAnnotation.Contains("documentbody"))
{
lclFileName = lclAnnotation["filename"].ToString();
//FileStream fileStream = new FileStream(lclFileName, FileMode.OpenOrCreate);
Byte[] fileContent = Convert.FromBase64String(lclAnnotation["documentbody"].ToString());
//fileStream.Write(fileContent, 0, fileContent.Length);
// Clear all content output from the buffer stream
Response.Clear();
// Add a HTTP header to the output stream that specifies the default filename
// for the browser's download dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + lclFileName);
// Add a HTTP header to the output stream that contains the
// content length(File Size). This lets the browser know how much data is being transfered
Response.AddHeader("Content-Length", fileContent.Length.ToString());
// Set the HTTP MIME type of the output stream
Response.ContentType = "application/octet-stream";
///=/ Write the data out to the client.
Response.BinaryWrite(fileContent);
}
}
}
}
Images
add this imges also
Comments
Post a Comment