2

Is it possible to create a document-library via SharePoint Rest API?

2 Answers 2

4

I assume you mean SharePoint 2013. In this case, of course, you can create lists, including document libraries via REST, as described here. For the document library you should use 101 as the BaseTemplate.

url: http://site url/_api/web/lists
method: POST
body: { '__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true, 'BaseTemplate': 101,
 'ContentTypesEnabled': true, 'Description': 'My doc. lib. description', 'Title': 'Test' }
Headers: 
    Authorization: "Bearer " + accessToken
    X-RequestDigest: form digest value
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body

If you need to access this endpoint from a Java-based application, you can use a library, like Apache HttpComponents that supports this kind of HTTP-based communication. You find several samples here.

As far as I the official documentation of this API, and other samples on the web (like this and this one) understand, the body of the request can be set in this case as a StringEntity instance via the setEntity method of the HttpPost object:

String jsonString = "{ '__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true, 'BaseTemplate': 101,
 'ContentTypesEnabled': true, 'Description': 'My Documents Library', 'Title': 'Documents' }";
HttpEntity e = new StringEntity(jsonString);
httpPost.setEntity(e);
9
  • How can i do it in Java? Commented Apr 8, 2015 at 7:50
  • Do you mean JavaScript (like from the context of a SharePoint app) or really Java (for example from a context of a console application on Unix or some kind server side code)?
    – pholpar
    Commented Apr 8, 2015 at 7:53
  • I mean a real Java example Commented Apr 8, 2015 at 8:08
  • @pholpar, your example is incomplete, endpoint url is missing Commented Apr 8, 2015 at 8:12
  • 1
    @VadimGremyachev, thanks, it was a copy-paste error. Next time feel free to complete my code if you find such mistakes.
    – pholpar
    Commented Apr 8, 2015 at 8:23
2

How to create a Document library using SharePoint REST:

url: http://site url/_api/web/lists
method: POST
body: { '__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true, 'BaseTemplate': 101,
 'ContentTypesEnabled': true, 'Description': 'My Documents Library', 'Title': 'Documents' }
Headers: 
    Authorization: "Bearer " + accessToken
    X-RequestDigest: form digest value
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body

References

Working with lists and list items with REST

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.