4

I have an edit form that opens up in a modal dialog box. I need to navigate it to different forms on SAVE/CANCEL clicks as per the user-group of currently logged in user.

For eg, project leaders to navigate back to My Quotes Finance team to navigate back to All quotes, etc...

(as of now all users get redirected to My Quotes)

Here is the piece of code of the modal box: How do I update the code to change its navigation rules?

<script type="text/javascript" src="/staging/CEIT/SiteAssets/Scripts/jquery.SPServices-2014.02/jquery.SPServices-2014.02.min.js"></script>
                    <script src="http://code.query.com/jquery-1.10.1.min.js"></script>


                        <script type="text/javascript">

                        document.getElementById(&quot;Approval&quot;).addEventListener (&quot;click&quot;, displayLayover, false);

                        function displayLayover() 
                        {
                            JSRequest.EnsureSetup();
                            var issueID = JSRequest.QueryString[&quot;ID&quot;];
                            var options = new Array();
                            options.url = '/sites/CEIT/Lists/QuoteSystemCEIT/Edit_Assigned.aspx?ID=' + issueID; 
                            options.allowMaximize = true;
                            options.showClose = true;
                            options.dialogReturnValueCallback = Function.createDelegate(null, CloseDialogCallbackAssign);
                            SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);

                        }
                        function CloseDialogCallbackAssign(dialogResult, returnValue)
                        {
                            if(dialogResult == SP.UI.DialogResult.OK)
                            {

                                SP.UI.Status.removeAllStatus(true);
                                SP.UI.ModalDialog.RefreshPage(dialogResult);
                                //window.location.replace('/sites/CEIT/SitePages/My%20Quote%20Requests.aspx');
                                //SP.UI.Status.removeAllStatus(true);
                                SP.UI.ModalDialog.RefreshPage(dialogResult);
                                alert("hi");
                                $().SPServices({
                                    operation:"GetGroupCollectionFromUser",
                                    userLoginName:$().SPServices.SPGetCurrentUser(),
                                    async:false,
                                    completefunc: function (xData, Status) {
                                   if (($(xData.responseXML).find("Group[Name='Finance group']").length == 1)||($(xData.responseXML).find("Group[Name='Marketing Group']").length == 1)||($(xData.responseXML).find("Group[Name='Quote Team']").length == 1))
                                    {               
                                       alert("I am not a leader");
                                         window.location.replace('/sites/CEIT/SitePages/All%20Quote%20Requests.aspx');
                                    }
                                    else if (($(xData.responseXML).find("Group[Name='Project Leader']").length == 1)) 
                                    {
                                        alert("I am leader");                                           
                                        window.location.replace('/sites/CEIT/SitePages/My%20Quote%20Requests.aspx');       
                                    }           
                                }});

                             }
                             else if(dialogResult == SP.UI.DialogResult.cancel)
                            {
                                SP.UI.Status.removeAllStatus(true);
                            }

                        }</script>
6

Yes, You can fulfill this requirement. I assume, you are having 2 SP groups - Project Leaders & Finance Team and you want to redirect user based on their group membership after closing the dialog box.

You can use SpServices.js for getting user membership. Download SPServices. Make sure you have Jquery reference in your page. I am updating part of your code:

