Get printerCreateOperation while registering the printer using java msal?

%3CLINGO-SUB%20id%3D%22lingo-sub-2273053%22%20slang%3D%22en-US%22%3EGet%20printerCreateOperation%20while%20registering%20the%20printer%20using%20java%20msal%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2273053%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20All%2C%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20register%20a%20printer%20in%20the%20universal%20print%20service%20using%20the%20Java%20SDK%20and%20I%20have%20been%20following%20this%20documentation%20-%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Fprinter-create%3Fview%3Dgraph-rest-beta%26amp%3Btabs%3Djava%23request%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fgraph%2Fapi%2Fprinter-create%3Fview%3Dgraph-rest-beta%26amp%3Btabs%3Djava%23request%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20am%20able%20to%20register%20the%20printer%20to%20the%20UP%20service.%20However%2C%20I%20need%20to%20know%20that%20the%20registration%20has%20been%20completed.%20The%20documentation%20mentioned%20above%20says%20that%20a%26nbsp%3BprinterCreateOperation%26nbsp%3Bobject%20will%20be%20returned%20as%20a%20response%20which%20can%20be%20used%20to%20track%20the%20status%20of%20the%20registration.%20But%20as%20far%20as%20I%20understand%20it%20would%20be%20returned%20only%20if%20I%20registered%20the%20printer%20via%20Http%20and%20not%20if%20I%20used%20java.%20Can%20anybody%20confirm%20this%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20a%20lot%20in%20advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional 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.