I'm new to sharepoint, I've created a web services where i post files to a document library but i also want to add metadata(columns data) 'Title' at the same time as i post the file using java. Is there any URL endpoint build in sharepoint i can use to accomplish this task?
3 Answers
You have not specified what web services you are using. You should keep in mind that SOAP web services are deprecated for SharePoint 2013. REST web services are most common for SharePoint 2013 and I hope you are using them.
Answering your question, there in no way to upload document and set metadata in one request. You have to do this in 3 steps:
- Upload document using FileCollectionAdd Endpoint. You can reference to specific location by:
List TitlePOST http://<sitecollection>/<site>/_api/web/lists(listid)/rootFolder
List IDPOST http://<sitecollection>/<site>/_api/web/lists/getByTitle(title)
Folder URL"http://<sitecollection>/<site>/_api/web /getfolderbyserverrelativeurl(<url>)/files /add(overwrite=true,url='filename.txt')
You will get uploaded SP.File in your response. - You have to get SP.ListItem associated with your new SP.File using
GET http://<sitecollection>/<site>/_api/web/folders/getbyurl(folderrelativeurl)/files/getbyurl(filerelativeurl)/listItemAllFields
endpoint.
You will get SP.ListItem in your response. - You can update SP.ListItem information for your SP.ListItem using
POST http://<sitecollection>/<site>/_api/web/lists(listid)/getItemById(id)
endpoint https://msdn.microsoft.com/EN-US/library/office/dn292552.aspx#ListItems
At first is looks quite complicated, but it works well. I can not find examples on Java, but where are some examples using javascript, so the steps are the same:
- How to: Upload a file by using the REST API and jQuery
- Properties when uploading Files using REST in SP 2013
Or you can view the documentation Working with folders and files with REST
This is how i make a POST to document library right now.. The next step is to make a GET to that file in document library but i can't figure it out. Sorry if this is bothersome but i'm new to this. How should the url look like for the GET?
documentLibrary = sharepoint server url to document library
http://industrial-ex/sites/tifdemo/enovia/
enoviaDocument = local path to a file
D:\tif-destination
if (enoviaDocument.isFile()) {
FileInputStream fileInputStream = new FileInputStream(enoviaDocument);
PutMethod method = new PutMethod(documentLibrary + enoviaDocument.getName());
RequestEntity requestEntity = new InputStreamRequestEntity(fileInputStream);
method.setRequestEntity(requestEntity);
Constant.getHttpclient().executeMethod(method);
System.out.println("STATUS CODE: " + method.getStatusCode() + " " + "STATUS TEXT: "
+ method.getStatusText());
}
-
I am not sure if that is correct way to upload a documents. But if it works - let it be. Now you can make GET request to industrial-ex/sites/tif-demo/_api/web/folders/… and you should get the ListItem as I have wrote in step 2. Maybe you should check stackoverflow.com/questions/23147092/…– ECM4DCommented Feb 9, 2015 at 20:50
-
Is Folders in the url a requirement? because i don't have any folders in my Document library If my document library is named 'enovia' then the url should look like: industrial-ex/sites/tifdemo/_api/web/enovia/ right? Commented Feb 10, 2015 at 13:35
-
Every library in the web has root folder, that ussualy is the same as library title. If my sample does not work, you could try other ways, like: by url
http://industrial-ex/sites/tif-demo/_api/web/GetFolderByServerRelativeUrl('/sites/tif-demo/enovia')/files/getbyurl(filerelativeurl)/listItemAllFields
or by library display namehttp://industrial-ex/sites/tif-demo/_api/web/Lists/GetByTitle('enovia')/RootFolder/files/getbyurl(filerelativeurl)/listItemAllFields
– ECM4DCommented Feb 10, 2015 at 20:48 -
I have also found another solution how you can skip that second request to get item ID. Look at the requests that are made using jQuery: stackoverflow.com/questions/18662332/… You should add
?$expand=ListItemAllFields
to end of first request and SharePoint should return item metadata in one request.– ECM4DCommented Feb 10, 2015 at 20:51 -
I tried out both of the urls in Postman with a GET and i got this: i.imgur.com/OgSzCWw.png Commented Feb 11, 2015 at 8:35
@ECM4D
This url works: http://industrial-ex/sites/tifdemo/_api/lists/getbytitle('enovia')/items
I can see my document file but how do i then select the metadata?
This is the Java Code:
String url = "http://industrial-ex/sites/tifdemo/_api/lists/getbytitle('enovia')/items";
GetMethod get = new GetMethod(url);
authentication.getHttpClient().executeMethod(get);
System.out.println("RESPONSE BODY: " + get.getResponseBody());
-
Here is the image in Postman so you can see: i.imgur.com/6HrX0EI.png Commented Feb 11, 2015 at 9:35