pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
750 stars 306 forks source link

Is there a way to assign a Site Column to the Site Content Type using a REST call? #2838

Closed mgordic closed 9 months ago

mgordic commented 10 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

3.0

Target environment

SharePoint Framework

Additional environment details

SharePoint Online, standard spfx webpart

Question/Request

Is there a way to assign a Site Column to the Site Content type using REST call?

Long ago I had the same question here, but I was unable to adjust proposed solution to make it works. I am always getting Unknown error as a result of the call.

Can someone help me to better understand above mentioned ProcessQuery request? From above example I need just a part where to add a Site Column to fields collection.

bcameron1231 commented 10 months ago

Personally, I would use Graph. It's a much simpler API call.

But if you want to share some code / the full error from your Network Tab, we'd be happy to review.

mgordic commented 10 months ago

Thank you for reply. Graph api is not an option, because of the limitations from customer side.

I network tab there is just a response with Unknown error as a message and a correlation id

bcameron1231 commented 10 months ago

Any way you can capture a screen shot or something, and also provide us with the Request body that you're sending over? Feel free to hide any tenant specific information.

mgordic commented 10 months ago

Hi @bcameron1231 ,

this is the payload of the request that I am sending:

image

and this is the response I am getting:

image

bcameron1231 commented 10 months ago

Unfortunately, I am not able to get too much information from those images as to why it's not working.

Out of curiosity, what is your client's limitation on using Graph? Is it that you won't get app registration consent to make that call?

If yes, I would then just recommend using the SharePoint v2 endpoints. Some Graph endpoints are exposed on the V2.1 endpoint that you can call directly from your SPFx Web Part. https://learn.microsoft.com/en-us/sharepoint/dev/apis/sharepoint-rest-graph

Get the SiteID, ContentTypeId from V2 endpoints, call the v2.1 contenttypes/add endpoint to add the column.

Method - POST Url - https://tenant.sharepoint.com/_api/v2.1/sites/{Graph Site Id}/contentTypes/{contentTypeId}/columns/add Body - { "value ":[{ "name ": "MyNewColumn ", "displayName ": "MyNewColumn ", "description ": " ", "required ":false, "customFormatter ": " ", "columnGroup ": "Custom Columns ", "sourceContentType ":{ "id ": "0x0101001C44477F648C1E4AB32112B806FB9F56 "}, "enforceUniqueValues ":null, "pushDownChanges ":true, "text ":{ "maxLength ":255, "allowMultipleLines ":false, "appendChangesToExistingText ":false, "textType ": " ", "unlimitedLengthInDocumentLibrary ":false}, "defaultValue ":{ "value ": " ", "formula ":null}, "columnValidation ":{ "formula ": " ", "message ": " "}}]}

You can also pass in existing columns using the Id Method - POST Url - https://tenant.sharepoint.com/_api/v2.1/sites/{Graph Site Id}/contentTypes/{contentTypeId}/columns/add Body - { "value ":[{ "id ": "060e50ac-e9c1-4d3c-b1f9-de0bcac300f6 ", "pushDownChanges ":true}]}

Unfortunately, outside of that, I'm not sure how we'll be able to help as we don't have any support for that within the library.

mgordic commented 10 months ago

Thank you @bcameron1231, somehow I completely missed the announcement about v2 endpoint.

I will do some investigation offcourse. Does it means that all operations defined for for example /sites endpoint is applicable on v2 sharepoint endpoint?

bcameron1231 commented 10 months ago

Thank you @bcameron1231, somehow I completely missed the announcement about v2 endpoint.

I will do some investigation offcourse. Does it means that all operations defined for for example /sites endpoint is applicable on v2 sharepoint endpoint?

Correct. In fact, the v2.1 endpoint is what SharePoint uses under the covers when you add a field to a content type through the UI.

mgordic commented 10 months ago

Great. And what about consuming this endpoint from spfx webpart? Just use spHttpClient or there is some better way? Looks like endpoint is not documented at all, except the graph api counterparts

bcameron1231 commented 10 months ago

Correct, the sphttpclient should work.

mgordic commented 9 months ago

@bcameron1231 I need some more help :) when I am executing any GET request against endpoints using spHttpClient, everything is working just fine. But when I try any POST request, I am getting 403 Forbidden error. I am guessing that somehow I need to include authorisation token (in graph api we are sending client id as a part of authentication process, but here?).

Request:

const url = `https://${window.location.host}/_api/v2.1/sites/${siteId}/columns`;

const requestHeaders: Headers = new Headers();
requestHeaders.append('Content-type', 'application/json;odata=verbose');
requestHeaders.append('Accept', 'application/json;');

const options: ISPHttpClientOptions = {
    headers: requestHeaders,
    body: JSON.stringify({
        "description": "Lorem Ipsum",
        "enforceUniqueValues": false,
        "hidden": false,
        "indexed": false,
        "required": true,
        "name": "provSingleTextColumn",
        "displayName": "Provisioning - Single Text Column",
        "text": {
            "allowMultipleLines": false,
            "appendChangesToExistingText": false,
            "linesForEditing": 0,
            "maxLength": 128
        }
    })
};

const response: SPHttpClientResponse = await this.spHttpClient.post(url, SPHttpClient.configurations.v1, options);

Error message:

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

Any ideas?

Thank you :)

juliemturner commented 9 months ago

Yes, although you can perform get calls, you have to provide a token in the case of posts, the v2.1 endpoints are not secured with cookie auth. I've gotten it to work for other things, but here's a thread with the details of how to provide that auth token. https://github.com/pnp/pnpjs/issues/2628 (this particular person apparently wasn't able to get it working but never responded to @patrick-rodgers offer for assistance)

So, essentially if you need to do post calls, you're going to have to deal with the auth/token issue... and at this point I would switch to the graph api.

mgordic commented 9 months ago

@juliemturner graph api is not an option, because it's not easy to get app registration in customer's enterprise environment. That's why I initially headed to your suggestion in abovementioned issue with xml and ProcessQuery endpoint. Can you maybe check it?

juliemturner commented 9 months ago

As @bcameron1231 mentioned above we would need more information. In this case you would need to submit a complete bit of code that clearly outlines how to reproduce the issue. This is most easily done with a GitHub code repository. At the end of the day, we're all volunteers so when one of us has extra time in our schedule doing so would ensure they have a starting point to try and assist.

I understand that your client doesn't want to do auth the right way and wants it to just magically work but unfortunately that's not reality. There are a lot of limitations, and there's a lot of legacy stuff (like cookie auth) that probably shouldn't work that way anymore, but for legacy reasons still does, and you're going to see movement away from it in the future... this is one of those places. I know as a consultant I have to have these tough conversations with my clients regularly, and it's never fun.

mgordic commented 9 months ago

Thank you @juliemturner for your opinion. Yes, you are right, and I am also more than aware what is preferable way of doing. But as I say, it a policy on a company level that limits and makes extremely difficult to get approved app registration that uses graph api.

I already posted a code example and referenced your previous comment in other issue.

I will close then this issue and try then to get suggestions / ideas / solutions on some other place.

github-actions[bot] commented 9 months ago

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.