Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have applied the jslink to the new form of custom list. In that JSLink, I am getting the user profile property and displaying it in label. But I want to blank the text of label when the user name is removed from the people picker control.

My code is:

var userProfileProperty;
var scriptPath = ['/_layouts/15/SP.UserProfiles.js'];      //['/_layouts/15/sp.js', '/_layouts/15/SP.UserProfiles.js'];
var terms = new Array();
var selectedTermName = "";
var _termLabel = "termName";
var _termId = "termId";
var SspId = "";
var TermsetId = "";
var _type = "type";
var _parentTermId = "parentTermId";
var _parentTermType = "ParentTerm";
var _subTermType = "SubTerm";

function LoadScript() {    
    var headTagName = 'head';
    for (count = 0; count < scriptPath.length; count++) {       

            var headTag = document.getElementsByTagName(headTagName)[0];
            var scriptTag = document.createElement('script');
            scriptTag.setAttribute('type', 'text/javascript');
            scriptTag.setAttribute('src', scriptPath[count]);
            headTag.appendChild(scriptTag);        
    }
}

function loadUserData(finalUserName) {

    //alert("Load User Data");
    //Get Current Context
    var clientContext = new SP.ClientContext.get_current();
    //Get Instance of People Manager Class
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    //Property to fetch from the User Profile

    var propertyName = "oiplbNativeDepartment";

    //Domain\Username of the user (If you are on SharePoint Online)

    var targetUser = finalUserName;

    //If you are on On-Premise:
    //var targetUser = domain\\username
    //Create new instance of UserProfileProperty
    //alert(targetUser);
    userProfileProperty = peopleManager.getUserProfilePropertyFor(targetUser, propertyName)
    //Execute the Query. (No load method necessary)
    clientContext.executeQueryAsync(onSuccess, onFail);
}

function onSuccess() {

    var messageText = "\"Native Department \" property is " + userProfileProperty.get_value();

    //alert(userProfileProperty.get_value());

    var value = userProfileProperty.get_value();

    document.getElementById('lblDepartmentValue').innerHTML = value;
}

function onFail(sender, args) {
    alert("Error: " + args.get_message());
}

(function () {

    var DepartmentCtx = {};


    DepartmentCtx.Templates = {};
    DepartmentCtx.Templates.Fields = {
        "oiplbDepartment": {
            "NewForm": oiplbDepartment
        }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(
        DepartmentCtx
        );
})();

function oiplbDepartment(ctx) {

    var managerField = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx);

    return "<Label ID='lblDepartmentValue'></Label>";
}

$(document).ready(function () {   

    LoadScript();
    $("input[title='Project Owner']").focusout(function () {        

        var loginName = $("span.ms-entity-resolved").attr("ID");

        alert(loginName);
        var start = loginName.indexOf(":");

        var end = loginName.indexOf("_Processed");

        var finalUserName = loginName.substring(start - 1, end);      

        var finalUserName =
        loadUserData(finalUserName);

        var loginName = $("span.ms-entity-resolved").attr("ID");

        if(loginName == undefined)
        {
            alert("LKJ");
        }
    });

    $("input[title='Project Owner']").OnControlResolvedUserChanged(function(){
        alert("Valued Deleted");
    });
});

After applying the above javascript code I am able to get the user profile property whenver the client people picker control's focusout is fired but I want to hide the label whenver the the entered user is removed from the control.

What should I do?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.