1

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 3

0

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:

  1. Upload document using FileCollectionAdd Endpoint. You can reference to specific location by:
    List Title POST http://<sitecollection>/<site>/_api/web/lists(listid)/rootFolder
    List ID POST 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.
  2. 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.
  3. 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:

Or you can view the documentation Working with folders and files with REST

1
  • Could you look at the new post i did please :) Commented Feb 9, 2015 at 12:16
0

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());



            }

5
  • 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/…
    – ECM4D
    Commented 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 name http://industrial-ex/sites/tif-demo/_api/web/Lists/GetByTitle('enovia')/RootFolder/files/getbyurl(filerelativeurl)/listItemAllFields
    – ECM4D
    Commented 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.
    – ECM4D
    Commented 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
0

@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());

1

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.