0

Looking at how SharePoint move items using the Network tab in chrome, the items get moved using _api/site/CreateCopyJobs. This returns result in

type=Collection(SP.CopyMigrationInfo)

This contains a copyJobInfo

  • EncryptionKey
  • JobId
  • JobQueueUrl

Then by calling /_api/site/GetCopyJobProgress passing in the above three values in copyJobInfo it returns status and logs for the item.

When monitoring this through Chrome network tools, SharePoint performs the GetCopyJobProgress in a $batch call.

> --batch_434116b9-0be7-4374-912f-0da1386a707e Content-Type: multipart/mixed;
> boundary="changeset_7a9d393f-e308-4e29-8285-c604908ec20e"
> Content-Length: 3648 Content-Transfer-Encoding: binary
> 
> --changeset_7a9d393f-e308-4e29-8285-c604908ec20e Content-Type: application/http Content-Transfer-Encoding: binary
> 
> POST
> https://xxxxxxxxxx.sharepoint.com/sites/AugustTest/_api/site/GetCopyJobProgress
> HTTP/1.1 Content-Type: application/json;odata=verbose accept:
> application/json;odata=verbose
> 
> {"copyJobInfo":{"__metadata":{"type":"SP.CopyMigrationInfo"},"EncryptionKey":"vqeFkaav+1991IjC1AfCIsVpx3AIbUMqMyvmx4aq6CDQ=","JobId":"1e3cd7a3-12e7-4c07-831e-0c848d89f6f2","JobQueueUri":"https://spolonmmem011pr.queue.core.windows.net:443/1988pq20180829-55ea1fd525024922bf80f2d6499f0136?sv=2017-07-29&sig=DXLw4RvTTVDqw8a8q1Z2ZEOcQVTdNFsfTzGjNSMq6aQ%3D&st=2018-08-28T07%3A00%3A00Z&se=2018-09-19T07%3A00%3A00Z&sp=rap"}}
> 
> --changeset_7a9d393f-e308-4e29-8285-c604908ec20e
> --batch_434116b9-0be7-4374-912f-0da1386a707e--

In C# I've be able to move the items, but now I want to do the checking in a batch similar to how Sharepoint does it. Therefore I been looking at code and example of using the $batch call. One example is at the following link

https://github.com/SharePoint/PnP/tree/master/Samples/Core.ODataBatch

So I've been looking at replicating in C# a batch call. My code is below, (I'm only trying to call the batch code using one entry first.)

 var baseUrl = url + "/_api/";
            var batchRequest = new BatchODataRequest(baseUrl);
            batchRequest.SetHeader("Authorization", "Bearer " + localAccessToken);

            //Message Writer
            var oDataMessageWriter = new ODataMessageWriter(batchRequest);
            var oDataBatchWriter = oDataMessageWriter.CreateODataBatchWriter();
            oDataBatchWriter.WriteStartBatch();
            oDataBatchWriter.WriteStartChangeset();


            var checkProgressUri = new Uri(url + "/_api/site/GetCopyJobProgress");
                var request = oDataBatchWriter.CreateOperationRequestMessage("POST", checkProgressUri);
                request.SetHeader("Accept", "application/json;odata=verbose");
                request.SetHeader("Content-Type", "application/json;odata=verbose");

                var copyJobInfoPayload = JsonConvert.SerializeObject(new
                {
                    copyJobInfo = migrationInfo.CopyJobInfo
                }, Formatting.Indented);

                //var copyJobInfoPayload = JsonConvert.SerializeObject(new
                //{
                //    migrationInfo.CopyJobInfo.EncryptionKey,
                //    migrationInfo.CopyJobInfo.JobId,
                //    migrationInfo.CopyJobInfo.JobQueueUri

                //});

                using (var oDataInsertWriter = new ODataMessageWriter(request, new ODataMessageWriterSettings() { AutoComputePayloadMetadataInJson = false }))
                {
                    var entryWriter = oDataInsertWriter.CreateODataEntryWriter();

                     var insertBody = new ODataEntry()
                    {
                        Properties = new[]{ new ODataProperty() { Name = "copyJobInfo", Value = copyJobInfoPayload }
                    }

                    entryWriter.WriteStart(insertBody);
                    entryWriter.WriteEnd();
                }
                //close batch and perform call
                oDataBatchWriter.WriteEndChangeset();
                oDataBatchWriter.WriteEndBatch();
                oDataBatchWriter.Flush();


     oDataMessageWriter.Dispose();

                    //Get Response
                    var oDataResponse = batchRequest.GetResponse();
using (var oDataReader = new ODataMessageReader(oDataResponse))
                    {
                        var oDataBatchReader = oDataReader.CreateODataBatchReader();
                        while (oDataBatchReader.Read())
                        {
                            switch (oDataBatchReader.State)
                            {
                                case ODataBatchReaderState.Initial:
                                    //Optionally handle the start of a batch payload;
                                    break;

                                case ODataBatchReaderState.Operation:
                                    //Start of an operation (either top-level or in a changeset)
                                    var operationResponse = oDataBatchReader.CreateOperationResponseMessage();
                                    using (var stream = operationResponse.GetStream())
                                    {

After this point my operationResponse.status is always 500.

>     Content-Type: multipart/mixed; boundary=batch_82013134-f7bd-4480-80c6-a10f077eb784 Content-Length: 924 accept:
> application/json;odata=verbose
> 
> --batch_82013134-f7bd-4480-80c6-a10f077eb784 Content-Type: multipart/mixed;
> boundary=changeset_73e68b20-08c1-42ee-acaa-e9e9cb904e04
> 
> --changeset_73e68b20-08c1-42ee-acaa-e9e9cb904e04 Content-Type: application/http Content-Transfer-Encoding: binary
> 
> POST
> https://xxxxxxx.sharepoint.com/sites/AugustTest/_api/site/GetCopyJobProgress
> HTTP/1.1 Accept: application/json;odata=verbose Content-Type:
> application/json;odata=nometadata
> 
> {"copyJobInfo":"{\"EncryptionKey\":\"neYosQpZhHqdoWRRwZa/uE8MbEiwsFVUuWLSwizCrrQ=\",\"JobId\":\"d77c9316-46dc-4c56-1234-caa6e522486b\",\"JobQueueUri\":\"https://spolonmmem005pr.queue.core.windows.net:443/1988pq20180829-0505f8168d44426c99b605eea525acbd?sv=2017-07-29&sig=gb97yh5VotQ3R7jWc7c1PpSH1ECX3HZq6qk60qyYlXg%3D&st=2018-08-28T07%3A00%3A00Z&se=2018-09-19T07%3A00%3A00Z&sp=rap\"}"}
> --changeset_73e68b20-08c1-42ee-acaa-e9e9cb904e04--
> --batch_82013134-f7bd-4480-80c6-a10f077eb784--

In the example above, the 500 message is An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected.

I've tried loads of different variations and unable to get anywhere with this. I've tried to add "__metadata":{"type":"SP.CopyMigrationInfo"}. I also find it strange that the JSON quotes in the string is escaped with \" everywhere.

Is it even possible using Microsoft.Data.OData?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.