ews-java-api icon indicating copy to clipboard operation
ews-java-api copied to clipboard

bug: java.lang.ClassCastException: microsoft.exchange.webservices.data.property.definition.ExtendedPropertyDefinition cannot be cast to microsoft.exchange.webservices.data.property.definition.PropertyDefinition

Open CharlesCJ opened this issue 9 years ago • 8 comments

when I try to get shared calendars with this code :

FindItemsResults<Item> fiResults = fFolders.getFolders().get(0).findItems(cntSearch, iv);

for (Item item : fiResults.getItems()) { try { Object wLinkAddressBookEID = null; item.tryGetProperty(extendedPropertyDefinition,(OutParam<Object>)wLinkAddressBookEID); byte[] ssStoreID = (byte[]) wLinkAddressBookEID;

Throws Exception: java.lang.ClassCastException: microsoft.exchange.webservices.data.property.definition.ExtendedPropertyDefinition cannot be cast to microsoft.exchange.webservices.data.property.definition.PropertyDefinition at microsoft.exchange.webservices.data.core.service.ServiceObject.tryGetProperty(ServiceObject.java:455) at microsoft.exchange.webservices.data.core.service.ServiceObject.tryGetProperty(ServiceObject.java:441)

CharlesCJ avatar Nov 21 '16 02:11 CharlesCJ

Hi, whether this is a bug or not, please provide a complete example fot getting shared calendars ! I've seen CalendarFolder cf = (CalendarFolder) Folder.bind(service, new FolderId(WellKnownFolderName.Calendar, new Mailbox("email@domain"))); code, but it generates (at least for me) a microsoft.exchange.webservices.data.core.exception.service.remote.ServiceResponseException: Folder doesnt exist in information store. Then a few users and I go the hard way through "Common views" and MAPI Extended Properties to get pidTagWlinkEntryId to convert to ewsId abut we then bump into that kind of issue without much help. I think it needs more explanation Thanks in advance.

jgranduel avatar Nov 21 '16 17:11 jgranduel

I've download the source code and fix it. Java is not like C#, there has some class can't cast to, like ExtendedPropertyDefinition cannot be cast to PropertyDefinition.

CharlesCJ avatar Nov 25 '16 01:11 CharlesCJ

Hi, CharlesCJ. I've download the source code. It Throwed Exception. Could you tell me how to fix?

MaxLi001 avatar Mar 13 '17 09:03 MaxLi001

Hi MaxLi001 I changed the code of type casting and type of return. (我改变了强转类型的那段源码,包括返回类型。然后打了个新的jar,修改了2.0的pom以便自己使用)

CharlesCJ avatar Mar 28 '17 03:03 CharlesCJ

Hi, since this bug is still open - do we have some progress related to accessing shared folders in the meantime? Or is there a working example? Thanks

mkerstner avatar Jan 25 '18 23:01 mkerstner

@CharlesCJ ,could you please share your jar, or could you please share the code how to access the shared calendars by user?

Thanks!

troyz avatar Sep 27 '18 02:09 troyz

Hi, after change the method tryGetExtendedProperty from protected to public, then it works.

Below is my code: get email list who have shared calendars with the current user.

private static List<String> getCalendarSharedEmailList(ExchangeService service) throws Exception
{
    List<String> emailList = new ArrayList<String>();
    FolderId rfRootFolderid = new FolderId(WellKnownFolderName.Root);
    FolderView fvFolderView = new FolderView(1000);
    SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Common Views");
    FindFoldersResults ffoldres = service.findFolders(rfRootFolderid, sfSearchFilter, fvFolderView);
    if(ffoldres.getFolders().size() == 1)
    {
    	PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
        ExtendedPropertyDefinition PidTagWlinkAddressBookEID = new ExtendedPropertyDefinition(0x6854, MapiPropertyType.Binary);
        ExtendedPropertyDefinition PidTagWlinkGroupName = new ExtendedPropertyDefinition(0x6851, MapiPropertyType.String);

        psPropset.add(PidTagWlinkAddressBookEID);
        ItemView iv = new ItemView(1000);
        iv.setPropertySet(psPropset);
        iv.setTraversal(ItemTraversal.Associated);
        SearchFilter cntSearch = new SearchFilter.IsNotEqualTo(PidTagWlinkGroupName, "Other Calendars");
        ArrayList<Folder> folderList = ffoldres.getFolders();
        Folder folder = folderList.get(0);
        
        FindItemsResults<Item> fiResults = folder.findItems(cntSearch, iv);
        for (Item itItem : fiResults.getItems())
        {	
        	Object GroupName = null;
        	OutParam<Object> WlinkAddressBookEID = new OutParam<Object>();
            if (itItem.tryGetExtendedProperty(Object.class, PidTagWlinkAddressBookEID, WlinkAddressBookEID))
            {
            	byte[] ssStoreID = (byte[])WlinkAddressBookEID.getParam();
                int leLegDnStart = 0;
                String lnLegDN = "";
                for (int ssArraynum = (ssStoreID.length - 2); ssArraynum != 0; ssArraynum--)
                {
                    if (ssStoreID[ssArraynum] == 0)
                    {
                        leLegDnStart = ssArraynum;
                        lnLegDN = new String(ssStoreID, leLegDnStart + 1, (ssStoreID.length - (leLegDnStart + 2)));
                        ssArraynum = 1;
                    }
                }
                NameResolutionCollection ncCol = service.resolveName(lnLegDN, ResolveNameSearchLocation.DirectoryOnly, false);
                if (ncCol.getCount() > 0)
                {
                	NameResolution nameResolution = ncCol.iterator().next();
                	
                	emailList.add(nameResolution.getMailbox().getAddress());
                }
            }
        }
    }
    return emailList;
}

troyz avatar Sep 28 '18 09:09 troyz

@troyz Thank you for your Hint!

carryjim811 avatar Apr 01 '20 00:04 carryjim811