1

EDIT: The cause of this is now known to be because those items are not of the same ItemClass. So if you have a folder that has IPM.Note, IPM.Appointment, IPM.Contact along with messages you will get this problem.

So the question is how do you handle this scenario.

Original Post below:
I have a piece of code that moves items from one folder to another using the EWS API and C#.

This is not a duplicate to What is a NullReferenceException, and how do I fix it? as this issue seems to occur within the API.

The exception is being handled but the original cause of this is unknown as there are items that are not being moved.

It seems to work fine for most items in a folder barring a few where it throws the message:

"Object reference not set to an instance of an object."

The code essentially looks like this:

FindItemsResults<Item> findResults = E_SERVICE.FindItems(src_fldr_id, new ItemView(1000000));

if (findResults.TotalCount != 0)
{
    for (int i = 0; i < findResults.TotalCount; i++)
    {
        try
        {
            findResults.Items[i].Move(tgt_fldr_id);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }                        
    }
}

This actually works for the most part. I have looked at what items it is struggling to move and it appears to be Calendar Invites or Draft items.

Anyone know how to get around this problem and force those items to be moved rather than generating a null item.

Here is the full function as defined. Apologies in advance for the poor commenting. Still in a scratch stage.

    static private bool MoveItemsInternal(FolderId fldr_id,string source, string target)
    {
        FolderView newFV = new FolderView(100000);
        newFV.Traversal = FolderTraversal.Deep;
        FindFoldersResults f_results = E_SERVICE.FindFolders(fldr_id, newFV);
        FolderId src_fldr_id = null;
        FolderId tgt_fldr_id = null;
        StringList errors = new StringList();

        Console.WriteLine("Total number of Folders in Destination Mailbox " + TARGETMAILBOX + ": " + f_results.TotalCount.ToString());

        int src_count = 0;
        int tgt_count = 0;


        foreach (Folder fldr in f_results)
        {
            if (fldr.DisplayName.ToLower() == source.ToLower())
            {
                src_fldr_id = fldr.Id;
                src_count++;
            }

            if (fldr.DisplayName.ToLower() == target.ToLower())
            {
                tgt_fldr_id = fldr.Id;
                tgt_count++;
            }
        }

        if (src_fldr_id != null)
        {
            if (src_count < 2)
            {
                Console.WriteLine("Source Folder " + source + " found!");
            }
            else
            {
                Console.WriteLine("More than " + src_count.ToString() + " source Folders Found");
                return false;
            }
        }
        else
        {
            Console.WriteLine("Source Folder Not Found");
            return false;
        }

        if (tgt_fldr_id != null)
        {
            if (tgt_count < 2)
            {
                Console.WriteLine("Target Folder " + target + " found!");
            }
            else
            {
                Console.WriteLine("More than " + src_count.ToString() + " target Folders Found");
                return false;
            }
        }
        else
        {
            Console.WriteLine("Target Folder Not Found");
            return false;
        }

        if (src_count == 1 && tgt_count == 1)
        {
            Folder src_folder = Folder.Bind(E_SERVICE, src_fldr_id);

            FindItemsResults<Item> findResults = E_SERVICE.FindItems(src_fldr_id, new ItemView(1000000));

            if (findResults.TotalCount != 0)
            {
                Console.WriteLine("Total Mail Count :" + findResults.TotalCount);
                Console.WriteLine("Moving all mail items to new location");

                for (int i = 0; i < findResults.TotalCount; i++)
                {
                    try
                    {
                        Console.WriteLine(TARGETMAILBOX + "\\" + source + "\\" + findResults.Items[i].Subject.ToString());
                        findResults.Items[i].Move(tgt_fldr_id);
                    }
                    catch (Exception e)
                    {
                        errors.Add("Item " + i + " Error: " + e.Message);
                    }                        
                }
            }
            else
            {
                Console.WriteLine("No Mail Items found");
            }

            FindFoldersResults f_fldr_result = E_SERVICE.FindFolders(src_fldr_id, newFV);
            if(f_fldr_result.TotalCount!=0)
            {
                Console.WriteLine("\n\nMoving all Sub folders to new location\n");
                foreach (Folder folder in f_fldr_result)
                {
                    Console.WriteLine(TARGETMAILBOX + "\\" + source + "\\" + folder.DisplayName.ToString());
                    folder.Move(tgt_fldr_id);
                }
            }else
            {
                Console.WriteLine("No Folders found");
            }
        }

        if(errors.Count!=0)
        {
            Console.WriteLine("Errors have been encountered:\n\n");
            foreach(string s in errors)
            {
                Console.WriteLine(s);
            }
        }

        return true;
    }
Community
  • 1
  • 1
