Rechercher dans ce blog

vendredi 10 août 2012

Comment importer les pièces jointes par programme

How to Import attachments programmatically

Voici les étapes:
1. Créer un fichier CSV contenant les pièces jointes que tu souhaites importer.
2. créer un enregistrement d'import.
3. Créer un enregistrement pour les pièces jointes qui contient le contenu des fichiers actuels.
4. Créer un enregistrement qui contient le mapping.
5. Appeler les méthodes SDK d'import.

      /// 
 
       /// Impport attachments in CRM
 
       /// 
 
       public void ImportAttachmentRecords()
 
       {            
 
OrganizationServiceProxy _serviceProxy; // Initialize the _serviceProxy appropriately.
 
           // 1. Create a CSV file containing the details of attachments that you want to import.
 
           string fileName = DateTime.UtcNow.Ticks + ".txt";
 
           string attachmentContent = "This is the attachment content"; // Put your attachment contents here.
 
           string attachmentData = Convert.ToBase64String(Encoding.UTF8.GetBytes(attachmentContent));
 
           StringBuilder sb = new StringBuilder();
 
           //Add header row
 
           sb.AppendLine("Title, Document, File Name, Regarding");
 
           string accountId = "04AFD3BF-16D6-E111-A43C-00155D187A0A"; // This is the unique id of CRM record e.g. account with which you want to attach this note.
 
           string dataRow = "attachment1, " + fileName + ", " + fileName + "," + accountId;
 
           sb.AppendLine(dataRow);
 
           string csvData = sb.ToString();
 
           // 2.    Create the root import record
 
           Import import = new Import()
 
           {
 
               ModeCode = new OptionSetValue((int)ImportModeCode.Create),
 
               Name = "Importing data"
 
           };
 
           Guid importId = _serviceProxy.Create(import);
 
          // 3.    Create import file record for attachments which contains actual file contents.
 
           ImportFile importAttachmentFile = new ImportFile()
 
           {
 
               Content = attachmentData, // Read contents from disk.
 
               Name = fileName,
 
               UseSystemMap = true,
 
               ImportId = new EntityReference(Import.EntityLogicalName, importId),
 
               ProcessCode =
 
                   new OptionSetValue((int)ImportFileProcessCode.Internal),
 
               FileTypeCode = new OptionSetValue(2)
 
           };
 
           Guid importAttachmentFileId = _serviceProxy.Create(importAttachmentFile);
 
           // 4.    Create import file record which contains mapping for attachments.
 
           ImportFile importFile = new ImportFile()
 
           {
 
               Content = csvData, // Read contents from disk.
 
               Name = "Attachment Import File",
 
               IsFirstRowHeader = true,
 
               UseSystemMap = true,
 
               Source = "Import Attachment",
 
               SourceEntityName = "attachment",
 
               TargetEntityName = Annotation.EntityLogicalName,
 
               ImportId = new EntityReference(Import.EntityLogicalName, importId),
 
               EnableDuplicateDetection = false,
 
               FieldDelimiterCode =
 
                   new OptionSetValue((int)ImportFileFieldDelimiterCode.Comma),
 
               DataDelimiterCode =
 
                   new OptionSetValue((int)ImportFileDataDelimiterCode.DoubleQuote),
 
               ProcessCode =
 
                   new OptionSetValue((int)ImportFileProcessCode.Process)
 
           };
 
           Guid importFileId = _serviceProxy.Create(importFile);
 
           // 5.    Call import SDK APIs
 
           // Parse the import file.
 
           ParseImportRequest parseImportRequest = new ParseImportRequest()
 
           {
 
               ImportId = importId
 
           };
 
           ParseImportResponse parseImportResponse =
 
               (ParseImportResponse)_serviceProxy.Execute(parseImportRequest);
 
           WaitForAsyncJobCompletion(_serviceProxy, parseImportResponse.AsyncOperationId);
 
           // Transform the import
 
           TransformImportRequest transformImportRequest = new TransformImportRequest()
 
           {
 
               ImportId = importId
 
           };
 
           TransformImportResponse transformImportResponse =
 
               (TransformImportResponse)_serviceProxy.Execute(transformImportRequest);
 
           WaitForAsyncJobCompletion(_serviceProxy, transformImportResponse.AsyncOperationId);
 
           // Upload the records.
 
           ImportRecordsImportRequest importRequest = new ImportRecordsImportRequest()
 
           {
 
               ImportId = importId
 
           };
 
           ImportRecordsImportResponse importResponse =
 
               (ImportRecordsImportResponse)_serviceProxy.Execute(importRequest);
 
           WaitForAsyncJobCompletion(_serviceProxy, importResponse.AsyncOperationId);
 
       }
 
       /// 
 
       /// Waits for the async job to complete.
 
       /// 
 
       /// 
public void WaitForAsyncJobCompletion(OrganizationServiceProxy serviceProxy, Guid asyncJobId)
 
       {
 
           ColumnSet cs = new ColumnSet("statecode", "statuscode");
 
           AsyncOperation asyncjob =
 
               (AsyncOperation)serviceProxy.Retrieve("asyncoperation", asyncJobId, cs);
 
           int retryCount = 100;
 
           while (asyncjob.StateCode.Value != AsyncOperationState.Completed && retryCount > 0)
 
           {
 
               asyncjob = (AsyncOperation)serviceProxy.Retrieve("asyncoperation", asyncJobId, cs);
 
               System.Threading.Thread.Sleep(2000);
 
               retryCount--;
 
           }
 
       }

Aucun commentaire:

Enregistrer un commentaire