I am using this code but need to add an extra column. I tried using fileID and my column name of AssociateID but can't gwet it to work. I know it's a simeple thing that I am doing wrong. Here's my code:
function uploadDocument(loc) {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('MTP');
var view = list.get_views().getByTitle('testAdminView');
var viewFields = view.get_viewFields();
context.load(viewFields);
context.executeQueryAsync(printFieldNames, onError);
function printFieldNames() {
var e = viewFields.getEnumerator();
while (e.moveNext()) {
var fieldName = e.get_current();
console.log(fieldName);
}
}
function onError(sender, args) {
console.log(args.get_message());
}
var files = $("#getFile")[0].files;
if (files.length > 0) {
fileName = files[0].name;
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var documentLibrary = "MTP";
var fileID = loc;
var targetUrl = _spPageContextInfo.webServerRelativeUrl + "/" + documentLibrary;
// Construct the Endpoint
var url = webUrl + "/_api/Web/GetFolderByServerRelativeUrl(@target)/Files/add(overwrite=true, url='" + fileName + "')?@target='" + targetUrl + "'&$expand=ListItemAllFields";
uploadFileToFolder(files[0], url, function(data) {
var file = data.d;
fileID = file.AssociateID;
DocFileName = file.Name;
var updateObject = {
__metadata: {
type: file.ListItemAllFields.__metadata.type
},
FileLeafRef: DocFileName, //FileLeafRef --> Internal Name for Name Column
AssociateID: fileID
};
alert("File uploaded successfully!");
}, function(data) {
alert("File uploading failed");
});
} else {
alert("Kindly select a file to upload.!");
}
}
// Get the local file as an array buffer.
function getFileBuffer(uploadFile) {
var deferred = jQuery.Deferred();
var reader = new FileReader();
reader.onloadend = function(e) {
deferred.resolve(e.target.result);
}
reader.onerror = function(e) {
deferred.reject(e.target.error);
}
reader.readAsArrayBuffer(uploadFile);
return deferred.promise();
}
function uploadFileToFolder(fileObj, url, success, failure) {
var apiUrl = url;
console.log('url = ' +apiUrl)
// Initiate method calls using jQuery promises.
// Get the local file as an array buffer.
var getFile = getFileBuffer(fileObj);
// Add the file to the SharePoint folder.
getFile.done(function(arrayBuffer) {
$.ajax({
url: apiUrl,//File Collection Endpoint
type: "POST",
data: arrayBuffer,
processData: false,
async: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
},
success: function(data) {
success(data);
},
error: function(data) {
success(data);
}
});
});
}
Daniel WrightPosted Feb 25, 2025, 12:50 AM
It seems like you are attempting to upload a document to a SharePoint library using JavaScript and add an extra column (AssociateID) to the uploaded document. Based on the provided code snippet, there are a few areas that need adjustment to make this work correctly.
1. Setting AssociateID Value: The way you are trying to set the AssociateID value from the uploaded file seems correct: `fileID = file.AssociateID;`. Ensure that the `AssociateID` property is indeed available in the `file` object as it comes from the response.
2. Updating the Metadata:
- The `updateObject` variable should also include the `AssociatID` field along with `FileLeafRef`. It seems you are doing this correctly in your code.
- Make sure that `file.ListItemAllFields.__metadata.type` contains the correct type for the list item fields.
3. Include AssociateID in the Update:
After setting the `AssociateID` in the `updateObject`, it should be included in the request to update the list item fields.
I see you are handling the success and failure scenarios appropriately in your code, which is great for error handling and feedback to the user.
Remember to check the following:
- Ensure that the `AssociateID` field exists in the SharePoint list and that the internal name matches what you are using.
- Double-check the response structure when you get the file back after uploading to ensure the existence and correct naming of the `AssociateID` property.
If you encounter difficulties, you can utilize browser debugging tools like the Console to inspect any errors or unexpected behavior that might help identify issues in the process. It's also useful to monitor the network requests to see the data being sent back and forth between your JavaScript code and the SharePoint API.
Feel free to provide more details or ask specific questions if you need further assistance with this integration!