本文介绍了从Notes实体下载附件并保存到Microsoft Dynamics CRM中的本地计算机时,出现插入路径错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

业务流程错误

代码如下:

QueryExpression notes = new QueryExpression { EntityName = "annotation", ColumnSet = new ColumnSet("filename", "subject", "annotationid", "documentbody","mimetype") };
                    notes.Criteria.AddCondition("annotationid", ConditionOperator.Equal, annotationid);
                    EntityCollection NotesRetrieve = service.RetrieveMultiple(notes);
                    if (NotesRetrieve != null && NotesRetrieve.Entities.Count > 0)
                    {
                        foreach (var note in NotesRetrieve.Entities)
                        {
                        string fileName = note.GetAttributeValue<string>("filename");
                        //string fileType = note.GetAttributeValue<string>("mimetype");
                        FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write, "D:\\note");
                        string fileLocation = f+ fileName;
                        byte[] fileContent = Convert.FromBase64String(NotesRetrieve.Entities[0].Attributes["documentbody"].ToString());
                        System.IO.File.WriteAllBytes(fileLocation, fileContent); 
                        }

                     }
                   }
                  catch (Exception ex)
                   {
                    throw new InvalidPluginExecutionException(ex.ToString());
                   }


推荐答案

Santosh,

您可以使用以下代码从Dynamics CRM中的注释实体在线下载附件。

You can use following code to download attachments from Note entity in Dynamics CRM online.

.cs文件的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int choice;
            CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString);
            IOrganizationService crmService = crmConn.OrganizationServiceProxy;
                    QueryExpression w_query = new QueryExpression { EntityName = "annotation", ColumnSet = new ColumnSet("filename","documentbody") };

                    EntityCollection w_account = crmService.RetrieveMultiple(w_query);
                     string name = "";
                    foreach (var count in w_account.Entities)
                    {
                        name = count.GetAttributeValue<string>("filename");

                        string doc_content = count.GetAttributeValue<string>("documentbody");
                        System.IO.FileStream wFile;
                        byte[] byteData = null;
                        if (name != null)
                        {
                            byte[] doc = Convert.FromBase64String(count.GetAttributeValue<string>("documentbody"));
                            byteData = doc;
                            wFile = new FileStream("D:\\attachments\\" + name, FileMode.Append);
                            wFile.Write(byteData, 0, byteData.Length);
                            wFile.Close();
                        }
                    }

                    Console.WriteLine("Please find all attachments in your local D: drive");
                    Console.WriteLine("Press any key to exit...");
                    Console.ReadKey();

            }
        }

    }

.config文件的代码:

Code for .config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>


  <connectionStrings>
    <add name="CRM" connectionString="AuthType=Office365;Url=https://your_organization_url; Username=your_user_name; Password=Your_password" />
  </connectionStrings>


    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.22.0.0" newVersion="2.22.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.22.0.0" newVersion="2.22.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

您只需要编辑.config文件代码,这应该能为您工作。

You just need to edit .config file code, this should work for you.

注意:您必须创建C#控制台应用程序并从Nuget添加所需的程序集。

Note:you have to create C# console application and add required assemblies from Nuget.

这篇关于从Notes实体下载附件并保存到Microsoft Dynamics CRM中的本地计算机时,出现插入路径错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:34