简体   繁体   English

使用C#从xml数据获取附件到Crm 2015

[英]Get attachments from xml data using c# to Crm 2015

we have a website that the customer writes her incident with attachments. 我们有一个网站,客户可以在该网站上记录附件事件。 I can get the incident and save it to Crm 2015 system. 我可以得到此事件并将其保存到Crm 2015系统。 Hovewer i want to save also the attachments like pictures etc. 我还想保存图片等附件。

I get the incidents from xml and read one by one and save the to Crm 2015 System: 我从xml中获取事件,并逐一阅读并将其保存到Crm 2015 System:

foreach (XElement xmlIncident in xmlIncidents)
{

}

In this foreach i can get the attachment values: 在此foreach中,我可以获得附件值:

var attachments = xmlIncident.Elements("attachments"); //get the collection of attachments.

As an example, ın may example incident i have 4 jpg photosand one of them seems to be like this in c# code: 举个例子,在我举个例子,我有4张jpg照片时,其中之一似乎在c#代码中是这样的:

https://docs.sikayetvar.com/complaint/1054/10543034/gbhjk-1528636381_160x160.jpg https://docs.sikayetvar.com/complaint/1054/10543034/gbhjk-1528636381.jpg photo https://docs.sikayetvar.com/complaint/1054/10543034/gbhjk-1528636381_160x160.jpg https://docs.sikayetvar.com/complaint/1054/10543034/gbhjk-1528636381.jpg照片

My question is how can i get the attachments form xml data and save it to Crm 2015 incident table using c# 我的问题是如何获取附件形式的xml数据并将其保存到使用C#的Crm 2015事件表中

在此处输入图片说明

I guess it's not a good idea to update the CRM database directly as there are plenty of things available in the CRM SDK. 我想直接更新CRM数据库不是一个好主意,因为CRM SDK中有很多可用的功能。

Steps to perform: 执行步骤:

  1. Get the file path from the XML 从XML获取文件路径
  2. verify the file type to be jpg then read the file as Base64 string 验证文件类型为jpg,然后将文件读取为Base64字符串
  3. Create Entity of type Annotation where objectid = record GUID 创建Annotation类型的实体,其中objectid = record GUID

Look at the below code it should work for you 看下面的代码,它应该适合你

Guid AttachToIncident(string filePath, Guid recordGuid){    
    Func<string,string> imageToBase64 = (fpath) => {
        using (Image image = Image.FromFile(fpath))
        {
            using (MemoryStream memStrm = new MemoryStream())
            {
                image.Save(memStrm, image.RawFormat);
                byte[] imageBytes = memStrm.ToArray();
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }   
    };

    string fileName = Path.GetFileName(filePath);

    Guid attachmentId = Guid.Empty;

    Entity newAnnotation = new Entity("annotation");
    newAnnotation["subject"] = "external attachment";
    newAnnotation["filename"] = filename;
    newAnnotation["mimetype"] = @"image/jpeg";
    newAnnotation["documentbody"] = imageToBase64(filePath);
    newAnnotation["objectid"] = new EntityReference("incident", recordGuid);

   //you must be knowing what this service is ;)
    attachmentId = orgService.Create(newAnnotation); 
    return attachmentId;    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM