web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Can't open the attachment file

(0) ShareShare
ReportReport
Posted on by 262

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

web.JPG

The attachment addet to Crm incidet but i can open but can not see the picture. It gives me error.

cant_5F00_open.JPG

How can i achieve this problem. Can anyone help me please?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    sandeepstw Profile Picture
    4,601 on at

    Hi,

    Please follow this article -

    community.dynamics.com/.../155131

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans