Skip to main content
Corrected code part
Source Link
Aakash Maurya
  • 8.5k
  • 5
  • 43
  • 76

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;
 
}

}

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;

}

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;
 
}
Source Link

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;

}