0

I'm trying to see if there is a way to sort contacts by Surname in EWS after they have already been imported into the contacts folder.

I had a member from this site assist me with some code to import a list of contacts using the ThreadJob Module. As an overview of what my code looks lie, you can please refer to this link: Contacts import using runspacepools

The modified code from the link above works great and it imports the contacts in half the time (originally took between 5-6 mins and afterwards between 2-3 mins). The new issue I am facing now is that since the import runs in different thread jobs it imports contacts out of order, so I'm curious if there is a easy way to tell Exchange to resort them by SurName? Any thoughts?

I am open to using EWS/EWS API/ Graph API, or any other method you can think of. I would need to sort this for all users mailboxes.

Thank you!

Koobah84
  • 185
  • 2
  • 12

1 Answers1

0

It looks like your not setting the FileAs property in you code which means you will just get the default FileAs order in Outlook or OWA. eg in the Graph you post should include that property eg

POST https://graph.microsoft.com/v1.0/me/contactFolders/{id}/contacts
Content-type: application/json
Content-length: 210

{
  "parentFolderId": "parentFolderId-value",
  "birthday": "datetime-value",
  "fileAs": "fileAs-value",
  "displayName": "displayName-value",
  "givenName": "givenName-value",
  "initials": "initials-value"
}

Usually you would do something like Surname,FirstName

Your question is a little confusing however because you mention EWS which doesn't sort items by default it will just return then in FIFO order so the order in which they where created. You can use OrderBy in EWS with an ItemView https://learn.microsoft.com/en-us/dotnet/api/microsoft.exchange.webservices.data.itemview.orderby?redirectedfrom=MSDN&view=exchange-ews-api#Microsoft_Exchange_WebServices_Data_ItemView_OrderBy

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • Thank you. That was exactly what I was looking for. Sorry for confusing you with the verbiage on my question. I know I was using\trying different techniques, but I think out of all the ones I tried, the MS Graph API seemed the best and most efficient. By the way, I have seen some of your work online! Great stuff! From what you've seen in the code in the link I posted, would you say that's a good way to import contacts to Exchange, or is there a better way? Is using threadJob vs runspacepools much different/perform better? – Koobah84 Jun 09 '21 at 02:30
  • If its working for you and the time its taking is acceptable that's all that matters, You could use batching in the Contact creation which would probably speed things up a little eg you only creating one contact per POST request you could do up to 20 in dependson batch or 4 concurrently (with EWS you could do around 50-100 in a single batch but you probably don't need that higher throughput). I doubt your threading method is making very much difference (I've always used runspacepools) the slowest part is going to be all the single post requests to create the Contacts. – Glen Scales Jun 09 '21 at 05:56
  • Thanks. The code in my current case is acceptable, but it only runs for me in my current test environment because I'm only using it on myself; whereas I would have to do the same thing for 500 other users mailboxes. Do you have any examples of how I could do the 20 POST requests; or how it can be done using EWS with 50-100 batches? I'd like to try all options and pick the one that would run the fastest for me. – Koobah84 Jun 09 '21 at 14:17