admin管理员组

文章数量:1122832

I'm building a Graph-Connector using msgraph-sdk-java to create connection/schema.
According to documentation an ExternalConnection has a SearchSettings property which then holds a collection of DisplayTemplate as search result templates.
I'm struggling to pass such an DisplayTemplate to the ExternalConnection.

The following approach throws an error (com.microsoft.graph.models.odataerrors.ODataError: The request is malformed or incorrect.):

ExternalConnection externalConnection = new ExternalConnection();
externalConnection.setId(connectionId);
externalConnection.setName(connectionName);

DisplayTemplate template = new DisplayTemplate();
template.setId("testTemplate");
template.setLayout(new UntypedString("{\"type\": \"AdaptiveCard\",\"version\": \"1.0\",\"body\": [{\"type\": \"TextBlock\",\"text\": \"A contoso ticket.\"}]}"));
SearchSettings settings = new SearchSettings();
settings.setSearchResultTemplates(List.of(template));
externalConnection.setSearchSettings(settings);

graphClient.external().connections().post(externalConnection);

For the Schema there is a separated API call, but I didn't find anything accordingly for the DisplayTemplate:

graphClient.external().connections().byExternalConnectionId(connectionId).schema().patch(schema);

There is a Manage search results layout in the documentation but it only shows how to read data.

I'm building a Graph-Connector using msgraph-sdk-java to create connection/schema.
According to documentation an ExternalConnection has a SearchSettings property which then holds a collection of DisplayTemplate as search result templates.
I'm struggling to pass such an DisplayTemplate to the ExternalConnection.

The following approach throws an error (com.microsoft.graph.models.odataerrors.ODataError: The request is malformed or incorrect.):

ExternalConnection externalConnection = new ExternalConnection();
externalConnection.setId(connectionId);
externalConnection.setName(connectionName);

DisplayTemplate template = new DisplayTemplate();
template.setId("testTemplate");
template.setLayout(new UntypedString("{\"type\": \"AdaptiveCard\",\"version\": \"1.0\",\"body\": [{\"type\": \"TextBlock\",\"text\": \"A contoso ticket.\"}]}"));
SearchSettings settings = new SearchSettings();
settings.setSearchResultTemplates(List.of(template));
externalConnection.setSearchSettings(settings);

graphClient.external().connections().post(externalConnection);

For the Schema there is a separated API call, but I didn't find anything accordingly for the DisplayTemplate:

graphClient.external().connections().byExternalConnectionId(connectionId).schema().patch(schema);

There is a Manage search results layout in the documentation but it only shows how to read data.

Share Improve this question edited Nov 21, 2024 at 14:06 flavio.donze asked Nov 21, 2024 at 13:45 flavio.donzeflavio.donze 8,06010 gold badges66 silver badges99 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

UntypedString will produce string, but you need to produce a JSON. It could be achieved by UntypedObject

template.setLayout(new UntypedObject(
    new HashMap<>() {
        {
            put("type", new UntypedString("AdaptiveCard"));
            put("version", new UntypedString("1.0"));                                                  
            put("body", new UntypedArray(Arrays.asList(new UntypedObject(
                new HashMap<>() {
                  {
                    put("type", new UntypedString("TextBlock"));
                    put("text", new UntypedString("A contoso ticket."));
                  }
                }))));
        }
    }));

                                    

After some more research I came accross Serialization helpers.

It seems like it is not possible to create an ExternalConnection, set the SearchSetting and pass the whole object in the post().
ODataError: One or more request precondition not met.

So first create and post() the ExternalConnection, then read the JSON to create the SearchSettings and use patch():

ExternalConnection externalConnection = new ExternalConnection();
externalConnection.setId(connectionId);
externalConnection.setName(connectionName);
graphClient.external().connections().post(externalConnection);

// read search settings and patch the ExternalConnection
try (InputStream in = new FileInputStream("D:/temp/template.json")) {
    SearchSettings searchSettings = KiotaJsonSerialization.deserialize(in, SearchSettings::createFromDiscriminatorValue);
    externalConnection.setSearchSettings(searchSettings);
    graphClient.external().connections().byExternalConnectionId(externalConnection.getId()).patch(externalConnection);
}

An example of a JSON can be found here: searchSettings resource type

本文标签: javahow to configure a DisplayTemplate for ExternalConnection in microsoft Graph APIStack Overflow