1

I am creating a Document Library using REST with the body content below. At the same time as i create this Document Library i also want to create custom columns. What should i type in the body content?

{'__metadata':{'type':'SP.List'},'AllowContentTypes':true,'BaseTemplate':101,'ContentTypesEnabled':true,'Description':'','Title':'" + parentName + "'}

3 Answers 3

2

I don't think you can create fields (columns) in a list/library at the same time when creating it as the REST endpoints for list/library and fields are different. So you will have to first create your document library and then add fields to it.

The Fields REST API reference on MSDN is a pretty comprehensive guide on how to create different type of fields. For e.g. to create a text field you can use the below sample code:

$.ajax({
  url: "http://<site url>/_api/web/lists(guid'da58632f-faf0-4a78-8219-99c307747741')
    /fields",
  type: "POST",
  data: "{ '__metadata': { 'type': 'SP.FieldText' }, 'FieldTypeKind': 2,
    'Title': 'Project Name', 'MaxLength': '22' }",
  headers: { 
    "X-RequestDigest": <form digest value>,
    "accept": "application/json;odata=verbose",
    "content-type": "application/json;odata=verbose",
    "content-length": <length of body data>
  },
  success: successHandler,
  error: errorHandler
});
1
  • Ok, I can add the column to the document library, but how can i set it to be show in the list by rest api?
    – germangti
    Jul 7, 2017 at 12:43
1

I'm not sure if a column can be created at the same time, but perhaps this example will guide you on how to create a column:

// Create a new list field
function createField() {
var fieldName = document.getElementById("createlistfieldname").value;
var executor;

// Initialize the RequestExecutor with the app web URL.
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
    url: appweburl + "/_api/SP.AppContextSite(@target)/web/Lists/getbytitle('Custom List')/fields?@target='" + hostweburl + "'",
    method: "POST",
    body: "{ '__metadata': { 'type': 'SP.Field' }, 'FieldTypeKind': 2, 'Title':'" + fieldName + "'}",
    headers: {
    "content-type": "application/json; odata=verbose",
    },
    success: createListFieldSuccessHandler,
    error: createListFieldErrorHandler
    });
}

Source

5
  • Tried it, doesnt work for me. Getting 'Bad Request' but thanks tho May 13, 2015 at 10:42
  • Is that when you tried creating the column separately after creating the document library? And are you sure your end point is correct?
    – Submits
    May 13, 2015 at 10:47
  • No i tried to create the document library at the same as the coulmns. May 13, 2015 at 10:56
  • Use Fiddler (telerik.com/fiddler) to watch the traffic and in the response you will see a proper error message, not just "Bad Request". This will give you more clues.
    – Submits
    May 13, 2015 at 10:58
  • Probably should note that this is for an app. If you're throwing JavaScript on a regular SharePoint page, then RequestExecutor is not necessary.
    – wjervis
    May 13, 2015 at 14:05
1

If you are using SharePoint on-premises you can't create the library and the custom columns in a single call using the REST API. You need to make a call to create the library and then make subsequent calls to create each of the custom columns.

The REST API in SharePoint Online now supports batching so you can create the library and the custom columns in a single call. There are several articles out there on REST API request batching, I suggest you start with this one, "SharePoint REST API Batching - Understanding Batching Requests"

Your Answer

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

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