Is it possible to create a document-library via SharePoint Rest API?
2 Answers
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);
-
How can i do it in Java?user3476614– user34766142015-04-08 07:50:35 +00:00Commented 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– pholpar2015-04-08 07:53:06 +00:00Commented Apr 8, 2015 at 7:53
-
I mean a real Java exampleuser3476614– user34766142015-04-08 08:08:20 +00:00Commented Apr 8, 2015 at 8:08
-
@pholpar, your example is incomplete, endpoint url is missingVadim Gremyachev– Vadim Gremyachev2015-04-08 08:12:09 +00:00Commented 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– pholpar2015-04-08 08:23:40 +00:00Commented Apr 8, 2015 at 8:23
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