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.

5
  • 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. Commented Dec 6, 2015 at 2:50
  • I updated my question with some more details as well as a bounty.
    – bgmCoder
    Commented Dec 8, 2015 at 3: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. Commented Dec 8, 2015 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
    Commented Dec 8, 2015 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
    Commented Dec 21, 2015 at 16:47

1 Answer 1

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
});
3
  • 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: sharepoint.stackexchange.com/a/53516/7452
    – bgmCoder
    Commented Dec 23, 2015 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
    Commented Dec 29, 2015 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
    Commented Jan 26, 2016 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.