This will upload a file to a SharePoint library with metadata added from a webpage using javascript and jquery.
<script type="text/javascript" src="https://xxxxx/Utilities/jquery-1.11.2.min.js"></script>
</ContentTemplate>
</SharePoint:UIVersionedContent>
<script type="text/javascript">
$(document).ready(function() {
var ups = getUrlVars()
setValues(ups);
});
String.prototype.replaceAll = function (find, replace) {
var str = this;
return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value) {
vars[key] = value;
});
return vars;
}
function setValues(ups){
for(var index in ups)
{
var key = index;
var value = decodeURI(ups[index].replaceAll('%C3%82%C2%A0',' ').replaceAll('%3A',':'));//%C3%82%C2%A0
//console.log(key + ": " + value);
if(key.indexOf("fdx") > -1)
{
var titleValue = key.replace('fdx','').replaceAll('%C3%82%C2%A0','');
var elements = document.querySelectorAll('[id="' + titleValue + '"]');// + titleValue + "']");
var att = document.createAttribute("value");
att.value = value;
if(elements[0] != null)
elements[0].setAttributeNode(att);
}
//alert(key);
}//end for
}
function loadFile() {
var filePath = $('#attafh').val(); // "c:\\test.pdf";
var file = filePath.match(/\\([^\\]+)$/)[1];
var siteValue = $('#Site').val();
var UICValue = $('#UIC').val();
var commandValue = $('#Command').val();
var batchValue = $('#Batch').val();
//var errorObj = $('#Error');
// var selectedIndex = errorObj.selectedIndex;
var errorValue = $('#Error').val();// errorObj.options[selectedIndex].value;
var approvedValue = $('#Approved').val();
var approvedDateValue = $('#ApprovedDate').val();
var soapEnv =
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \
<soap:Body>\
<CopyIntoItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>\
<SourceUrl>" + filePath + "</SourceUrl>\
<DestinationUrls>\
<string>https://xxxxxx/" + file + "</string>\
</DestinationUrls>\
<Fields>\
<FieldInformation Type='Text' DisplayName='UIC_Num' InternalName='UIC' Value='"+UICValue +"' />\
<FieldInformation Type='Text' DisplayName='Site' InternalName='Site' Value='"+siteValue +"' />\
<FieldInformation Type='Text' DisplayName='Command' InternalName='Command' Value='"+commandValue+"' />\
<FieldInformation Type='Text' DisplayName='Batch' InternalName='Batch' Value='"+batchValue+"' />\
<FieldInformation Type='Text' DisplayName='Error' InternalName='Error' Value='"+errorValue+"' />\
<FieldInformation Type='Text' DisplayName='Approved By' InternalName='Approved_x0020_By' Value='"+approvedValue+"' />\
<FieldInformation Type='DateTime' DisplayName='Date Approved' InternalName='Date_x0020_Approved' Value='"+approvedDateValue+"' />\
</Fields>\
<Stream>base64Binary</Stream>\
</CopyIntoItems>\
</soap:Body>\
</soap:Envelope>";
$.ajax({
url: "https://xxxxxx/_vti_bin/copy.asmx",
beforeSend: function (xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/CopyIntoItems"); },
type: "POST",
dataType: "xml",
data: soapEnv,
error: errorHandler,
success: processResult,
contentType: "text/xml; charset=\"utf-8\""
});
}
function errorHandler(evt) {
alert(evt);
}
function processResult(evt) {
alert('Success!!');
window.location.href = 'https://xxxxxx/Forms/AllItems.aspx';
}
</script>
<table>
<tr>
<td>Site:</td><td><input id="Site"></input></td>
</tr>
<tr>
<td>UIC:</td><td><input id="UIC"></input></td>
</tr>
<tr>
<td>Command Name:</td><td><input id="Command"></input></td>
</tr>
<tr>
<td>Batch:</td><td><input id="Batch"></input></td>
</tr>
<tr>
<td>Error:</td>
<td>
<select id="Error">
<option value="Laundered TLD">Laundered TLD</option>
<option value="Damaged TLD">Damaged TLD</option>
<option value="Lost TLD">Lost TLD</option>
<option value="Flown TLD">Flown TLD</option>
<option value="L2/L1">L2/L1</option>
<option value="Greater than 100mrem">Greater than 100mrem</option>
<option value="Unexpected Exposure">Unexpected Exposure</option>
<option value="Shallow/Deep Discrepancy">Shallow/Deep Discrepancy</option>
<option value="Improperly Stored Controls">Improperly Stored Controls</option>
<option value="Control Value Discrepancy">Control Value Discrepancy</option>
<option value="TLD Incorrectly Assigned">TLD Incorrectly Assigned</option>
<option value="Other" selected="selected">Other</option></select>
</td>
</tr>
<tr>
<td>Approved By:</td><td><input id="Approved"></input></td>
</tr>
<tr>
<td>Date Approved:</td><td><input id="ApprovedDate"></input></td>
</tr>
</table>
<input id="attafh" type="file" class="AlltextAccount" />
<input type="button" value="upload" onclick="loadFile()" />
Upload to Sharepoint list.txtOpen
1 of 3 items
Upload to Sharepoint list.txtCSV Upload.txtsmime.p7sDisplaying Upload to Sharepoint list.txt.