Tuesday 2 July 2019

Import data using Excel MSCRM programitacally

   public static void ImportToCRMPluginErrorCodes()
        {
            //string filePath = Environment.CurrentDirectory + @"\Active sheetname.xlsx";
            Entity import = new Entity("import");
            import["modecode"] = new OptionSetValue(1);
            import["name"] = "Active sheet Name";
            import["sendnotification"] = false;
            Guid importId = organizationService.Create(import);

            OrganizationServiceContext servicecontext = new OrganizationServiceContext(organizationService);

            var importMap = (from a in servicecontext.CreateQuery("importmap") where a.GetAttributeValue<string>("name") == "DataFinal" select new { a.Id }).FirstOrDefault();

            Entity impmap = new Entity("importmap");
            Guid importMapId = importMap.Id;
            var importEntityMap = (from cl in servicecontext.CreateQuery("columnmapping")
                                   where (Guid)cl["importmapid"] == importMap.Id && cl["sourceentityname"] != null
                                   select cl).FirstOrDefault();

            // Create the ImportFile class
            Entity importFile = new Entity("importfile");
            importFile["content"] = getEncodedFileContents(filePath);
            importFile["name"] = "File Name";
            importFile["filetypecode"] = new OptionSetValue(3);
            importFile["isfirstrowheader"] = true;
            importFile["source"] = "lenovo.xlsx";
            importFile["sourceentityname"] = (string)importEntityMap["sourceentityname"];
            // Schema name of the Target entity
            importFile["targetentityname"] = "lvo_languageresource";
            importFile["importmapid"] = new EntityReference(impmap.LogicalName, importMapId);
            importFile["importid"] = new EntityReference(import.LogicalName, importId);
            //importFile["Size"] = importFile.Content.Length.ToString();
            importFile["size"] = importFile.FormattedValues.Count.ToString();
            importFile["processcode"] = new OptionSetValue(1);
            importFile["datadelimitercode"] = new OptionSetValue(1);
            importFile["fielddelimitercode"] = new OptionSetValue(2);
            importFile["usesystemmap"] = false;
            importFile["enableduplicatedetection"] = true;
            organizationService.Create(importFile);

            ParseImportRequest parseRequest = new ParseImportRequest();
            parseRequest.ImportId = importId;
            organizationService.Execute(parseRequest);

            TransformImportRequest transRequest = new TransformImportRequest();
            transRequest.ImportId = importId;
            TransformImportResponse transResponse = (TransformImportResponse)organizationService.Execute(transRequest);

            ImportRecordsImportRequest request = new ImportRecordsImportRequest();
            request.ImportId = importId;

            ImportRecordsImportResponse response = (ImportRecordsImportResponse)organizationService.Execute(request);
        }

        static public string getEncodedFileContents(String pathToFile)
        {
            FileStream fs = new FileStream(pathToFile, FileMode.Open, FileAccess.Read);
            byte[] binaryData = new byte[fs.Length];
            long bytesRead = fs.Read(binaryData, 0, (int)fs.Length);
            fs.Close();
            return System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
        }

No comments:

Post a Comment