RR_SH
  • 58
  • 8
  • could you include a full call stack, so we see where exactly this exception originates from - is it the indexer of `Items`? It is correct that there are some "invalid move scenarios", but that is not limited to EWS, you won't be able to do it in outlook either. – Cee McSharpface Feb 10 '17 at 14:02
  • Hi @dlatikay thanks for the quick response. The exception originates from 'findResults.Items[i].Move(tgt_fldr_id);' as it only occurs trying to access some items in the folder and not all. – RR_SH Feb 10 '17 at 14:09
  • I have amended the post so you can see the full function. The function takes 3 arguments. fldr_id -> Is the folder ID for the source folder root. source is the name of the source folder and target is the folder we are moving the items to. The function copies all items in the folder as well as any sub-folders attached to it into the target folder location specified. – RR_SH Feb 10 '17 at 14:26
  • Posting the full method won't help. Have you tried debugging? What is null? Is it `findResults`, `tgt_fldr_id`, one of the items? Have you tried *checking* for nulls? Why are you using TotalCount and a `for` instead of `foreach(var item in findResults)`? – Panagiotis Kanavos Feb 10 '17 at 14:38
  • Is the operation paged? Did you check the `MoreAvailable`, `NextPageOffset` properties of [FindItemsResult](https://msdn.microsoft.com/en-us/library/office/dd635437(v=exchg.80).aspx)? I doubt the server will return 1M items even if you request it – Panagiotis Kanavos Feb 10 '17 at 14:38
  • PS, this *is* a duplicate until you actually post a call stack that shows otherwise. Post the *full* exception, including the call stack. You can get this easily with `Exception.ToString()`. Even if the exception is thrown by the API, the duplicate applies - *check* the objects, debug the method. Don't just post the code and ask us to guess. – Panagiotis Kanavos Feb 10 '17 at 14:40
  • @PanagiotisKanavos Barring getting all philosophical about what is null. Perhaps if you read the question it might help. Like I said this is a scratch code and after a bunch of testing I am using a for loop. Now there is no null in the code anywhere. But this is being returned by the **FindItemsResults findResults = E_SERVICE.FindItems(src_fldr_id, new ItemView(1000000));** So in essence the null is appearing when the folder is being queried on the mailbox. The question is where the null is coming from as it is not being generated by the code! – RR_SH Feb 10 '17 at 14:43
  • @PanagiotisKanavos the Exception is what has been posted! Object reference not set to an instance of an object. This is not a duplicate until someone can explain why EWS would classify some items as null and carry on processing the messages. – RR_SH Feb 10 '17 at 14:45
  • @RR_SH you only posted the message, not the full exception. The full exception is returned by `Exception.ToString()` and contains the entire call stack. It should be at least 10 lines long. There is nothing philosophical about this. Either one of the items is null, or you have a paged operation and trying to access an item beyond the current page. Otherwise, *why* are you using `TotalCount` instead of `foreach` or `Enumerable.Count()` ? – Panagiotis Kanavos Feb 10 '17 at 14:46
  • @RR_SH the call stack is what will tell you where the null was first encountered. If it is something wrong in the API itself, it will show up in the call stack. If one of the items is null, well, check for nulls. Why was it generated? You did ask for 1M items at once. The server almost certainly has a hard paging limit. Check the paging-related properties. Perhaps the server is already telling you that you shouldn't go beyond `NextPageOffset` without requesting the next page – Panagiotis Kanavos Feb 10 '17 at 14:50
  • Here we are: Item 0 Error: System.NullReferenceException: Object reference not set to an instance of an object. at EWS_MoveFolderwContents.Program.MoveItemsInternal(FolderId fldr_id, String source, String target) in C:\[TRUNCATED]\Program.cs:line 122 Item 24 Error: System.NullReferenceException: Object reference not set to an instance of an object. at EWS_MoveFolderwContents.Program.MoveItemsInternal(FolderId fldr_id, String source, String target) in C:\[TRUNCATED]\Program.cs:line 122 But in essence this is all I get in the exception. No explanation on why it says some items are null! – RR_SH Feb 10 '17 at 14:55
  • @PanagiotisKanavos I am using the method in my code for trouble shooting as opposed to using TotalCounts and foreach. As I said in my post! – RR_SH Feb 10 '17 at 14:57
  • @PanagiotisKanavos If one of the items is null, well, check for nulls. Well there are no nulls as I can see the items there! It can't be the NextPageOffset either as it gets past these errors as long as it has been handled. Which it does! – RR_SH Feb 10 '17 at 14:59
  • @PanagiotisKanavos Please see my edit above. I hope this now starts to make sense! – RR_SH Feb 10 '17 at 15:41
  • For those of you interested in an answer in the future please see: http://stackoverflow.com/questions/42206187/ews-finditemsresultsitem-item-move-does-not-move-certain-item-types-to-a-mai – RR_SH Feb 13 '17 at 15:25
  • @PanagiotisKanavos see the answer here: **[link]http://stackoverflow.com/questions/42206187/ews-finditemsresultsitem-item-move-does-not-move-certain-item-types-to-a-mai** Can you please unmark this as a duplicate or change the link! – RR_SH Feb 13 '17 at 15:27

0 Answers0