Wednesday, September 2, 2015

Select lookup field in NewForm.aspx using Querystring


We all knew about lookup column in SharePoint. It is used to get reference from another list. Here, I am going to give an idea to select this lookup column in New form using querystring.

Pass your querystring to Newform.aspx like

http://yourmachinename:portnumber/lists/listname/newform.aspx?itemId=1

Insert a content editor in list's new form. Paste the below script in content editor
<script>
$(document).ready(function ($) {
function SetFormFields() {
    
    var QItemID = GetQueryStringParameter("itemId", "NULL");
    var IDLookup = SPUtility.GetSPFieldByInternalName('yourcolumnname');
    if (QItemID != "NULL") {
        IDLookup.SetValue(QItemID);
    }    
}
});

function GetQueryStringParameter(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
</script>

No comments:

Post a Comment