Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 55831

Plugin - CSV file Error while creating CSV file in local/server physical path.

$
0
0

Hi Experts,

I am working on CRM 2015 on premises and I have a scenario where, updating the status of the Lead entity a csv file will be create with the lead details in local/server physical path. I developed a plugin it was registered in Lead entity - POST operation stage - Update message - Asynchronous execution mode - Server Deployment, and also registered as non-Isolation mode.

When I changed the lead status I am getting an exception i.e."Security Exception was caught - Request for the permission of type 'System.Security.Permissions.FileIOPermission' failed".

I am attaching csv file creation code and Exception screen shot.

Please guide me where I am doing mistake.

Thanks in Advance

HarryPK

//Create csv file in local system file path
StringBuilder csvContent = new StringBuilder();
string header, body = "";


if (customer.Entities[0].Attributes.Contains("fullname"))
{
header = "Full Name";
body = customer.Entities[0].Attributes["fullname"].ToString();
// csvContent.A = "Full Name";
// workSheet.Cells[2, "A"] = customer.Entities[0].Attributes["fullname"].ToString();
}
else
{
header = "Full Name";
body = " ";
}

if (customer.Entities[0].Attributes.Contains("aparna_fathershusbandname"))
{
header += " , Father's / Husband's Name";
body += " , " + customer.Entities[0].Attributes["aparna_fathershusbandname"].ToString();
}
else
{
header += " , Father's / Husband's Name";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("aparna_age"))
{
header += " , Age";
body += " , " + customer.Entities[0].Attributes["aparna_age"].ToString();
}
else
{
header += " , Age";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("aparna_professiontype"))
{
header += " , Occupation";
body += " , " + customer.Entities[0].Attributes["aparna_professiontype"].ToString();
}
else
{
header += " , Occupation";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("aparna_panwardcirclerange"))
{
header += " , PAN/Ward/Circle/Range";
body += " , " + customer.Entities[0].Attributes["aparna_panwardcirclerange"].ToString();
}
else
{
header += " , PAN/Ward/Circle/Range";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("address1_line1"))
{
header += " , Address 1";
body += " , " + customer.Entities[0].Attributes["address1_line1"].ToString();
}
else
{
header += " , Address 1";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("address1_line2"))
{
header += " , Address 2";
body += " , " + customer.Entities[0].Attributes["address1_line2"].ToString();
}
else
{
header += " , Address 2";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("address1_city"))
{
header += " , City";
body += " , " + customer.Entities[0].Attributes["address1_city"].ToString();
}
else
{
header += " , City";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("aparna_state"))
{
header += " , State";
body += " , " + customer.Entities[0].Attributes["aparna_state"].ToString();
}
else
{
header += " , State";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("aparna_country"))
{
header += " , Country";
body += " , " + customer.Entities[0].Attributes["aparna_country"].ToString();
}
else
{
header += " , Country";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("mobilephone"))
{
header += " , Contact Number";
body += " , " + customer.Entities[0].Attributes["mobilephone"].ToString();
}
else
{
header += " , Contact Number";
body += " , ";
}

if (customer.Entities[0].Attributes.Contains("emailaddress1"))
{
header += " , Email";
body += " , " + customer.Entities[0].Attributes["emailaddress1"].ToString();
}
else
{
header += " , Email";
body += " , ";
}

if (lead.Entities[0].Attributes.Contains("aparna_tower"))
{
header += " , Apt No.";
body += " , " + lead.Entities[0].Attributes["aparna_tower"].ToString();
}
else
{
header += " , Apt No.";
body += " , ";
}

if (lead.Entities[0].Attributes.Contains("aparna_block"))
{
header += " , Block";
body += " , " + lead.Entities[0].Attributes["aparna_block"].ToString();
}
else
{
header += " , Block";
body += " , ";
}

if (lead.Entities[0].Attributes.Contains("aparna_floor"))
{
header += " , Floor";
body += " , " + lead.Entities[0].Attributes["aparna_floor"].ToString();
}
else
{
header += " , Floor";
body += " , ";
}

if (lead.Entities[0].Attributes.Contains("aparna_unitfacing"))
{
header += " , Facing";

if (((Microsoft.Xrm.Sdk.OptionSetValue)(lead.Entities[0].Attributes["aparna_unitfacing"])).Value == 100000000)
{
body += " , East";
}
else if (((Microsoft.Xrm.Sdk.OptionSetValue)(lead.Entities[0].Attributes["aparna_unitfacing"])).Value == 100000001)
{
body += " , West";
}
else if (((Microsoft.Xrm.Sdk.OptionSetValue)(lead.Entities[0].Attributes["aparna_unitfacing"])).Value == 100000002)
{
body += " , North";
}
else if (((Microsoft.Xrm.Sdk.OptionSetValue)(lead.Entities[0].Attributes["aparna_unitfacing"])).Value == 100000003)
{
body += " , South";
}
}

csvContent.AppendLine(header);
csvContent.AppendLine(body);

string fileName = "CRM2ERP-PB_" + lead.Entities[0].Attributes["aparna_leadreference"] + "_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".csv";
string filePath = @"D:\" + fileName;

if (!File.Exists(filePath))
{
File.Create(filePath).Close();
}

File.AppendAllText(filePath, csvContent.ToString());


Viewing all articles
Browse latest Browse all 55831

Trending Articles