Monday, February 7, 2011

Filling fields using javascript and web services

Some days ago there was a request for filling a look up field in New Item page automatically according to the current user information. One of the ways of doing this, is coding for custom field but the other quick and easy way is using JavaScript and web services

1- Adding a field with default value of [Me] to hold the user name of current user
2- Adding a content Editor webpart in NewForm.aspx page of List and put the code below inside its source editor:

<script type="text/javascript" language="javascript">

    var siteCompleteURL = document.URL;
    componentList = siteCompleteURL.split('//');
    siteURL = componentList[1].split('/');

    var curUserLastName = new Array();
    var curUserID = new Array();
    var baseURL = componentList[0] + "//" + siteURL[0] + "/";
    var siteURL = componentList[0] + "//" + siteURL[0] + "/";

    //Get The Value Of Hidden UserName Field
    var curUser = document.getElementById('USERNAMEID').value;


    //Hide The UserName Field and its Label
    document.getElementById('
USERNAMEID').style.visibility='hidden';
    var nobrTags = new Array();
    nobrTags=document.getElementsByTagName('nobr');
    for (i = 0; i < nobrTags.length; i++) {
    if (nobrTags(i).innerHTML=='UserName')
      nobrTags(i).innerHTML='';
     }

    //Get The LookUp Filed Object
    var expOwnerCID='LOOKUPFIELDID';

    //GUID Of The List We Want To Query
    var listGUID = 'd6e7ebe5-79b1-4e92-b50a-adf6d4c79c6e';

    //Where Statement of Web service Request
    var whereStatement = "<Eq><FieldRef Name=\"_x0646__x0627__x0645__x0020__x06\" /><Value Type=\"User\">"+curUser+"</Value></Eq>";
   
    function GetRootUrl() {
        return siteURL;
    }

    function getNodeValue(obj, tag) {
        return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
    }

    function QueryListEx(listGuid, where, rowLimit, extractRows) {
        var a = new ActiveXObject("Microsoft.XMLHTTP");
        if (a == null) return null;
        a.Open("POST", GetRootUrl() + "_vti_bin/dspsts.asmx", false);
        a.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        a.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/dsp/queryRequest");

        var d = '<?xml version=\"1.0\" encoding=\"utf-8\"?>'
    + "<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:Header xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope\/\">"
    + " <dsp:versions xmlns:dsp=\"http://schemas.microsoft.com/sharepoint/dsp\">"
    + " <dsp:version>1.0</dsp:version>"
    + " </dsp:versions>"
    + " <dsp:request xmlns:dsp=\"http://schemas.microsoft.com/sharepoint/dsp\""
    + " service=\"DspSts\" document=\"content\" method=\"query\">"
    + " </dsp:request>"
    + " </soap:Header>"
    + "<soap:Body>"
    + "<queryRequest "
    + " xmlns=\"http://schemas.microsoft.com/sharepoint/dsp\">"
    + " <dsQuery select=\"/list[@id='" + listGuid + "']\""
    + " resultContent=\"dataOnly\""
    + " columnMapping=\"attribute\" resultRoot=\"Rows\" resultRow=\"Row\">"
    + " <Query RowLimit=\"" + rowLimit + "\">"
    + " <Where>" + where + "</Where>"
    + " </Query>"
    + " </dsQuery>"
    + " </queryRequest>"
    + "</soap:Body>"
    + "</soap:Envelope>";

        a.Send(d);

        if (a.status != 200)
            return null;

        var xmlDoc = a.responseXML;
        var responseElement = xmlDoc.getElementsByTagName("Row");
        var temp = '';        
        for (i = 0; i < responseElement.length; i++) {
            //Store Title Column Value
            curUserLastName[i] = responseElement[i].getAttribute('Title');

            var expOwner = curUserLastName[i];
           
            //To Execute This Code At The End Of The Page
            Sys.Application.add_load(function () {
                document.getElementById(
                getSubControlID(expOwnerCID, g_EntityEditorUpLevelId)).innerHTML = (expOwner);
                onKeyUpRw(expOwnerCID);
            });
                    }
    }
    QueryListEx(listGUID, whereStatement, 1, true);
</script>

A good source for JavaScript API that help me to complete this job:

No comments:

Post a Comment