Hi,
My aim is to get attachments from one web site and take them from xml document and save the attachments files to Crm Incident entity.
Her is my code:
if (id== "10554046")// The incident id from sikayetvar.com for testing (It has 2 pictures as attachment)
{
//In our company the incidents from sikayetvar.com is also saved to crm using api. This incident saved early without attachments.
//Now as an example incident I wanto to save the attachments form sikayetvar.com to Crm incident entity.
#region get Incident Id from Crm
string fetch = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='incident'>
<all-attributes />
<filter type='and'>
<condition attribute='new_cagrinumarasi' operator='eq' value='{0}' />
</filter>
</entity>
</fetch>", "SV-" + id);//id from sikayetvar.com. In our crm ve save them with adding SV-
Entity entt = CRM.crmLib.GetEntityByFetchXml(fetch);
#endregion get Incident Id
#region Get Attachments form sikayetvar.com
byte[] sul = new byte[10];
var attachments = xmlIncident.Elements("attachments"); // before this code blok i got the xml info from sikayetvar.com
int i = 1;
Entity attachment = new Entity("activitymimeattachment");
foreach (var fileAttachment in attachments)
{
//in xml data the url of the file is between <original> tags. I wanto to get rid of them .
string original = "<original>";
string original2 = "</original>";
string url = fileAttachment.LastNode.PreviousNode.ToString();
if (url.StartsWith(original))
url = url.Substring(original.Length);
if (url.EndsWith(original2))
url = url.Substring(0, url.Length - 11);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// execute the request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// we will read data via the response stream
Stream ReceiveStream = response.GetResponseStream();
string filename = Path.GetTempPath() + "SV-" + id + "-" + i + ".jpg";
byte[] buffer = new byte[1024];
FileStream outFile = new FileStream(filename, FileMode.Create);
int bytesRead;
while ((bytesRead = ReceiveStream.Read(buffer, 0, buffer.Length)) != 0)
outFile.Write(buffer, 0, bytesRead);
// Or using statement instead
outFile.Close(); //I got the first attachment photo to the appData temp folder. When i want to see the photo i can reach and open it successfully
#region Save attachments to Crm
Guid attachmentId = Guid.Empty;
Entity note = new Entity("annotation");
note["subject"] = "Incident attachments" ;
note["filename"] = "SV-" + id + "-" + i + ".jpeg";
note["mimetype"] = @"image/jpeg";
note["documentbody"] = Convert.ToBase64String(buffer);
note["objectid"] = new EntityReference("incident", entt.Id);
attachmentId = CRM.crmLib.CreateEntity(note);
#endregion Save attachments to Crm
#region delete filename
DirectoryInfo d = new DirectoryInfo(Path.GetTempPath());
FileInfo[] allFiles = d.GetFiles("*.jpeg");
foreach (FileInfo file in allFiles)
{
if (file.Name.Contains("SV-" + id + "-" + i + ".jpeg".ToString()))
{
file.Delete();// Remove the saved photo from appdata temp folder after attaching to the crm incident to reduce the disc space int the server
}
}
#endregion delete filename
i++;
}
#endregion Get Attachments form sikayetvar.com
}
Here is the picture from web thta the example incident looks like
The attachment addet to Crm incidet but i can open but can not see the picture. It gives me error.
How can i achieve this problem. Can anyone help me please?
*This post is locked for comments
I have the same question (0)

Report
All responses (
Answers (