Null Contacts returned from MAPFolder.Items

  • Thread starter Thread starter leahann
  • Start date Start date
L

leahann

I'm new to OL programming, am using C#, and am having an issue...

We have a Public Folder that contains a Contacts form with
approximately 5,000 contacts. We want to export this to an Excel
report programmatically.


Using Outlook.MAPIFolder.Folders[MY_FOLDER].Items.Count shows the
correct number of items, but when looping through these items after
about 2,000 they all end up being null. I've found a few topics on
this problem, but none seem to address the problem I'm having.


Is there a limit to the number of Items that can be returned from the
folder? Does anyone know a solution for this? ANY help would be
greatly appreciated!!!


My code:


Outlook.MAPIFolder aFolder =
mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllP­ublicFolders).Folders["My

Folder"]


Items items = aFolder.Items;


int itemCount = items.Count;
object item = items.GetFirst();


for(int i = 0; i < itemCount; i++)
{
ContactItem cItem = item as ContactItem;


if (cItem != null)
{
itemInfo[0] = cItem.LastName;
itemInfo[1] = cItem.FirstName;
itemInfo[2] = cItem.CompanyName;
...
}


allItems.Add(itemInfo);
itemInfo = new String[9];


item = items.GetNext();
}



Thanks!

leahann
 
1. If you are using a for (1..Count) loop there is no reason to use
GetFirst/GetNext
for(int i = 1; i <= itemCount; i++)
{
item = items;
ContactItem cItem = item as ContactItem;

2. If you prefer to use GetFirst/GetNext, use a while loop.

3. Try to invoke GC.Collect() at each step of the loop (you can also try 50
or so steps instead) - Exchange has a limit of 255 RPC channels open at any
given time, so you can easily run over teh limit, especially with .Net,
which does not immediately release the COM objects.

4. Keep in mind that you can have distributions lists as well as contacts.
Your code will fail if you hit a DistListItem object instead of ContactItem.


Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool

I'm new to OL programming, am using C#, and am having an issue...

We have a Public Folder that contains a Contacts form with
approximately 5,000 contacts. We want to export this to an Excel
report programmatically.


Using Outlook.MAPIFolder.Folders[MY_FOLDER].Items.Count shows the
correct number of items, but when looping through these items after
about 2,000 they all end up being null. I've found a few topics on
this problem, but none seem to address the problem I'm having.


Is there a limit to the number of Items that can be returned from the
folder? Does anyone know a solution for this? ANY help would be
greatly appreciated!!!


My code:


Outlook.MAPIFolder aFolder =
mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllP­ublicFolders).Folders["My

Folder"]


Items items = aFolder.Items;


int itemCount = items.Count;
object item = items.GetFirst();


for(int i = 0; i < itemCount; i++)
{
ContactItem cItem = item as ContactItem;


if (cItem != null)
{
itemInfo[0] = cItem.LastName;
itemInfo[1] = cItem.FirstName;
itemInfo[2] = cItem.CompanyName;
...
}


allItems.Add(itemInfo);
itemInfo = new String[9];


item = items.GetNext();
}



Thanks!

leahann
 
Well, isn't it always the simple thing that ends up being the solution?
I invoked the GC and it looks as though everything is coming back now.
(Oh, and the for loop was a lazy leftover from several attempts at
accessing the items in different ways, namely a direct assign.) Thanks
for your help!

leahann
 
Back
Top