Page Not Found Issue fixed after using _spPageContextInfo.webServerRelativeUrl

I was working with showModalDialog function and everything working fine, the issue came with the code when I deployed the code on other servers(IT, UAT...). The URL was causing an issue and because of this I was getting "Page not found" message when function "showModeldailog" was called from the JS file.

The location of the Page (Inside the layout folder).


The Problem:
We have a different-2 sever and each server has its own site collection with diff URL and
Dev System
In my development server I had created the URL without manage path.(http:severname/_layouts/15/testProj/MyPage.aspx) and it is working fine in my Development server.
Other Servers
Other servers like (IT, UAT & PROD) the URL was causing an issue.The URL : '/_layouts/15/testProj/MyPage.aspx'. I used  below for URL creation. ("Page Not Found" issue cause on these servers)

URL creation method on Dev System
function ShowModelDialog(listid, itemid) {
    var fullUrl = '/_layouts/15/testProj/MyPage.aspx?List=' + listid + '&itemid=' + itemid + '&IsDlg=1'
    var dialogOptions = {
        url: fullUrl,
        title: 'My Page',
        showClose: true,
        dialogReturnValueCallback: DialogCallback
    }
    SP.UI.ModalDialog.showModalDialog(dialogOptions);;
};

To finding the root cause of the issue. I checked the files that located under folder and all the files were there, when I saw the site URL it was created under the manage path. here is the gotcha Then I change the code.

After done some changes the below code working on my IT and other servers.
function ShowModelDialog(listid, itemid) {
    var u = _spPageContextInfo.webServerRelativeUrl;
    if (u.length > 0) { if (u[u.length - 1] != '/') u += '/'; }
    var fullUrl = u + '/_layouts/15/testProj/MyPage.aspx?List=' + listid + '&itemid=' + itemid + '&IsDlg=1'
    var dialogOptions = {
        url: fullUrl,
        title: 'My Page',
        showClose: true,
        dialogReturnValueCallback: DialogCallback
    }
    SP.UI.ModalDialog.showModalDialog(dialogOptions);
}

Problem not solved yet it, the above code working on my IT server but not working on my development server :(.. Now what to do.. I have checked my code twice and thrice no luck...

Solution

At last I found the issue and the problem solved by removing the extra "/" slash and problem solved.
Even the extra "/" can cause the issue.

The finally the working code for all the environment (IT , UAT & PROD) and Development server as well :)

function ShowModelDialog(listid, itemid) {
    var u = _spPageContextInfo.webServerRelativeUrl;
    if (u.length > 0) { if (u[u.length - 1] != '/') u += '/'; }
    var fullUrl = u + '_layouts/15/testProj/MyPage.aspx?List=' + listid + '&itemid=' + itemid + '&IsDlg=1'
    var dialogOptions = {
        url: fullUrl,
        title: 'My Page',
        showClose: true,
        dialogReturnValueCallback: DialogCallback
    }
    SP.UI.ModalDialog.showModalDialog(dialogOptions);
}

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

STS CryptographicException Error : Key set does not exist

SharePoint 2013 Search =>Increase CutoffMaxBuckets size of the refiner