SharePoint JavaScripts Library

Update: April 23rd, 2012

Here you can find a collection of JavaScripts methods and objects that I’ve created over time.

  1. Rating: +0

    Positive Negative

    getField - get a SharePoint Form field

    function getField(fieldType,fieldTitle) {  
        var docTags = document.getElementsByTagName(fieldType);  
        for (var i=0; i < docTags.length; i++) {  
            if (docTags[i].title == fieldTitle) {  
                return docTags[i]  
            }
        }
            return null;
    }
  2. Rating: +0

    Positive Negative

    qs - Query URL parameter

    function qs(paramName) {
            var args = document.location.search.substring(1).split("&");
            for(i = 0; i < args.length; i++) {
                    nameValues = args[i].split("=");
                    if(nameValues[0] == paramName) return nameValues[1];
            }
            return null;
    }
  3. Rating: +0

    Positive Negative

    getElementByClassName - get a collection of elements with specified css class

    function getElementByClassName(elementName, cssClassName) {
            var allElements = document.getElementsByTagName(elementName);
            var resultElements = new Array();
            for(i = 0; i < allElements.length; i++) {
                    if(allElements[i].className != null) {
                            if(allElements[i].className.indexOf(cssClassName) > -1) {
                                    resultElements.push(allElements[i]);
                            }
                    }
            }
            return resultElements;
    }
  4. Rating: +0

    Positive Negative

    parseXML - parse text into XML object

    function parseXML(inputString) {
            if (window.DOMParser) {
                    parser=new DOMParser();
                    xmlDoc=parser.parseFromString(inputString,"text/xml");
            }
            else { // Internet Explorer
                    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async="false";
                    xmlDoc.loadXML(inputString); 
            }
            return xmlDoc;
    }

    This script is from www.w3schools.com

  5. Rating: +0

    Positive Negative

    attributeValue - read XML node attribute value

    function attributeValue(node, attributeName) {
            var attributesCollection = node.attributes;
            for(atv = 0; atv < attributesCollection.length; atv++) {
                    if(attributesCollection[atv].name == attributeName) return attributesCollection[atv].value
            }
            return "";
    }

    This script is from www.w3schools.com

  6. Rating: +0

    Positive Negative

    _spBodyOnLoadFunctionNames array

    _spBodyOnLoadFunctionNames.push("functionName")
  7. Rating: +0

    Positive Negative

    2010: ExecuteOrDelayUntilScriptLoaded

    ExecuteOrDelayUntilScriptLoaded(yourFunctionName,"SP.js");
  8. Rating: +0

    Positive Negative

    jQuery Ajax Post to SharePoint web services

       $.ajax({ 
                    type: "POST", 
                    url: L_Menu_BaseUrl + "/_vti_bin/lists.asmx", 
                    contentType: "application/soap+xml", 
                    data: soapQuery, 
                    dataType: "text", 
                    success: function(data) { 
                            yourFunctionName(data);
                    } 
             }); 
  9. Rating: +0

    Positive Negative

    "Edit document" link to document in document library

    editDocumentWithProgID2('[path_to_file]', '', 'SharePoint.OpenDocuments', '0', 'http://[site_url]', '0')
  10. Rating: +0

    Positive Negative

    iso2DisplayDate - convert yyyy-MM-dd date into dd.MM.yyyy

    function iso2DisplayDate(inputString) {
            result = parseInt(inputString.substring(8,10)) + "."
            result += parseInt(inputString.substring(5,7)) + "."
            result += parseInt(inputString.substring(0,4)) + " "
            result += inputString.substring(11,16)
            return result;
    }
  11. Rating: +0

    Positive Negative

    iso2Date - convert ISO date time to javascript date object

    function iso2Date(vhodString) {
        razbitiString = vhodString.split('-');
        var d = new Date();
        d.setDate(razbitiString[2]);
        d.setFullYear(razbitiString[0]);
        d.setMonth(razbitiString[1] - 1);
        return d;
    }
  12. Rating: +0

    Positive Negative

    romanize - convert number to roman digits

    function romanize (num) {
            if (!+num)
                    return false;
            var     digits = String(+num).split(""),
                    key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","I","II","III","IV","V","VI","VII","VIII","IX"],
                    roman = "",
                    i = 3;
            while (i--)
                    roman = (key[+digits.pop() + (i * 10)] || "") + roman;
            return Array(+digits.join("") + 1).join("M") + roman;
    }

    Script from this fantastic blog

  13. Rating: +0

    Positive Negative

    deromanize - convert roman digits to number

    function deromanize (str) {
            var     str = str.toUpperCase(),
                    validator = /^M*(?:D?C{0,3}|C[MD])(?:L?X{0,3}|X[CL])(?:V?I{0,3}|I[XV])$/,
                    token = /[MDLV]|C[MD]?|X[CL]?|I[XV]?/g,
                    key = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},
                    num = 0, m;
            if (!(str && validator.test(str)))
                    return false;
            while (m = token.exec(str))
                    num += key[m[0]];
            return num;
    }

    Script from this fantastic blog

Bookmarklets

SPB's Internal name revealer <-- Add this link to your bookmarks and call bookmarklet on List settings page reveal field internal names.

SPB's Link Right Click Return (beta) <-- If you're used to right-click quick launch or top navigation in SharePoint and opening page new tab, this bookmarklet re-enables "open tab>

                                
TOP