2

I have custom list in which there is one user field and a taxonomy field.

I want to populate the taxonomy field value automatically based on the value entered in the user field, the value to be filled in the taxonomy field will be the user profile profile property.

I have successfully got the value from the user profile property, but I don't know how to set that value to the taxonomy field.

Here is my code:

var userProfileProperty;
var scriptPath=['/_layouts/15/sp.js','/_layouts/15/SP.UserProfiles.js'];

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 = "NativeDepartment";


//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());
}

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


$(document).ready(function () {

LoadScript();

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

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

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

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

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

    alert(finalUserName);

    loadUserData(finalUserName);

});
});

Now I will have to fill the data in the taxonomy field whose column name is:Department how should I set the value to the taxonomy field?

3

I like to use SetFieldValueByTerm. It eliminates the need for you to worry about GUIDs and the internal storage format. Here's an example:

var lcid = Application.CurrentCulture.LCID;
var session = TaxonomySession.GetTaxonomySession(context);
var store = session.TermStores.GetByName("Managed Metadata Service");
var group = store.Groups.GetByName("Managed Code");
var set = group.TermSets.GetByName("Projects");
var term = set.Terms.GetByName("Alan Parsons Project");

context.Load(term);
context.ExecuteQuery();

var list = context.get_web().get_lists().getByTitle("Bids");
var item = list.getItemById(2);
var field = list.Fields.GetByInternalNameOrTitle("Project");
var taxField = context.CastTo<TaxonomyField>(field);
taxField.SetFieldValueByTerm(item, term, lcid);
item.Update();

context.ExecuteQuery();

EDIT:

Sorry, I copied and pasted the managed code example by mistake. Here's the JavaScript code example:

var context = SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle("Bids");
var item = list.getItemById(2);

var lcid = _spPageContextInfo.currentLanguage;
var session = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var store = session.get_termStores().getByName("Managed Metadata Service");
var group = store.get_groups().getByName("JavaScript");
var set = group.get_termSets().getByName("Projects");
var term = set.get_terms().getByName("Alan Parsons Project");

context.load(item);
context.load(term);
context.executeQueryAsync(success1, fail);

function success1() {
    var field = list.get_fields().getByInternalNameOrTitle("Project");
    var taxField = context.castTo(field, SP.Taxonomy.TaxonomyField);
    taxField.setFieldValueByTerm(item, term, lcid);
    item.update();

    context.executeQueryAsync(success2, fail);
}

function success2() {
    var message = jQuery("#message");
    message.text("Item updated");
}

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

I have a whole module (chapter) on working with Managed Metadata using the Client Object Model in my SharePoint 2013 Development: Client Object Model and REST API course on Pluralsight.

  • Pluralsight not a free site. Wondering if the same is made available to the public audience in any other site. – Biju Joseph - MCSD Nov 3 '13 at 8:44
  • There's a 10 minute clip from an earlier part of the module on the Pluralsight blog (bit.ly/16nabi5). To see more you can sign-up for a free trial at (bit.ly/12fs9HG) – Rob Windsor Nov 3 '13 at 16:05
0

Try to understand this post, I think that will serve your purpose.

set-taxonomy-field-multiple-values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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