pnpjs icon indicating copy to clipboard operation
pnpjs copied to clipboard

Support for addCopyFromContentTypeHub

Open jpalo opened this issue 2 years ago • 7 comments

Category

  • [X] Enhancement
  • [ ] Bug
  • [X] Question
  • [ ] Documentation gap/issue

Please specify what version of the library you are using: [2.13.0]

Please specify what version(s) of SharePoint you are targeting: [Online]

Expected / Desired Behavior / Question

  1. Content Type adding has changed, nowadays you need to use REST API v2.1 and addCopyFromContentTypeHub to add Content Types to a list.Is it currently only doable by using spPost? Does anyone happen to have a functioning spPost sample how to do it? Tracing how SPO does it, it is using batch, but before I start to investigate it in detail and guessing how it works, maybe someone has already done it.

image

  1. More importantly I'm trying to add content types on a list on another site collection and PnPJS addAvailableContentType doesn't work. If I add CT via browser (as it is there), then call the same API, it returns CT details, so the query is fundamentally correct. Site has existed a long time and I can see the CTs on the site via browser.

image

Thank you.

jpalo avatar May 19 '22 13:05 jpalo

So, when you see things implemented in that endpoint that would be exposed in the graph, so I think the correct way forward would be to implement using that api... https://docs.microsoft.com/en-us/graph/api/contenttype-addcopyfromcontenttypehub?view=graph-rest-1.0&tabs=http

That said we have not done so in our graph library so this would need to be an enhancement. You can still make the calls as you can use the graphPost method (i've given samples out in other issues), or you could use the Microsoft Graph SDK.

juliemturner avatar May 19 '22 14:05 juliemturner

I'm now attempting to make a call to:

https://xyz.sharepoint.com/sites/projectfolders_test_1/_api/v2.1/sites/ff673fd4-6eb1-4828-a407-340aae66b8c8/lists/e08f4aeb-d319-49d8-b98e-747de20803be/contentTypes/addCopyFromContentTypeHub

Payload being the familiar:

{"contentTypeId":"0x010100958E2DA38BA8BF4893C277E968ED5C32"}

and it works great if I copy&paste the request SPO makes from browser DEV tools to Postman, but calling that using spPost gives error:

code: "notAllowed"
message: "OAuth only flow is enabled and the call has not been issued from an app."

Code in question:

for (let i = 0; i < aCts.length; i++) {
    await spPost(Folder(webUrl, `/_api/v2.1/sites/${destinationWeb.Id}/lists/${destinationList.Id}/contentTypes/addCopyFromContentTypeHub`),
    body({
        contentTypeId: aCts[i]                
    }));
}

How should I make the call in my SPFx web part?

jpalo avatar May 20 '22 08:05 jpalo

How do you use graphPost with addCopyFromContentTypeHub, cannot find any documentation? And can a normal user (member on a SPO site) even call this method? Does it exist on 2.13.0?

jpalo avatar May 20 '22 12:05 jpalo

It's not something we document in our documentation per say but it is a work around which I shared with others in issues -- eg. #1955

If a user can call a REST method successfully is dependent on two things, one that they have permission to do the thing that the REST call does and two that the token issued grants them access to that. Can I assume you're using SPFx? If yes, then to make sure the token you have can allow the user who has access to the thing you want them to do, to work, you would need to request the appropriate permissions for the graph endpoint in your package-solution.json file and in this case based on the link I shared with you above that would be either Sites.Manage.All or Sites.FullControl.All. If the user who executes the code doesn't have the right (or any) access to the site you're trying to execute the REST call on even if you set this up it will fail... same as any other SharePoint REST endpoint.

juliemturner avatar May 20 '22 12:05 juliemturner

Now getting this error, is this something that can be centrally consented by admin for all users?

AADSTS65001: The user or administrator has not consented to use the application with ID 'c5b145f3-05a6-4cd1-b336-0509ecbde1ca' named 'SharePoint Online Client Extensibility Web Application Principal'. Send an interactive authorization request for this user and resource.

Current code:

            await graphPost(GraphQueryable(webUrl, `/_api/v2.1/sites/${destinationWeb.Id}/lists/${destinationList.Id}/contentTypes/addCopyFromContentTypeHub`),
                {
                    body: JSON.stringify(
                        {
                            contentTypeId: aCts[i]
                        }
                    )
                });

jpalo avatar May 20 '22 12:05 jpalo

Then you probably skipped this part of my message:

Can I assume you're using SPFx? If yes, then to make sure the token you have can allow the user who has access to the thing you want them to do, to work, you would need to request the appropriate permissions for the graph endpoint in your package-solution.json file and in this case based on the link I shared with you above that would be either Sites.Manage.All or Sites.FullControl.All. If the user who executes the code doesn't have the right (or any) access to the site you're trying to execute the REST call on even if you set this up it will fail... same as any other SharePoint REST endpoint.

If you read the information in the link I shared that should help you resolve the issue.

juliemturner avatar May 20 '22 13:05 juliemturner

Looks like requesting these permissions cannot be done via workbench, so had to to add the webApiPermissionRequests once to current PROD package, and deploy it in order to get the API request and to be able to approve that. Thank you for your help and patience @juliemturner.

jpalo avatar May 23 '22 11:05 jpalo

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.

github-actions[bot] avatar Aug 19 '22 02:08 github-actions[bot]