magritton
5/13/2016 - 10:14 PM

This adds a document to a SharePoint library using SOAP with the related metadata passed through the URL as parameters

This adds a document to a SharePoint library using SOAP with the related metadata passed through the URL as parameters

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 UploadMe(readFile) {
     var reader = new FileReader();
     reader.readAsDataURL(readFile);
     reader.onload = fileLoaded;
     reader.onerror = errorHandler;
}

function fileLoaded(evt)
{
	
	var fileString = evt.target.result;;
    var y = evt.target.result.match(/,(.*)$/)[1];
    var filePath = $('#attafh').val(); // "c:\\test.pdf";
    var file = filePath.match(/\\([^\\]+)$/)[1];
    filePath = '';
	var siteValue = $('#Site').val();
	var UICValue = $('#UIC').val();
	var commandValue = $('#Command').val();
	var batchValue = $('#Batch').val();
	var errorValue = $('#Error').val();	 
	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>gdfg" + filePath + "</SourceUrl>\
	                             <DestinationUrls>\
	                                 <string>https://xxxxx" + 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>" + y + "</Stream>\
	                     </CopyIntoItems>\
	                 </soap:Body>\
	             </soap:Envelope>";

         $.ajax({
             url: "https://xxxxx/_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 loadFile() {
     var filez = document.getElementById('attafh').files[0];
     UploadMe(filez);
}

function errorHandler(evt) {
    alert(evt);
}
 
function processResult(evt) {
	//alert('Success!!');
	window.location.href = 'https://xxxxx/Forms/AllItems.aspx';
}