Tuesday, September 26, 2023

SharePoint Online - PnP.Core sample to check CopyJobProgress via Azure Storage Queue SDK for .Net

This is the continuation of the last post in this blog related to checking the status of CopyJobs in SharePoint Online. The last post has code snippets using PnP.Framework. It is not a complete compilable and working sample. This post is to introduce a functional example. The only difference is that this sample is using PnP.Core SDK.

If anyone wondering what is PnP.Framework and PnP.Core, please have a look at the dilemma of choosing .Net SDK to interact with SharePoint Online.

Sample located at https://github.com/dotnet-demos/sharepoint-createcopyjob-azure-queue-tracking.

Points of interest

Though the code has a lot of comments, below are the areas that may bring some questions.

Getting the job status

There is no way to get the Copy Job status in PnP.Core SDK. Even there is no plan to implement that feature. Feature request #1277 was closed.

So either we need to switch to PnP.Framework SDK or make a direct HTTP call. In the sample, we can see it is making a direct HTTP call.

Determining the JSON request body for direct HTTP calls

If we refer to any documentation related to SharePoint migration API, it is referring to the SDK which is nothing but .Net classes and methods. No HTTP request-response level documentation is available. The PnP.Framework SDK uses XML to craft the request payload and the response is JSON. That request XML is the hardest thing in the world to understand. Luckily the PnP.Core SDK uses JSON for request and response. Though there is a tutorial on basic operations to SharePoint REST endpoints, it is difficult to craft the JSON request to do a simple operation such as GetMigrationJobStatus(guide jobId).

Raw HTTP call to GetMigrationJobStatus(guide jobId).

The official documentation is there in 2 places. But in both places, it is talking about .Net SDK usage, not the HTTP-level request response.

First, we have to decide what should be the HTTP method. Common sense tells us this should be GET as its read operation. Url should be something like _api/site/GetMigrationJobStatus/{JobId}
We are totally wrong. It should be HTTP POST.

Now how should we build the POST body to include the jobId? Just the id in the POST body or we should have an object as below?
{
"jobId":"{jobId}"
}

Nothing is gonna work except the below one.

{
"id":"{jobId}"
}

Refer to the GetJobStatus() in the example.

Have fun with SharePoint. Also please give PR or create an issue in case of any improvements to be made.

No comments: