EWS FindItemsResults<Item> Item.Move() does not move certain item types to a Mail folder such as IPM.Appointment

落花浮王杯 提交于 2019-12-11 02:37:53

问题


I have some code that is moving items into a Mail Folder from the Deleted Items. The code works well and moves all the items generally. The problem occurs when it encounters an item that is not IPM.Note. It gives the error that is a null reference (See: What is a NullReferenceException, and how do I fix it?)

Which is odd as there are items there and it can't be null.

Here is a code excerpt:

// Specify the Exchange Service
ExchangeService E_SERVICE = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

// Look at the root of the Mailbox (Top of Information Store)
FolderId fldr_id = WellKnownFolderName.MsgFolderRoot;

// Define the folder view
FolderView newFV = new FolderView(1000);

// Perform a deep traversal
newFV.Traversal = FolderTraversal.Deep;

// Get the results of all the folders in the Mailbox
FindFoldersResults f_results = E_SERVICE.FindFolders(fldr_id, newFV);

// Define the source and target folder id variables as null.
FolderId src_fldr_id = null;
FolderId tgt_fldr_id = null;

// Define the folders we are looking to move items from the source to the target
string source = "Deleted Items"
string target = "Old Deleted Items"

// Search through all the folders found in the mailbox
foreach (Folder fldr in f_results)
{
    // If the source folder name is the same as the current folder name then set the source folder ID
    if (fldr.DisplayName.ToLower() == source.ToLower())
    {
        src_fldr_id = fldr.Id;
    }
    // If the target folder name is the same as the current folder name then set the target folder ID
    if (fldr.DisplayName.ToLower() == target.ToLower())
    {
        tgt_fldr_id = fldr.Id;
    }
}

// Get all the items in the folder
FindItemsResults<Item> findResults = E_SERVICE.FindItems(src_fldr_id, new ItemView(1000));

// If the number of results does not equal 0
if (findResults.TotalCount != 0)
{
    // For each item in the folder move it to the target folder located earlier by ID.
    foreach(Item f_it in findResults)
    {
        f_it.Move(tgt_fldr_id);
    }
}

We get the error thrown on the following line:

        f_it.Move(tgt_fldr_id);

This is as a Null Reference Exception which can't be the case as there are items there and it is usually an item that is not IPM.Note.

So how would I go about getting around this and ensure the item gets moved regardless of what type it is?

I have previously posted here about this Unable to move Mail items of different Item classes from the same folder using EWS

Only to be shot down about it being a NullReferenceException when this is not the case!

So any helpful answers would be greatly appreciated.


回答1:


Okay the solution to get around this issue is to ensure you Load() the item before you perform a Move()

make sure you use a try..catch block and handle the exception like below:

try
{
    f_it.Move(tgt_fldr_id);
}
catch (Exception e)
{
    Item draft = Item.Bind(E_SERVICE, f_it.Id);
    draft.Load();
    draft.Move(tgt_fldr_id);
}

This will force the item to load separately and then move it even if it does throw an error. Why, it does this is not known as yet. But should hopefully help those struggling with why you are getting a NullReferenceException

Thanks everyone!

EDIT: You may want to read https://social.msdn.microsoft.com/Forums/exchange/en-US/b09766cc-9d30-42aa-9cd3-5cf75e3ceb93/ews-managed-api-msgsender-is-null?forum=exchangesvrdevelopment on why certain items are Null as this will help you deal with Null items returned a bit better.



来源:https://stackoverflow.com/questions/42206187/ews-finditemsresultsitem-item-move-does-not-move-certain-item-types-to-a-mai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!