3

i was trying to create a console application so i could take an export package using the API(http://erichuiza.blogspot.nl/2013/12/tridion-2013-sp1-api-improvements.html) I have this code:

ImportExportServiceClient client = new ImportExportServiceClient();
   ReadFromTxt txtObject= new ReadFromTxt();
   Selection[]  selection = txtObject.fetchList();
   ExportInstruction instruction = new ExportInstruction { LogLevel = LogLevel.Normal };
   string processId = client.StartExport(selection, instruction);

This gives me the error

Unhandled Exception: System.ServiceModel.CommunicationException: There was an er ror while trying to serialize parameter http://www.sdltridion.com/ContentManager /ImportExport/2013:selections. The InnerException message was 'Type 'ExportImpor tBusiness.implementSelection' with data contract name 'implementSelection:http:/ /schemas.datacontract.org/2004/07/ExportImportBusiness' is not expected. Conside r using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by a dding them to the list of known types passed to DataContractSerializer.'

This breaks at client.StartExport.
ExportImportBusiness is the namespace of my business class.
The method ReadFromTxt() returns an array of Selection. It reads from a text file containing TCM-ids, 1 in a row.

Code from ReadFromTxt

public class ReadFromTxt
    {

        public Tridion.ContentManager.ImportExport.Selection[] fetchList()
        {
            Tridion.ContentManager.ImportExport.Selection[] a = null; ;
            a = populateArray(a);
            Console.WriteLine("After populating Array");
            Console.WriteLine(a[0].ToString());
            return a;
        }


        public Tridion.ContentManager.ImportExport.Selection[] populateArray(Tridion.ContentManager.ImportExport.Selection[] a)
        {
            Console.WriteLine("Inside method populateArray. About to populate Array");
            string filePath = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
            Console.WriteLine("Value of variable file is" +filePath);
            int count = File.ReadLines(filePath).Count();
            a = InitializeArray<implementSelection>(count, filePath);
            return a;
        }

        public implementSelection[] InitializeArray<T>(int length, string filePath) where T : new()
        {
            Console.WriteLine("Inside method InitializeArray ");
            implementSelection[] array = new implementSelection[length];
            for (int i = 0; i < length; ++i)
            {
                string line = File.ReadLines(filePath).Skip(i - 1).Take(1).First();
                //T item = new T();
                implementSelection item = new implementSelection(line);
               // item.field = line;
                array[i] = item;
                Console.WriteLine(array[i]);
            }

            Console.WriteLine(array.GetType());
            return array;
        }



        public ProcessState WaitForProcessFinish(ImportExportServiceClient client, string processId)
        {
            do
            {
                Thread.Sleep(1000);
                ProcessState? processState = client.GetProcessState(processId);
                if (processState == ProcessState.Finished || processState == ProcessState.Aborted || processState == ProcessState.AbortedByUser)
                {
                    return processState.Value;
                }
            }

            while (true);
        }


        public void DownloadPackage(string processId, string packageLocation)
        {
            var downloadClient = new ImportExportStreamDownloadClient();
            // downloadClient.DownloadProcessLogFile(processId, true) to download log file
            using (var packageStream = downloadClient.DownloadPackage(processId, deleteFromServerAfterDownload: true))
            {
                using (var fileStream = File.Create(packageLocation))
                {
                    packageStream.CopyTo(fileStream);
                }
            }
        }
    }

And i have used the Selection class as below:

public class implementSelection : Selection
    {
        public implementSelection()
        { }

        public implementSelection(string value)
        {
            field = value;
        }

        public string field = "abdc";
        public override string ToString()
        {

            return field;
        }
    }

Thanks in advance

  • what are you exporting? what fetchList() is returning? can you post in your question. – Raj Kumar Feb 18 '16 at 7:15
  • could you share signature and code of ReadFromTxt() it seems this method returning some reference to your namespace also, which is being reported as exception – Raj Kumar Feb 18 '16 at 8:03
5

Selection is a parent class of selections for Export, such as GroupsSelection, SubtreeSelection, etc. More about selections you can read in following blog: http://tridioninternals.blogspot.nl/2014/01/selecting-items-to-export.html

In your case I suspect you want to export list of tcm-uris or webdav urls, which you read from file. In this case you should use ItemsSelection and your code will look like:

string[] array = { "tcm:0-1-1", "/webdav/Publication" }; //array which you read from file   
Selection[] selection = { new ItemsSelection { ItemIds = array } };
  • Hi, the function ReadFromTxt() returns an array of Selection. It reads from a text file containing TCM-ids, 1 in a row. – NutsAndBolts Feb 18 '16 at 7:59
  • I looks like your code also have a Selection class, which is also declared in ImportExportClient. I thinks it might be incorrectly recognized in your program from which place to get this class. Can you try to directly put namespace before your class? – Stanislav Maryenko Feb 18 '16 at 8:29
  • Hi, i updated it as public class implementSelection : Tridion.ContentManager.ImportExport.Selection but the error is same. I did F12 before change and it was going to right definition. – NutsAndBolts Feb 18 '16 at 9:31
  • Why do you need to re-implement Selection class in first place? It will case a data contact break, because new properties can not be deserialized. – Stanislav Maryenko Feb 18 '16 at 9:49
0

so public class implementSelection : Selectionis your custom class in your mentioned namespace.

That's the reason your are getting serialization error. No aliens. so you need to update you code. remove your implementSelection class.

Update your code as per following Sample Code

public IEnumerable<string>InitializeArray(int length, string filePath)
        {
        }
public IEnumerable<string> populateArray()
        {
        }

public IEnumerable<string> fetchList(){}


var client = new ImportExportServiceClient();
var txtObject= new ReadFromTxt();
var itemLists=  txtObject.fetchList();

var selection = new[] { new ItemsSelection(itemsList) };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.