jquerysharepointhttp-redirectcontenttypedynamic-url

SharePoint JQuery Redirect NewForm.aspx depending on content type and Start Time


Aloha All,
I'm using a SharePoint 2007 calendar with four content types.

  1. Standard Meeting
  2. Skype Meeting
  3. Telephone Meeting
  4. Chat Meeting

I'm using JQuery 1.4.2.

I'm trying to create an alert popup on the calendar page for Skype meetings that are scheduled less than 72 hours in advance. I have the alert built into the Calendar page to show up if "SkypeError=1" is passed in the URL.

Right now I'm trying to get the NewForm.aspx to recognize that the Skype Meeting content type has been selected and display the error. In the PreSaveAction function I have an alert showing that the ContentTypeId is being recognized, another alert to show what dynamic URL has been selected. Then in the setOnSubmitRedir function I have an alert showing that the same URL was passed into it and another alert to show the full url was modified for the Submit button.

When adding a Satandard Meeting to the calendar everything works fine as expected. However, if I choose any of the other content types only the cancel button works. If I press Submit I get the generic MOSS2007 "An unexpected error has occurred." which is odd considering I can see the correct info is being passed through. Plus when I look at the URL of the error page I see it there:
https://myURL/myPortal/Lists/ConfRes/NewForm.aspx?RootFolder=%2fmyPortal%2fLists%2fConfRes&Source=/myPortal/Pages/MeetingCalendar.aspx?SkypeTime=yes

I've tried removing "?SkypeTime=yes" from dynamic redirect thinking maybe the "?" was causing issues.
Everything seems to fail when it's saving the content. The content is never saved when getting the error. When I remove the code from the page the submit works fine for all content types.

Assistance is greatly appreciated and needed. Any help with also figuring out how to test for events added less than 72 hours from the start time is also greatly appreciated.

The code is added using a CEWP. I don't have server rights to enable more robutst errors or look at the logs.

<script type="text/javascript" src="/94thaamdc/SiteCollectionCode/jquery-1.4.2.min.js">
//Load JQuery
</script>

<script type="text/javascript">
// Where to go when cancel is clicked on the form
goToWhenCanceled = '/myPortal/Pages/MeetingCalendar.aspx';
// Edit the redirect on the cancel-button's
$('.ms-ButtonHeightWidth[id$="GoBack"]').each(function(){
    $(this).click(function(){
            STSNavigate(goToWhenCanceled);
      })
});

// Function to determine the dynamic URL for the OnSubmit-redirect.
// This function is automatically executed before saving the item.
function PreSaveAction(){
// The URL is determined by the ContentTypeId located in the URL of the content type link.
// Grab the ContentTypeId from the content type link's URL and save it to a local
// variable called contentTypeId.
var contentTypeId = querySt("ContentTypeId");
// Assign a dynamic redirect URL to the function by setting it here
// based on contentTypeId from the URL.
    if(contentTypeId=='0x010...'){
        var dynamicRedirect = '/myPortal/Pages/MeetingCalendar.aspx?SkypeError=1';
    }else{
        var dynamicRedirect = '/myPortal/Pages/MeetingCalendar.aspx';
    }
    // Alerts are just to watch what's going on in the code. Remove when done testing.
    alert("1. contentTypeId:  "+contentTypeId);
    alert("2. dynamicRedirect:  "+dynamicRedirect);
    // Call the function and set the redirect URL in the form-action attribute
    setOnSubmitRedir(dynamicRedirect);
    // This function must return true for the save item to happen
    return true;
}

//Function to parse the ContentTypeId from the URL string
function querySt(stKey) {
    stQString = window.location.search.substring(1);
    arKeyValues = stQString.split("&");
    for (i = 0; i < arKeyValues.length; i++) {
        arPairs = arKeyValues[i].split("=");
        if (arPairs[0] == stKey) {
            return arPairs[1];
        }
    }
};

