Get printerCreateOperation while registering the printer using java msal?

Copper Contributor

Hello All,

I am trying to register a printer in the universal print service using the Java SDK and I have been following this documentation - https://docs.microsoft.com/en-us/graph/api/printer-create?view=graph-rest-beta&tabs=java#request

I am able to register the printer to the UP service. However, I need to know that the registration has been completed. The documentation mentioned above says that a printerCreateOperation object will be returned as a response which can be used to track the status of the registration. But as far as I understand it would be returned only if I registered the printer via Http and not if I used java. Can anybody confirm this ?

 

Thanks a lot in advance.

 

2 Replies
Hi Himan Jo,
Thanks for reaching out.
I'm the main maintainer of the Microsoft Graph Java SDK.
The reason why the SDK is not returning any information as of now is because the create method is described as an action, and actions don't have a return type (search for Parameter Name="bindingParameter" Type="Collection(graph.printer)" in https://graph.microsoft.com/beta/$metadata )
I've let the product team know they should address this issue, either by correcting the documentation and removing the response payload or by using a Function with a return type instead so the code generated in the SDK will have a return type too.

In the meantime and as a workaround, you should be able to use a custom request like this:
final PrinterCreateParameterSet result = new CustomRequest<PrinterCreateParameterSet>("https://graph.microsoft.com/beta/print/printers/create", graphClient, null, PrinterCreateParameterSet.class).post(
PrinterCreateParameterSet
.newBuilder()
.withDisplayName(displayName)
.withManufacturer(manufacturer)
.withModel(model)
.withPhysicalDeviceId(physicalDeviceId)
.withHasPhysicalDevice(hasPhysicalDevice)
.withCertificateSigningRequest(certificateSigningRequest)
.withConnectorId(connectorId)
.build());
Hi HimanJo,

The printer-create action is a long running operation. Hence, it returns an empty response body and a URL(in 'Operation-Location' header) to track the status of the operation.
Due to limitation in Graph SDK, you can't directly fetch the response headers for POST request that returns empty response body.
There's a workaround for that. You can get request from Graph SDK:
graphClient.print().printers()
.create(PrinterCreateParameterSet
.newBuilder()
.withDisplayName(displayName)
.withManufacturer(manufacturer)
.withModel(model)
.withPhysicalDeviceId(physicalDeviceId)
.withHasPhysicalDevice(hasPhysicalDevice)
.withCertificateSigningRequest(certificateSigningRequest)
.withConnectorId(connectorId)
.build())
.buildRequest().getHttpRequest();

Then you can use native client to get response, similar to this:
https://github.com/microsoftgraph/msgraph-sdk-java-core#3-make-requests-against-the-service

After fetching the operationId from "Operation-Location" header, you can get operation:
PrintOperation printOperation = graphClient.print().operations("{printOperationId}")
.buildRequest()
.get();

This return type is PrintOperation, however, it will return PrinterCreateOperation (which inherits from PrintOperation).
Hence, you can explicitly cast printOperation to PrinterCreateOperation.