1

On my NewForm.aspx or EditForm.aspx pages I am using jQuery to fill in a peoplepicker control.

The problem is that this causes the page to POST this error:

"This Page has been modified since you opened it. You must open the page again."

This error, in the ULS is System.Web.UI.ViewStateException: Invalid viewstate.

This page has a few suggestions to solve this, but I haven't figured out just what to do yet. Also, I have a thread in the spservices Discussions over on CodePlex, but I need an answer. And there is this thread, but it's tagged C# and doesn't help me with a jQuery issue.

Here is the code I'm using to fill in the peoplepicker control:

SetPeoplePickerToCurrentUser("Requestor",false);

function SetPeoplePickerToCurrentUser(whatDisplayName, overwrite){
    //var currentControlValue = $().SPServices.SPFindPeoplePicker({  peoplePickerDisplayName: whatDisplayName }).row.find("span[class='ms-entity-resolved']").attr("title");    
    var currentControlValue = $("div[title='People Picker']").text().trim();
    if(overwrite || currentControlValue == undefined || currentControlValue == ""){ 
        var currentuser = $().SPServices.SPGetCurrentUser({ fieldName: "Name", debug: false });
        var peoplepicker = $("div[title='People Picker']");
        peoplepicker.text(currentuser); 
        $('a[title="Check Names"]').click();        
     }
}

[update]

Well, I've discovered that this actually does work, but if the user refreshes the page without saving, THEN it locks up with the viewstate error. Normally, a person can refresh without saving. The function won't modify the value again if there is anything in it. Normally, if a person refreshes a page without saving, a people-picker control gets blanked out, but the custom form's people-picker does not get emptied out.

bgmCoder
  • 3,581
  • 16
  • 61
  • 102
  • It seems to me that what you have should work, provided it is properly set up. Try this to troubleshoot: remove all your code from the form except for the jQuery reference. Then execute the two lines of JS in your browser console, one at a time, just to see if it does what you expect it to. If it does, then there's an issue with the way you've wired it up. In any case I suspect there is something else going on with this form, because it has no business throwing that invalid viewstate error. – Derek Gusoff Dec 06 '15 at 02:50
  • I updated my question with some more details as well as a bounty. – bgmCoder Dec 08 '15 at 03:46
  • The invalid viewstate error is thrown server-side unless I'm mistaken, and the JavaScript would have no effect on it whatsoever. I suspect that whatever is going on has nothing to do with this code. – Derek Gusoff Dec 08 '15 at 14:25
  • I disagree. The invalid viewstate ONLY happens when I try to set the value in the peoplepicker. Maybe its not the code directly, but it is the code indirectly. If I don't try to set the value via javascript I never see the error. – bgmCoder Dec 08 '15 at 16:16
  • Get rid of the SPServices calls and it will work. You don't need them. Get the current user from the "Welcome" text on the page, instead. – vapcguy Dec 21 '15 at 16:47

1 Answers1

1

Get rid of the SPServices calls and it will work. You don't need them. Get the current user from the "Welcome" text on the page, instead.

// Default the Requestor's Name
var welcomeText = $('[id*="zz8_Menu"]').text(); // this is zz7_Menu in SP 2007
var user = welcomeText.split('Welcome ')[1];
var counter=0;
$('div[title="People Picker"]').each(function() {
    //alert(counter);
    if (counter == 1) { // this is because I was setting the 2nd people picker on the page
        $(this).html(user);  // <-- this is the magic
    }
    counter++;
    $('a[title="Check Names"]').click();  // <-- clicks the "Check Name" button programmatically to resolve your user
});
vapcguy
  • 308
  • 2
  • 10
  • 1
    I think you are right; it was the spservices call fetching the username that must have been invalidating the page. I replaced it with your function and it worked. Nota Bene, though, for 2010, I fetched the user value with this instead: $('[id*="zz17_Menu"] span').text(); and didn't have to use split on welcomeText. But it worked! I am astounded. But you can also fetch the username without spservices like this: http://sharepoint.stackexchange.com/a/53516/7452 – bgmCoder Dec 23 '15 at 16:06
  • Yeah, they've modified that ID to be different in all the SP versions, I stopped keeping up with them. Glad you found the right ID. In 2013, they stopped putting "Welcome" beside the logged-in user's name. – vapcguy Dec 29 '15 at 22:45
  • I since found out, you could do the SPServices call, but they don't have "Name" as a field - there is "FirstName" and "LastName", and you'd have to do some concatenation: var thisUsersValues = $().SPServices.SPGetCurrentUser({ fieldNames: ["FirstName", "LastName"], debug: false }); var name = thisUsersValues.FirstName + ' ' + thisUsersValues.LastName; Then you would still use .html(name) to add it in. – vapcguy Jan 26 '16 at 16:16