3

Thanks to Mark Rackley and Scott Brickley, who have contributed much help and information toward the following solution I've been working on.

At this point, everything seems to work except my attempt to redirect to a new URL after a new item has been saved. Scott Brickley gave me the foundation for the 'changeNewItemRedirect()' function, but I haven't got the syntax correct somewhere and it fails to redirect. I'm not sure at this point, if the problem is with my lack of understanding of '_spPageContextInfo.siteAbsoluteUrl' and perhaps there is an issue with my concatenation after that? I'm attempting to have it land back on the Display form of a parent list.

The functionality of grabbing the ID of the Parent item is there and working for my other function, so I would assume it would work in this one as well. However, perhaps I'm going to have to add some parenthesis or alter my quotations somehow, to get the syntax right?

Thanks for taking a look and for any suggestions you may have.

<!--
    Name: newChild.js 
-->

<script type="text/javascript" src="https://My-Domain.sharepoint.com/BusinessOffice/SiteAssets/jquery.min.js"></script>
<script type="text/javascript">


    _spBodyOnLoadFunctionNames.push("setPurchaseOrderID");
    _spBodyOnLoadFunctionNames.push("changeNewItemRedirect");



    //get the Issue ID from the query string
    var PurchaseOrderID = getParameterByName("PurchaseOrder");

function setPurchaseOrderID() {    

    //Set the corresponding Lookup field value to the Issue ID
    $("select[title='PurchaseOrder']").val(PurchaseOrderID);    
    //use this line to disable the lookup field selection
    $("select[title='PurchaseOrder']").attr('disabled','disabled');
    //use this line to hide the lookup field and label entirely
    $("select[title='PurchaseOrder']").closest("tr").hide();  

};

function changeNewItemRedirect() {
    var redirectUrl = _spPageContextInfo.siteAbsoluteUrl + "/Lists/Purchase%20Orders/DispForm.aspx?ID=" + PurchaseOrderID;
    WPQ2FormCtx.RedirectInfo.redirectUrl = redirectUrl;  
};



// no, I didn't write this function from scratch, I found it at
// http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

</script>

*****UPDATE******

Everything in this code seems to be properly formatted and appears to be working...the redirect just doesn't happen.

I dug deeper into the Dev Tools in my browsers and was able to find the ability to 'Watch Expressions' in Chrome. I was able to add a watch for WPQ2FormCtx.RedirectInfo.redirectUrl. When the page loads, nothing shows up as the value for this, however by refreshing the 'watch', the url value I'm pushing comes up in perfect form, properly displaying the value from the concatenated query string value. I can copy the URL there into a browser window and it immediately pulls up the page I'm shooting for.

However on clicking 'Save' from the new item form, when it should redirect to my injected value, it instead goes straight back to the main parent list view, which is the default behavior. It never redirects to the intended page. I'm stumped. It seems like everything is correct, but the functionality doesn't happen. Could it be a timing thing with _spBodyOnLoadFunctionNames? Any suggestions? Thanks for your insight.

Error After adding console.log to redirect function:

  • 1
    You could use GetUrlKeyValue SharePoint function to get the query string value – Nadeem Yousuf May 19 '15 at 14:51
  • Thanks for your input. I'm assuming you're talking about replacing the current 'getParameterByName' function with 'GetUrlKeyValue'? If so, I'll look at that. I can see in the Source tab of Developer Tools that my concatenation is working as expected and the 'WPQ2FormCtx.RedirectInfo.redirectUrl' does get set to the correct URL. However, the redirection is not actually happening. I'm perplexed! – Jason Puckett May 19 '15 at 20:37
  • I've added up-to-date information after the code in the original post. Everything in the code seems to work properly, the final redirect just does not happen, even thought the redirectUrl appears properly set. – Jason Puckett May 20 '15 at 3:22
  • Ok, so I'm consistently getting a DOM7011 error when I enter into the form with the javascript inserted. Being new to most of this, I am learning about debugging as I go. I inserted this statement inside my function setting the redirecturl: – Jason Puckett Jun 2 '15 at 15:40
  • console.log([WPQ2FormCtx.RedirectInfo.redirectUrl]); Once run, there is an added line to my DOM7011 error. I have added a copy of the error into the original post above. It seems the log shows that the string assigned to WPQ2... is correct, but I can't figure out what is causing the redirect not to happen. Anyone with insight? – Jason Puckett Jun 2 '15 at 15:47

Your Answer

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

Browse other questions tagged or ask your own question.