0

As I understand, you have to make a 3 steps to update the metadata on the file. I have uploaded a file to my document library using POST and after that made a GET and took out the metadata i need to change, in this case "Title". The next step is to make a PUT and send the metadata with it but how? I don't want to implement Microsofts classes.

Here is the code to upload a file:

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);
                    PostMethod method = new PostMethod(documentLibrary + enoviaDocument.getName());
                    RequestEntity requestEntity = new InputStreamRequestEntity(fileInputStream);
                    method.setRequestEntity(requestEntity);
                    authentication.getHttpClient().executeMethod(method);
                    System.out.println("STATUS CODE: " + method.getStatusCode() + " "
                            + "STATUS TEXT: " + method.getStatusText());
                    System.out.println("STATUS LINE: " + method.getStatusLine());

Here is the code to get file:

String url = "http://industrial-ex/sites/tifdemo/_api/web/lists/getbytitle('enovia')/items/";
                    GetMethod get = new GetMethod(url);
                    get.setRequestHeader("Accept", "application/json; odata=verbose;");
                    authentication.getHttpClient().executeMethod(get);
                    JsonParser jsonParser = new JsonParser();
                    JsonElement jsonElement = jsonParser.parse(get.getResponseBodyAsString());
                    JsonObject asJsonObject = jsonElement.getAsJsonObject();
                    String title = asJsonObject.get("d")
                            .getAsJsonObject()
                            .get("results")
                            .getAsJsonArray()
                            .get(0)
                            .getAsJsonObject()
                            .get("Title")
                            .getAsString();
                    System.out.println("Metadata Title: " + title);

The next step is to make a PUT with metadata but how?

3
  • Check out JShare, Java API for SharePoint. It has many examples how to update metadata.
    – user39650
    Commented Feb 14, 2015 at 5:39
  • 1
    JShare Is not open source Commented Feb 14, 2015 at 8:37
  • @Steve Do you know any good java api for sharepoint that is free and UpToDate ? Commented Feb 14, 2015 at 14:15

1 Answer 1

2

You can actually do this in two requests. You can upload the file to SharePoint and get the returned URL. Then use the returned URL to update the Title. The example below uses JavaScript, but you can see the required headers you need to set in order to update the title. You can also see the other steps you need to take if you do not want more than one version when uploading and setting metadata at this link: http://sharepointfieldnotes.blogspot.com/2014/04/uploading-documents-and-setting.html

function updateMetadata(fileUrl) {

appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
var restSource = appweburl +
    "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Documents')/rootfolder/files/getbyurl(url='" + fileUrl + "')/listitemallfields?@target='" + hostweburl + "'";
var dfd = $.Deferred();

$.ajax(
{
    'url': restSource,
    'method': 'POST',
    'data': JSON.stringify({
        '__metadata': {'type':'SP.ListItem'},
        'Title': 'My Title 3'
    }),
    'headers': {
        'accept': 'application/json;odata=verbose',
        'content-type': 'application/json;odata=verbose',
        'X-RequestDigest': $('#__REQUESTDIGEST').val(),
        'X-Http-Method': 'PATCH',
        "If-Match": "*"
    },
    'success': function (data) {
        var d = data;
        dfd.resolve();
    },
    'error': function (err) {
        dfd.reject();
    }
}
   );

    return dfd;

}
1
  • Thanks for the response @Steve Curran, is there any example on this using java? Commented Feb 13, 2015 at 7:30

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.