function CloseDialogCallbackAssign(dialogResult, returnValue)
    {
        if(dialogResult == SP.UI.DialogResult.OK)
        {

            SP.UI.Status.removeAllStatus(true);
            SP.UI.ModalDialog.RefreshPage(dialogResult);
            $().SPServices({
                operation: "GetGroupCollectionFromUser",
                userLoginName: $().SPServices.SPGetCurrentUser(),
                async: false,
                completefunc: function (xData, Status) {
                if (($(xData.responseXML).find("Group[Name='Project Leaders']").length == 1)) 
                {               
                    window.location.replace('/sites/CEIT/SitePages/My%20Quote%20Requests.aspx');
                }
                else if (($(xData.responseXML).find("Group[Name='Finance Team']").length == 1)) 
                {
                    window.location.replace('/sites/CEIT/SitePages/All%20Quote%20Requests.aspx');       
                }           
            });
            else {
                alert("Oops! user is not mapped to respective SP group yet");
            }
        }
        else if(dialogResult == SP.UI.DialogResult.cancel)
        {
            SP.UI.Status.removeAllStatus(true);
        }
    }

EDIT : Please note you SP group must have enabled for all users can view membership enter image description here

  • that's great.... Happy SharePoint-ing – DvG Jun 16 '17 at 8:13
  • hey there is a problem, it redirects me to the same page now. It is not working from $().SPServices..... Please check my updated code :( – Sanjana Chhabira Jun 16 '17 at 8:29
  • are you pointing SPService.js file properly? Is your alert ("Hi") is popping up? .... try using Debugger tools (F12) to see errors/issues.! – DvG Jun 16 '17 at 9:59
  • yes "Hi" is popping up and even SP services is linked properly . – Sanjana Chhabira Jun 16 '17 at 10:16
  • Then It must work fine. have you tried debugging, using F12, setting breakpoint and see what is returned in completeFunc(xData)? – DvG Jun 16 '17 at 10:17
1

Answer to my posted question:

<script src="http://code.query.com/jquery-1.10.1.min.js"></script>
                    <script type="text/javascript">

                        document.getElementById(&quot;PaymentComplete&quot;).addEventListener (&quot;click&quot;, displayLayoverPaymentComplete, false);
                        function displayLayoverPaymentComplete() 
                        {
                            JSRequest.EnsureSetup();
                            var issueID = JSRequest.QueryString[&quot;ID&quot;]
                            var options = new Array();
                            options.url = '/sites/CEIT/Lists/QuoteSystemCEIT/Edit_Payment_Complete.aspx?ID=' + issueID; 
                            options.allowMaximize = true;
                            options.showClose = true;
                            options.dialogReturnValueCallback = Function.createDelegate(null, CloseDialogCallbackPC);
                            SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
                        }
                        function CloseDialogCallbackPC(dialogResult, returnValue)
                        {
                            if(dialogResult == SP.UI.DialogResult.OK)
                            {

                                SP.UI.Status.removeAllStatus(true);
                                SP.UI.ModalDialog.RefreshPage(dialogResult);
                                ExecuteOrDelayUntilScriptLoaded(TempMethod, "SP.js");
                                SP.SOD.executeFunc('sp.js', 'SP.ClientContext', TempMethod);
                                CheckCurrentUserMembership();                
                             }
                             else if(dialogResult == SP.UI.DialogResult.cancel)
                            {
                                SP.UI.Status.removeAllStatus(true);
                            }
                        }
                        var userGroups;
                        function TempMethod()
                        {}
                        function CheckCurrentUserMembership() {
                        var clientContext = SP.ClientContext.get_current();
                        var currentUser = clientContext.get_web().get_currentUser();
                        clientContext.load(currentUser);
                        userGroups = currentUser.get_groups();
                        clientContext.load(userGroups);
                        clientContext.executeQueryAsync(OnQuerySucceeded); var done = true;
                        //alert("Project Leader has been assigned successfully!!!");
                        }
                        function OnQuerySucceeded() {
                        var isLeader = false;
                        var groupsEnumerator = userGroups.getEnumerator();
                        while (groupsEnumerator.moveNext()) {
                        var group= groupsEnumerator.get_current();
                        if(group.get_title() == "Project Leader") {
                        isLeader = true;
                        window.location.replace('/sites/CEIT/SitePages/My%20Quote%20Requests.aspx');
                        break;
                        }
                        else { window.location.replace('/sites/CEIT/SitePages/All%20Quote%20Requests.aspx'); break; }
                        }

                        OnResult(isMember);
                        }

                        function OnQueryFailed() { OnResult(false); }</script>
  • This works fine as you are using CSOM here instead of SPServices – DvG Jun 16 '17 at 18:11
  • Yes exactly. Thanks for help though :) – Sanjana Chhabira Jun 16 '17 at 20:17

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.