Link to home
Start Free TrialLog in
Avatar of Lob5t3r
Lob5t3r

asked on

How do I access the public folders through Exchange Web Services in SQL 2008 R2

Hi I am trying to implement this sample which can be downloaded here Using SQL Server Table-Valued User-defined Functions with Exchange Web Services

I have successfully built the sample and have been able to return my Contacts in my mailstore to the view in SQL.

I am trying to modify the sample to return the contacts in the public folders sub folder '\Contacts'

This function returns my contacts.
internal override StringBuilder generateSoap()
		{
			StringBuilder soapReq = new StringBuilder(200);
			soapReq.Append(ExchangeWebServiceHelper.getSoapHdr());
			soapReq.Append("  <soap:Body>");   
			soapReq.Append("    <m:FindItem Traversal=\"Shallow\">");
			soapReq.Append("      <m:ItemShape>");
			soapReq.Append("        <t:BaseShape>AllProperties</t:BaseShape>");
			soapReq.Append("      </m:ItemShape>");
			soapReq.Append("      <m:ParentFolderIds> ");
                        soapReq.Append("        <t:DistinguishedFolderId Id=\"contacts\"/> ");
			soapReq.Append("      </m:ParentFolderIds>");
			soapReq.Append("    </m:FindItem>");
			soapReq.Append("  </soap:Body>");   
			soapReq.Append("</soap:Envelope>");   
			return soapReq;
		}

Open in new window



soapReq.Append("        <t:DistinguishedFolderId Id=\"publicfoldersroot\"/> ");

Open in new window


I've attempted modifying this line to publicfoldersroot which when I try to run my query on the contacts view returns nothing I believe this is because the actual root of public folders has no items of the type contacts they are in a subfolder.




ExchangePublicFolderContacts.png
Avatar of Lob5t3r
Lob5t3r

ASKER

I was able to get the folderId and changekey in another project using Hunting those elusive Public Folders using Exchange Web Services

this worked i was able to step through and confirm that the code had located the right folder with 4441 contacts inside.

FolderId: "AAEuAAAAAAAaRHOQqmYRzZvIAKoAL8RaAwC4GC889z9fRKyshDuqQrE1AAAARzI6AAA="
ChangeKey: "AwAAABYAAAC4GC889z9fRKyshDuqQrE1AAAATaLt"

So I attempted to point the SOAP code which I am quite unfamiliar with directly at :
soapReq.Append("      <m:ParentFolderIds> ");
                        soapReq.Append("      <t:FolderId Id=\"AAEuAAAAAAAaRHOQqmYRzZvIAKoAL8RaAwC4GC889z9fRKyshDuqQrE1AAAARzI6AAA=\" ChangeKey=\"AwAAABYAAAC4GC889z9fRKyshDuqQrE1AAAATaLt\" />");        
			soapReq.Append("      </m:ParentFolderIds>");

Open in new window



		
internal override StringBuilder generateSoap()
		{
			StringBuilder soapReq = new StringBuilder(200);
			soapReq.Append(ExchangeWebServiceHelper.getSoapHdr());
			soapReq.Append("  <soap:Body>");   
			soapReq.Append("    <m:FindItem Traversal=\"Shallow\">");
			soapReq.Append("      <m:ItemShape>");
			soapReq.Append("        <t:BaseShape>AllProperties</t:BaseShape>");
			soapReq.Append("      </m:ItemShape>");
			soapReq.Append("      <m:ParentFolderIds> ");
                        soapReq.Append("      <t:FolderId Id=\"AAEuAAAAAAAaRHOQqmYRzZvIAKoAL8RaAwC4GC889z9fRKyshDuqQrE1AAAARzI6AAA=\" ChangeKey=\"AwAAABYAAAC4GC889z9fRKyshDuqQrE1AAAATaLt\" />");        
			soapReq.Append("      </m:ParentFolderIds>");
			soapReq.Append("    </m:FindItem>");
			soapReq.Append("  </soap:Body>");   
			soapReq.Append("</soap:Envelope>");   
			return soapReq;
		}

Open in new window


Which returned an empty table one step closer but no cigar :( lol.
ASKER CERTIFIED SOLUTION
Avatar of LeeDerbyshire
LeeDerbyshire
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lob5t3r

ASKER

Hi thank you for your response I removed the changekey from the query and it still returned nothing I am wondering if the section <m:ParentFolderIds> works with <t:FolderId the way i set it up? The account I am using with the soapClient in the sample code has owner permissions on the public folders.

soapReq.Append("      <m:ParentFolderIds> ");
soapReq.Append("      <t:FolderId Id=\"AAEuAAAAAAAaRHOQqmYRzZvIAKoAL8RaAwC4GC889z9fRKyshDuqQrE1AAAARzI6AAA=\" />");
soapReq.Append("      </m:ParentFolderIds>");

Open in new window




The sub folder Contacts has the same DefaultMessageClass of IPM.Contact as my personal contacts where ive done some tests of over 300 contacts and they return fine i'm going to test copying the entire contacts folder to my contacts to see if its a problem with quantity.
Avatar of Lob5t3r

ASKER

Well it looks as though it will not return more than a 1000 Contacts do I accept your answer and open a new question asking how to retrieve more than a 1000 Contacts through Exchange Web services?
Avatar of Lob5t3r

ASKER

By adding this line I was able to finally return the contacts from my public folders contacts folder but it seems even though i told it that MaxEntriesReturned=5000 it still only returns the first 1000 Contacts
soapReq.Append("      <m:IndexedPageItemView MaxEntriesReturned=\"5000\" BasePoint=\"Beginning\" Offset=\"0\" />");

Open in new window


Sorry for the delay - I live in the UK, and it's evening here.  I don't know why it only returns 1000 items, but you sometimes find strange restrictions like that when you use AllProperties (for instance, it will only return the first 1000 characters of a message body for a FindItem - to get the full message you need to do GetItem).  Try changing it to Default, and see if that helps.
Avatar of Lob5t3r

ASKER

Hi LeeDerbyshire I did some searching and found out about the ThrottlingPolicy which by default is set to a 1000 thank you for your assistance :)
https://blogs.msdn.com/b/exchangedev/archive/2010/03/12/throttling-policies-and-the-ewsfindcountlimit.aspx