// Function to edit the form-action attribute to add the source=yourCustomRedirectPage
function setOnSubmitRedir(redirURL){
var action = $("#aspnetForm").attr('action');
var end = action.indexOf('&');
    if(action.indexOf('&')<0){
        newAction = action + "?Source=" + redirURL;
    }else{
        newAction = action.substring(0,end) + "&Source=" + redirURL;
    }
$("#aspnetForm").attr('action',newAction);
// Test to see if the URL was passed correctly. Remove when done testing.
alert("3. redirURL:  "+redirURL);
// Test to see if the new URL is properly formatted. Remove when done testing.
alert("4. newAction:  "+newAction);
}
</script>

Solution

  • Since I couldn't figure out why the redirect wasn't working I took another path that doesn't require it. I'd still like to know what's wrong with the original attempt though. I need to improve the actual warning box that pops up but the below code works well. The warning only displays for the person who entered the event and it shows up each time they go to the calendar until they enter a new event over 72 hours prior.

    <script type="text/javascript" language="javascript" src="/myPortal/jquery.1.4.2.min.js"></script>
    <script type="text/javascript" language="javascript" src="/myPortal/jquery.SPServices-0.7.2.min.js"></script>
    <script type="text/javascript" language="javascript">
    /*
    Name:           EventTimeLimit
    Developer:      RogueVeggie
    Version - Date: v1 - 20130207
    
    Purpose:        Test if a Skype meeting has been added less than 72 hours prior to 
                    the actual meeting time in a SharePoint Calendar.
                    A warning displays if this is the last event entered by the user.
    
    Prerequisite:   Create a Content Type called "Skype Meeting".
                    In the SharePoint calendar Create a Calculated column called Skype72hrTest.
                    Paste the following script into it:
    
                    IF([Content Type]="Skype Meeting",IF([Sit Down Time]-Created>=3,"Pass","Fail"),"NA")
    
                    Add this code to a Content Editor Web Part at the bottom of your SharePoint 
                    Calendar page.
                    Set the web part Chrome to "None".
                    Don't hide the web part or the warning won't display.
    */
    
    var taskListName = "MYGUID12-3456-7890-ASDF-GHJKLZXCVBNM" // This is the GUID of the list we want to query.
    var lastId = $().SPServices.SPGetLastItemId({ 
        listName: taskListName 
    }); // This gets the ID of the last event added by the current user.
    var testQuery = "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Text'>"+lastId+"</Value></Eq></Where></Query>"; // This is our CAML Query string.
    var divWarn = "<DIV style='Z-INDEX: 999; POSITION: absolute; PADDING-BOTTOM: 25px; BACKGROUND-COLOR: #800505; PADDING-LEFT: 25px; WIDTH: 743px; PADDING-RIGHT: 25px; HEIGHT: 175px; COLOR: white; FONT-SIZE: 150%; TOP: 55%; FONT-WEIGHT: 900; PADDING-TOP: 25px; LEFT: 22.38%' id='VTCWarn'><DIV style='FONT-SIZE: 200%'>WARNING:</DIV><DIV>Skype scheduling requires 72 hour notice to register.<BR>Please contact IT.</DIV></DIV>";//This is the warning message
    
    $(document).ready(function() {
      $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: taskListName ,
       CAMLRowLimit: 1,
        CAMLQuery: testQuery,
        CAMLViewFields: "<ViewFields><FieldRef Name='Skype72hrTest' /></ViewFields>",// Skype72hrTest is a calculated column in the calendar to test if the event was added less than 72 hours before the Skype meeting.
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
                var passFail = $(this).attr("ows_Skype72hrTest");// Skype72hrTest stores the result in a string as "Pass", "Fail", or "NA".
    
                if (passFail === "string;#Fail"){// Tests to see if the string Fail is in the Skype72hrTest column
                    $("#Warning").append(divWarn);
                }
            });
        }
      });
    });
    // The below DIV is needed so we have a place to put the warning when passFail=Fail.
    </script>
    <div id='Warning'></div>