EWS FindItemsResults< Item> Item.Move()不会将某些项目类型移动到IPM之类的Mail文件夹中 [英] EWS FindItemsResults<Item> Item.Move() does not move certain item types to a Mail folder such as IPM.Appointment

查看:132
本文介绍了EWS FindItemsResults< Item> Item.Move()不会将某些项目类型移动到IPM之类的Mail文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以将项目从已删除邮件"移到邮件文件夹"中.该代码运行良好,通常可以移动所有项目.当遇到非IPM.Note的项目时,会发生此问题.它给出的错误是空引用(请参阅:什么是NullReferenceException,我该如何解决?)

这很奇怪,因为那里有物品,而且不能为空.

这是一段代码摘录:

// 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);
    }
}

我们在以下行上引发了错误:

        f_it.Move(tgt_fldr_id);

这是一个Null引用异常,因为那里有项目,而且通常不是IPM的项目,所以不会出现这种情况.

那么我该如何解决这个问题,并确保不管它是什么类型,都可以移动它?

我以前曾在此处发布过有关此解决方案

好的解决此问题的方法是确保在执行Move()之前先加载()项目,然后执行Move()

确保使用try..catch块并处理如下异常:

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);
}

这将迫使该项目分别加载,然后移动它,即使它确实引发了错误.为什么这样做呢,目前尚不知道.但是希望能帮助那些苦苦挣扎的人,为什么你会得到一个 NullReferenceException

谢谢大家!

编辑:您可能需要阅读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.

解决方案

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.

这篇关于EWS FindItemsResults&lt; Item&gt; Item.Move()不会将某些项目类型移动到IPM之类的Mail文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