0

My modal dialog for newform is not capturing the query string value I'm trying to pass to it. Alert on main page confirms the qs value for XID is set correctly.

    var xid = GetUrlKeyValue("ID", false, location.href);
    var xurl = "https://mysite.sharepoint.com/sites/siteX";
    var xmodal = xurl + "/Lists/List_Tasks/NewForm.aspx?XID=" + xid;

    $('#WebPartWPQ5 #idHomePageNewItem').attr('href', '#');
    $('#WebPartWPQ5 #idHomePageNewItem').attr('onclick', function () {
        return "javascript:showDialog('" + xmodal + "');"
    });

    function showDialog(url) {
        var options = {
            autosize: true,
            url: url,
        };
        SP.UI.ModalDialog.showModalDialog(options);
    }

I have the following placed in a script editor at the bottom of the newform to capture the qs value for XID. Nothing, alert is empty.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () { 
    var xid = GetUrlKeyValue("XID", false, location.href);
    alert(xid);
  });
</script>

Tried this, same. Alert is empty. Console: Cannot read property 'SPGetQueryString' of undefined.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    var queryStringVals = $().SPServices.SPGetQueryString();
    var xid = queryStringVals["XID"];
    alert(xid);
  });
</script>

Tried placing in a form loaded event instead of $(document).ready, no luck. Got sick of qs and tried passing args, that didn't work either.

Suggestions?

UPDATE

So, I removed the function showDialog as it is not being used at all. javascript:showDialog just opens the dialog with the specified url.

Using console.log(location.href) on the newform shows the XID from the querystring is not being passed. Why?

$('#WebPartWPQ5 #idHomePageNewItem').attr('onclick', donothing());

function donothing() {
  console.log("hello");
}

This override of the onclick event is doing nothing, literally. You can use this format to change the text, but it isn't doing anything for the onclick event.

$('#WebPartWPQ5 #idHomePageNewItem').text("add something");
  • 1
    You are using a SPService function (in your last code-snippet) but are not referencing to it. You need to load Spservices after jQuery is loaded. – Christoffer Apr 18 at 8:09
  • See what console.log(document.location.href) returns, just to check if XID is really passed. – Salah Apr 18 at 10:37
  • You're right Salah, the qs ISN'T being passed. Now to figure out why... – BDC604 Apr 18 at 16:11
  • I've never seen that method (setting the onclick attribute) of attaching a click handler with jQuery. Usually what I have seen (and done) is $('#selector').click(function() { \\code }); jQuery documentation. – Dylan Cristy Apr 18 at 16:34
1

You can maybe also use GetUrlKeyValue() if you don't want to add more libraries: https://praneethmoka.wordpress.com/2012/01/12/some-useful-javascript-variablesfunctions-in-sharepoint/ (did not test it on SharePoint Online)

0

Well, that was painfully annoying. Here's the working solution:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    $('#WebPartWPQ5 #idHomePageNewItem').text("my custom text");
    $('#WebPartWPQ5 #idHomePageNewItem').attr('href', '');
    $('#WebPartWPQ5 #idHomePageNewItem').attr('onclick', 'dosomething(); return false;');
  }

function dosomething() {   // put this outside document.ready
  var xid = GetUrlKeyValue("ID", false, location.href);
  var xurl = "https://mysite.sharepoint.com/sites/siteX";
  var xmodal = xurl + "/Lists/List_Tasks/NewForm.aspx?XID=" + xid;
  var options = {
    autosize: true,
    url: xmodal,
  };
  SP.UI.ModalDialog.showModalDialog(options);
}
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.