javamicrosoft-graph-apimicrosoft-graph-sdks

how to list ExternalItems from a graph-connector


I'm building a Graph-Connector using msgraph-sdk-java to create connection/schema and create ExteralItems.

I want to build functionality to delete all ExternalItems created by our connector. The problem is that I'm not able to get a list of ExternalItems or a list of ids of the ExternalItems. I'm able to receive an ExternalItem by id but no list of all items.

This approach returns null:

getGraphClient().external().connections().byExternalConnectionId(connectionId).get().getItems()

This approach throws an ODataError UnknownError:

getGraphClient().external().connections().byExternalConnectionId(connectionId).items().get().getValue()

This approach throws: "Forbidden","message":"Application permission is only supported for the following entity types:site, list, listItem, drive and driveItem.":

QueryPostRequestBody queryPostRequestBody = new QueryPostRequestBody();
List<SearchRequest> requests = new LinkedList<>();
SearchRequest searchRequest = new SearchRequest();
LinkedList<EntityType> entityTypes = new LinkedList<>();
entityTypes.add(EntityType.ExternalItem);
searchRequest.setEntityTypes(entityTypes);
LinkedList<String> contentSources = new LinkedList<>();
contentSources.add("/external/connections/" + connectionId);
searchRequest.setContentSources(contentSources);
SearchQuery query = new SearchQuery();
query.setQueryString("*");
searchRequest.setQuery(query);
searchRequest.setRegion("CHE");
requests.add(searchRequest);
queryPostRequestBody.setRequests(requests);
var result = getGraphClient().search().query().post(queryPostRequestBody);

How should I receive the ExternalItems? Is there another way to delete all ExternalItems of the Connector without deleting the complete connector?


Solution

  • You can delete an external item only if you know it's id.

    When you create an external item, you specify the item's id. You will have to store all items ids somewhere.

    Other option is to use /search/query to search for external items and return the substrateContentDomainId property in response. You can parse the value and take the substring after the comma , which is the itemId of the Graph connector item. The limitation is that /search/query doesn't support application permissions for externalItem. If you try to search on behalf of signed-in user (with delegated permissions), the user must have access to all items.

    POST https://graph.microsoft.com/v1.0/search/query
    {
        "requests": [
            {
                "entityTypes": [
                    "externalItem"
                ],
                "contentSources": [
                    "/external/connections/<connection_id>"
                ],
                "query": {
                    "queryString": "*"
                },
                "from": 0,
                "size": 3,
                "fields": [
                    "substrateContentDomainId"
                ]
            }
        ]
    }
    

    So there is no easy solution for this if you want to avoid deleting of the entire connector.