Search code examples
javascriptsharepoint-2010google-chrome-devtoolsindexof

Why is indexOf not recognized in my .ascx file (Sharepoint 2010)?


I have this code in my Sharepoint app:

function upsertPostTravelListItemTravelerInfo1() {
    var clientContext = SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('PostTravelFormFields');        
    this.website = clientContext.get_web();
    currentUser = website.get_currentUser();
    var commonEffluvia = 'i:0#.f|smp|';
    if (currentUser.indexOf(commonEffluvia) > -1) {
        currentUser = currentUser.substr(commonEffluvia.length, currentUser.length - commonEffluvia.length);
    }
    . . .

The reduction of currentUser is necessary because for some reason it often returns 'i:0#.f|smp|' + the user's email address, such as ''i:0#.f|smp|clayshan@ucsc.edu' rather than simply 'clayshan@ucsc.edu'

This code works just dandy in jsfiddle, as evidenced in my fiddle faddle here, but in the code above, running in Chrome, I see, 'Uncaught TypeError: currentUser.indexOf is not a function'

Why does a Javascript method recognized by jsfiddle go unrecognized by Chrome, or Sharepoint (or "whoever" the problem is)?


Solution

  • The short answer is that currentUser will be an Object of type SPUser, not a string.

    Once you've hydrated that variable with a call to clientContext.load(currentUser) followed by a call to clientContext.executeQueryAsync(), you'll be able to access its member methods such as currentUser.get_loginName() and currentUser.get_title() (neither of which seem to be fully documented in Microsoft's JavaScript class library documentation yet), which will allow you to access specific user properties directly... which should be easier than trying to parse out that effluvia.