V0.6.0 Alpha Test [Copy Operations]

Feb 1, 2011 at 6:04 PM

Again, I am not an official tester, but thought I would add more info on my testing of the Alpha so far. I know that the copy operations were released in an earlier version, but I never tested them with that version.

My goal was to see if I could use the copy operations from client-side code to add a document to a document library. The document is an xml spreadsheet file that is created on the fly with a xsl stylesheet.

I was able to do this in IE and am not sure if it can be done in other browsers.

The code below is the first part of the operation:

    var xmldata1 = new ActiveXObject("MSXML.DomDocument");
    xmldata1.async = false;
    var xmldata2 = new ActiveXObject("MSXML.DomDocument");
    xmldata2.async = false;
    var xmldata3 = new ActiveXObject("MSXML.DomDocument");
    xmldata3.async = false;
    var xsldata1 = new ActiveXObject("MSXML.DomDocument");
    xsldata1.async = false;
    xsldata1.load(L_Menu_BaseUrl + "/PrintTemplates/CTSPrintList.xsl")

$().SPServices({
        operation: "GetListItems",
        async: false,
        webURL: L_Menu_BaseUrl,
        listName: "Commitments",
        CAMLQuery: camlQuery,
        CAMLViewFields: camlViewFields,
        completefunc: function(xData, Status) {
            //alert(xData.responseXML.xml);
            var rtz = _nnsy_elementContents(xData.responseXML.xml, "GetListItemsResult");
            try {
                xmldata1.loadXML(rtz);
            }
            catch (e) { }
            xmldata1.transformNodeToObject(xsldata1, xmldata2);
            var output = "<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?>" + xmldata2.xml;
            xmldata3.loadXML(output);

            var flds = "";
	    var results = "";
            var desturls = ["http://wss_sa/tools/cts/CTSReports/Test.xml"]; // This could be a dynamic filename!!
            var encoded = Base64.encode(xmldata3.xml);
            $().SPServices({
                operation: "CopyIntoItems",
                SourceUrl: "http://wss_sa/tools/cts/PrintTemplates/CTSPrintList.xsl",
                DestinationUrls: desturls,
                Fields: flds,
                Stream: encoded,
                completefunc: function(xData, Status) {
                    alert(xData.responseText);
                }
            });
        }
     } // end completefunc
});  // end spservices

var oxl = new ActiveXObject("Excel.Application");
oxl.Visible = true; // set to false
var owb = oxl.Workbooks.Open("http://wss_sa/tools/cts/CTSReports/Test.xml");
oxl.UserControl = true;

var Base64 = {

    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode: function(input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode: function(input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
The main function that really helps make this work is the Base64 encoding. This really helped! But this code allows the creating of files in a doc library from the client side!
Coordinator
Feb 1, 2011 at 8:09 PM
Daniel: If you're trying stuff, then you're an alpha tester. Thanks for posting this code. I'm sure that it'll be useful to people. I'll be looking through it myself, too, of course! M.
Sep 13, 2013 at 8:29 PM
The above parsing will not work for XML strings. Can anyone please provide a way to convert large XML strings to stream?