2016-06-13 5 views
0

私は、エンティティごとに添付ファイル(注釈)をダウンロードするコンソールアプリケーションを作成しています。そのアイデアは、エンティティ名、私がフェッチてきた今まで各フォルダへの添付ファイルをダウンロードする名前は添付ファイルのIDです。crm2011

string filepath = "C:\\"; 
String fetchXmlNote = "<fetch mapping='logical' count='20'>"; 
fetchXmlNote += "<entity name='annotation'>"; 
fetchXmlNote += "<attribute name='filename'/>"; 
fetchXmlNote += "<attribute name='documentbody'/>"; 
fetchXmlNote += "<attribute name='mimetype'/>"; 
fetchXmlNote += "<attribute name='objectid'/>"; 
fetchXmlNote += "<link-entity name='" +EntityName+ "' from='" + EntityName + "id' to='objectid'>"; 
fetchXmlNote += "</link-entity>"; 
fetchXmlNote += "</entity>"; 
fetchXmlNote += "</fetch>"; 

Microsoft.Xrm.Sdk.EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXmlNote)); 
if (result != null) 
{ 
    FilePath = FilePath + EntityName + "\\"; 

    foreach (Entity e in result.Entities) 
    { 
     Directory.CreateDirectory(FilePath + e.GetAttributeValue<EntityReference>("objectid").Id.ToString()); 
     if (!String.IsNullOrWhiteSpace(e.Attributes["documentbody"].ToString())) 
     { 
      byte[] data = Convert.FromBase64String(e.Attributes["documentbody"].ToString()); 

      File.WriteAllBytes(FilePath + e.Attributes["filename"].ToString(), data); 
     } 

:エンティティフォルダ、今までの私のコードで注釈のオブジェクトIDと名前が付けられ、各サブフォルダにfolder.HereのIDに対応するファイルになりますファイルをダウンロードするためのxml、私はそれぞれのobjectidのためのフォルダを作成しましたが、私は対応するフォルダに対応するIDを持つ各ファイルを配置する必要があります。 重複のチェックが必要であることに注意してください。 何か助けていただければ幸いです。

+0

ない、あなたが求めているのかわから存在する場合は、各file/folderをチェックしてみてください。すでにディレクトリ( 'Directory.CreateDirectory')を作成するコードがあり、すでに' objectid'を持っています。あなたはどんな問題を抱えていますか? – jasonscript

答えて

0

は、それがすでに

using System.IO; 


if (result != null) 
{ 
FilePath = FilePath + EntityName; 
if(CheckIfFolderExists(FilePath + EntityName)==false) //Check primary folder, if do not exists create one 
{ 
    Directory.CreateDirectory(FilePath + EntityName); //Create primary folder 
} 
FilePath+= "\\"; 
foreach (Entity e in result.Entities) 
{ 
    if(CheckIfFolderExists(FilePath + e.GetAttributeValue<EntityReference>("objectid").Id.ToString()))==false) //Check subfolder, if do not exists create one 
    { 
    Directory.CreateDirectory(FilePath + e.GetAttributeValue<EntityReference>("objectid").Id.ToString()); //Create subfolder inside primary folder 
    } 
//Also check here, if file with the same name already exists, if not create one 
    if (!String.IsNullOrWhiteSpace(e.Attributes["documentbody"].ToString()) && !File.Exists(FilePath + e.Attributes["filename"].ToString())) 
    { 
     byte[] data = Convert.FromBase64String(e.Attributes["documentbody"].ToString()); 
     File.WriteAllBytes(FilePath + e.Attributes["filename"].ToString(), data); 
    } 
} 
} 
public static bool CheckIfFolderExists(string path) 
{ 
return Directory.Exists(path); 
} 
関連する問題