0

I created a library that contains documents in Sharepoint Online. Let's call it myLib. I also created a content type, myCT. My objective is to retrieve informations concerning fields I created in a content type in sharepoint online. The library is using the content type i created.

For this i also created a webpart component that aims to generate a form with the content type fields / columns. I need to retrieve :

  • the field name
  • the field type
  • the values (for select, radio buttons, checkboxes)

For this I found that i can access XML datas from sharepoint at this URI : https://myAppUrl.sharepoint.com/_api/web/

enter image description here

My issue is that I spent a rather long time trying to find where the data from the content type is stored. I tried those URLs :

Without finding what i'm looking for. I also tried to create a script in the webpart: :

function getallContentTypes() {
    var clientContext = SP.ClientContext.get_current();
    oContentTypes = clientContext.get_web().get_contentTypes();
    clientContext.load(oContentTypes);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, function() {
            var ctypesInfo = '';
            var ctypesEnumerator = oContentTypes.getEnumerator();

            while (ctypesEnumerator.moveNext()) {
                var ocontentType = ctypesEnumerator.get_current();
                ctypesInfo += '\n' + 'ContentType Title: ' + ocontentType.get_name()+'\t'+ ocontentType.get_id();
            }
            console.log(ctypesInfo.toString());
        }),
        Function.createDelegate(this, function() {
            console.log('failed');
        }));
}

function injectMethod(){
    getallContentTypes();
}
ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");

it returned me information concerning the content type : ContentType Title: myCT 0x0101007D18D0E5D0A176498869E2673AD0CA14

Now i wish to know how to handle this.

UPDATE after FredericDietrich help : I tried to use URL like : _api/Web/ContentTypes?$select=Name,Id,StringId&$filter=Name eq 'myCT'

And i obtain an XML:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xml:base="https://dsidiff365.sharepoint.com/_api/">
    <id>c2f25390-1496-45f6-9c87-0f6c0a9ea026</id>
    <title />
    <updated>2019-11-27T15:54:07Z</updated>
    <entry>                <id>https://myurl.sharepoint.com/_api/Web/ContentTypes('0x010100ED0CEB986D733A47AD45D9A995911BAA')</id>
        <category term="SP.ContentType" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <link rel="edit" href="Web/ContentTypes('0x010100ED0CEB986D733A47AD45D9A995911BAA')" />
        <title />
        <updated>2019-11-27T15:54:07Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:Id m:type="SP.ContentTypeId">
                <d:Name>Facture</d:Name>
                <d:StringId>0x010100ED0CEB986D733A47AD45D9A995911BAA</d:StringId>
            </m:properties>
        </content>
    </entry>
</feed>

but no information concerning fields

2 Answers 2

0

you can use a REST request as below

/_api/Web/getByTitle('Pages')/ContentTypes/getById('0x0101007D18D0E5D0A176498869E2673AD0CA14')/Fields

enter image description here

4
  • I obtain : <m:code>-1, Microsoft.SharePoint.Client.ResourceNotFoundException</m:code>
    – davidvera
    Nov 27, 2019 at 13:57
  • in my sample i use list : Pages, on web contentTypes prefer this : /_api/web/AvailableContentTypes?$expand=Fields&$select=Name,StringId,fields/TypeAsString Nov 27, 2019 at 14:16
  • I tried it and i obtain results ... but not the one I expect
    – davidvera
    Nov 27, 2019 at 15:54
  • try this /_api/Web/ContentTypes('0x010100ED0CEB986D733A47AD45D9A995911BAA')/Fields Nov 28, 2019 at 7:31
0

in my case it's ok with : /_api/Web/ContentTypes('0x010100ED0CEB986D733A47AD45D9A995911BAA')/Fields

in ie console, fields details

full code to display contentType fields available here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.