private static bool writeIAMSCSV(IamsItem iamsRecord, String outFolder)
        {
            bool fError = false;

            try // to write the csv...
            {
                List <String> strHeaders = new List <string>();

                strHeaders.Add("Child shelfmarks");


                string fNameString = "IamsRecords";
                string outPath     = outFolder + @"\" + fNameString + ".csv";

                if (!File.Exists(outPath))                            // only write once per shelfmark...
                {
                    using (var sr = new StreamWriter(outPath, false)) // changed from uce to utf8
                    {
                        using (var csvFile = new CsvHelper.CsvWriter(sr, System.Globalization.CultureInfo.InvariantCulture))
                        {
                            csvFile.Configuration.Delimiter = ",";
                            //csvFile.Configuration.HasExcelSeparator = true;

                            csvFile.WriteField("Shelfmark");
                            csvFile.WriteField("Item type");
                            csvFile.WriteField("Catalogue status");
                            csvFile.NextRecord(); // skips to next line...

                            csvFile.WriteField(iamsRecord.ItemShelfmark);
                            csvFile.WriteField(iamsRecord.ItemType);
                            csvFile.WriteField(iamsRecord.CatalogueStatus);
                            csvFile.NextRecord(); // skips to next line...



                            foreach (var header in strHeaders)
                            {
                                csvFile.WriteField(header);
                            }
                            csvFile.NextRecord(); // skips to next line...
                            foreach (var cShelfmark in iamsRecord.ChildRecordTitles)
                            {
                                csvFile.WriteField(cShelfmark); // field value
                                csvFile.NextRecord();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error writing IAMS CSV File: {0}", ex);
                fError = true;
            }
            return(!fError);
        }
        public static IamsItem GetIAMSRecords(HMDObject item)
        {
            // Remember to get the list of child records and pass this back to the image order csv somehow...
            String shelfmark = item.Shelfmark;
            String itemID    = null;

            if (item.ID != null)  // allows for some flexibility on being tied to sharepoint here
            {
                itemID = item.ID;
            }


            // build the request
            // Need to use a request with the 'None' format to get the catalogue publication status/state
            // Also need 'itemtype' field where this can be fonds etc

            // The only output format of the IAMS request that contains the catalogue publication status is "NONE"
            // So make two requests to get all the information required

            var iamsCatStatusRequest = IAMSURL + shelfmark.Replace(" ", "%20").Replace(",", "%2C") + @"&format=None";
            var iamsGetUrl           = IAMSURL + shelfmark.Replace(" ", "%20").Replace(",", "%2C") + @"&format=Qatar"; // Could use DIPS format, but Qatar gets you more information


            // Run the first request:
            var xmlTextReaderIamsNone = new XmlTextReader(iamsCatStatusRequest);
            var xmlDocumentIamsNone   = new XmlDocument();

            try
            {
                xmlDocumentIamsNone.Load(xmlTextReaderIamsNone);
            }
            catch
            {
                return(null);
            }
            var xmlNodeItemType  = xmlDocumentIamsNone.SelectSingleNode("//Header//RecordDetails//ItemType");
            var xmlNodeCatStatus = xmlDocumentIamsNone.SelectSingleNode("//Header//RecordDetails//Status");



            var iamsItemType = string.Empty;

            if (xmlNodeItemType != null)
            {
                iamsItemType = xmlNodeItemType.InnerText;
            }

            var iamsCatStatus = string.Empty;

            if (xmlNodeCatStatus != null)
            {
                iamsCatStatus = xmlNodeCatStatus.InnerText;
            }



            var xmlTextReaderIams = new XmlTextReader(iamsGetUrl);
            var xmlDocumentIams   = new XmlDocument();

            try
            {
                xmlDocumentIams.Load(xmlTextReaderIams);
            }
            catch
            {
                return(null);
            }
            var xmlNodeArk          = xmlDocumentIams.SelectSingleNode("//MDARK");
            var xmlNodeTitle        = xmlDocumentIams.SelectSingleNode("//Title");
            var xmlNodeSubSubSeries = xmlDocumentIams.SelectSingleNode("//Ancestors//Ancestor[@level='1']//Reference");
            var xmlLogicalLabel     = xmlDocumentIams.SelectSingleNode("//LogicalLabel");
            var xmlLogicalType      = xmlDocumentIams.SelectSingleNode("//LogicalType");
            var xmlChildRecords     = xmlDocumentIams.SelectNodes("//Children//Child//Reference");
            var xmlShelfmark        = xmlDocumentIams.SelectSingleNode("//Reference");


            var logicalLabel = string.Empty;
            var logicalType  = string.Empty;

            if (xmlNodeArk == null)
            {
                return(null);
            }
            var iamsArk = xmlNodeArk.InnerText;

            var  iamsRetrievedShelfmark = string.Empty;
            bool deleteFlagShelfmark    = false;

            if (xmlShelfmark != null)
            {
                iamsRetrievedShelfmark = xmlShelfmark.InnerText;
            }
            if (iamsRetrievedShelfmark.ToString().Contains("DEL") || iamsRetrievedShelfmark.ToString().Contains(@"D/"))
            {
                deleteFlagShelfmark = true;
            }

            var iamsTitle = string.Empty;

            if (xmlNodeTitle != null)
            {
                iamsTitle = xmlNodeTitle.InnerText;
            }

            var subSubSeries = string.Empty;

            if (xmlNodeSubSubSeries != null)
            {
                subSubSeries = xmlNodeSubSubSeries.InnerText;
            }

            if (xmlLogicalLabel != null)
            {
                logicalLabel = xmlLogicalLabel.InnerText;
            }

            if (xmlLogicalType != null)
            {
                logicalType = xmlLogicalType.InnerText;
            }

            List <string> childRecordTitles        = new List <String>();
            List <string> deletedChildRecordTitles = new List <String>();

            bool containsDeletedChildRecords = false;

            if (xmlChildRecords != null)
            {
                foreach (XmlNode record in xmlChildRecords)
                {
                    var innerText = record.InnerText;
                    childRecordTitles.Add(record.InnerText);
                    if (record.InnerText.ToString().Contains("DEL") || record.InnerText.ToString().Contains(@"D/"))
                    {
                        containsDeletedChildRecords = true;
                        deletedChildRecordTitles.Add(record.InnerText);
                    }
                }
            }
            bool isDeleteFlagPresent = false;

            if (containsDeletedChildRecords || deleteFlagShelfmark)
            {
                isDeleteFlagPresent = true;
            }

            var iamsItem = new IamsItem
            {
                SharepointID             = itemID,
                ItemShelfmark            = shelfmark,
                ItemDescription          = iamsTitle,
                ArkIdentifier            = iamsArk,
                SubSubSeries             = subSubSeries,
                LogicalLabel             = logicalLabel,
                LogicalType              = logicalType,
                ChildRecordTitles        = childRecordTitles,
                DeletedChildRecordTitles = deletedChildRecordTitles,
                DeleteFlagPresent        = isDeleteFlagPresent,
                ItemType        = iamsItemType,
                CatalogueStatus = iamsCatStatus
            };



            return(iamsItem);
        }