http://www.vrdmn.com/2012/12/working-with-taxonomy-and-javascript-in.html
http://code.msdn.microsoft.com/office/SharePoint-2013-Create-and-1ebbdd36
http://code.msdn.microsoft.com/office/SharePoint-2013-Create-and-1ebbdd36
vs2012->file->new->project c# ->office/SharePoint->apps->apps for SharePoint 2013-> "SP_Taxonomy_js"
Click on 'Finish'.
Clink on 'Sign in'
Go to SP_Taxonomy_js -> Pages -> Open 'Dafault.aspx' page
add below java script reference in 'PlaceHolderAdditionalPageHead'
<script type="text/javascript" src="../Scripts/SP.Taxonomy.js"></script>
Add below text in 'PlaceHolderMain'
<div>
<p id="message">
<!--
The following content will be replaced with the user name when you run the app
- see App.js -->
initializing...
</p>
</div>
<div>
<input id="listExisting" type="button" value="List Managed
Metadata" />
<input id="createTerms" type="button" value="Create Privacy
Terms" />
<div id="report"></div>
</div>
Go to SP_Taxonomy_js -> Scripts -> Add 'SP.Taxonomy.js' from physical folder
Go to SP_Taxonomy_js -> Scripts -> 'App.js' open
Add below java script
'use
strict';
var context;
var web;
var site;
var session;
var termStore;
var groups;
var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
$(document).ready(function () {
site = context.get_site();
web = context.get_web();
$('#listExisting').click(function () { listGroups(); });
$('#createTerms').click(function () { createTerms(); });
getUserName();
});
function getUserName() {
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}
function onGetUserNameSuccess() {
$('#message').text('Hello ' + user.get_title());
}
function onGetUserNameFail(sender, args) {
alert('Failed
to get user name. Error:' + args.get_message());
}
function listGroups() {
session =
SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStore =
session.getDefaultSiteCollectionTermStore();
context.load(session);
context.load(termStore);
context.executeQueryAsync(onListTaxonomySession,
onFailListTaxonomySession);
}
function onListTaxonomySession() {
groups = termStore.get_groups();
context.load(groups);
context.executeQueryAsync(onRetrieveGroups,
onFailRetrieveGroups);
}
function onRetrieveGroups() {
$('#report').children().remove();
var groupEnum = groups.getEnumerator();
while (groupEnum.moveNext()) {
var currentGroup = groupEnum.get_current();
var groupName = document.createElement("div");
groupName.setAttribute("style", "float:none;cursor:pointer");
var groupID = currentGroup.get_id();
groupName.setAttribute("id",
groupID);
groupName.setAttribute("onclick", "showTermSets('" +
groupID + "');");
groupName.appendChild(document.createTextNode(currentGroup.get_name()));
$('#report').append(groupName);
}
}
function showTermSets(groupID) {
var parentDiv = document.getElementById(groupID);
while (parentDiv.childNodes.length > 1) {
parentDiv.removeChild(parentDiv.lastChild);
}
var groupEnum = groups.getEnumerator();
while (groupEnum.moveNext()) {
var currentGroup = groupEnum.get_current();
if (currentGroup.get_id() == groupID) {
context.load(currentGroup);
context.executeQueryAsync(
function () {
var termSets =
currentGroup.get_termSets();
context.load(termSets);
context.executeQueryAsync(
function () {
var termSetEnum =
termSets.getEnumerator();
while (termSetEnum.moveNext()) {
var currentTermSet =
termSetEnum.get_current();
var termSetName =
document.createElement("div");
termSetName.appendChild(document.createTextNode(" + " +
currentTermSet.get_name()));
termSetName.setAttribute("style", "float:none;cursor:pointer;");
var termSetID =
currentTermSet.get_id();
termSetName.setAttribute("id", termSetID);
termSetName.setAttribute("onclick", "showTerms(event,'" + groupID + "','" + termSetID + "');");
termSetName.setAttribute("id", termSetID);
parentDiv.appendChild(termSetName);
}
},
function () {
parentDiv.appendChild(document.createTextNode("An error occurred in loading the term sets for this
group"));
});
},
function () {
parentDiv.appendChild(document.createTextNode("An error occurred in loading the term sets for this
group"));
});
break;
}
}
}
function showTerms(event, groupID, termSetID) {
event.cancelBubble = true;
var parentDiv = document.getElementById(termSetID);
while (parentDiv.childNodes.length > 1) {
parentDiv.removeChild(parentDiv.lastChild);
}
var groupEnum = groups.getEnumerator();
while (groupEnum.moveNext()) {
var currentGroup =
groupEnum.get_current();
if (currentGroup.get_id() == groupID) {
context.load(currentGroup);
context.executeQueryAsync(
function () {
var termSets = currentGroup.get_termSets();
context.load(termSets);
context.executeQueryAsync(
function () {
var termSetEnum =
termSets.getEnumerator();
while (termSetEnum.moveNext()) {
var currentTermSet =
termSetEnum.get_current();
if (currentTermSet.get_id() ==
termSetID) {
context.load(currentTermSet);
context.executeQueryAsync(
function () {
var terms =
currentTermSet.get_terms();
context.load(terms);
context.executeQueryAsync(
function () {
var
termsEnum = terms.getEnumerator();
while
(termsEnum.moveNext()) {
var
currentTerm = termsEnum.get_current();
var term =
document.createElement("div");
term.appendChild(document.createTextNode(" - " +
currentTerm.get_name()));
term.setAttribute("style", "float:none;margin-left:10px;");
parentDiv.appendChild(term);
}
},
function () {
parentDiv.appendChild(document.createTextNode("An error occurred when trying to retrieve terms in
this term set"));
});
},
function () {
parentDiv.appendChild(document.createTextNode("An error occurred when trying to retrieve terms in
this term set"));
});
break;
}
}
},
function () {
parentDiv.appendChild(document.createTextNode("An error occurred when trying to retrieve terms in
this term set"));
});
},
function () {
parentDiv.appendChild(document.createTextNode("An error occurred when trying to retrieve terms in
this term set"));
});
break;
}
}
}
function onFailRetrieveGroups(sender, args) {
$('#report').children().remove();
$('#report').append("Failed to
retrieve groups. Error:" +
args.get_message());
}
function onFailListTaxonomySession(sender, args) {
$('#report').children().remove();
$('#report').append("Failed to
get session. Error: " + args.get_message());
}
function createTerms() {
session =
SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStore =
session.getDefaultSiteCollectionTermStore();
context.load(session);
context.load(termStore);
context.executeQueryAsync(onGetTaxonomySession, onFailTaxonomySession);
}
function onGetTaxonomySession() {
var guidGroupValue = new SP.Guid.newGuid();
var guidTermSetValue = new SP.Guid.newGuid();
var guidTerm1 = new SP.Guid.newGuid();
var guidTerm2 = new SP.Guid.newGuid();
var guidTerm3 = new SP.Guid.newGuid();
var guidTerm4 = new SP.Guid.newGuid();
var myGroup = termStore.createGroup("CustomTerms",
guidGroupValue);
var myTermSet = myGroup.createTermSet("Privacy",
guidTermSetValue, 1033);
myTermSet.createTerm("Top Secret", 1033,
guidTerm1);
myTermSet.createTerm("Company Confidential", 1033, guidTerm2);
myTermSet.createTerm("Partners Only",
1033, guidTerm3);
myTermSet.createTerm("Public", 1033,
guidTerm4);
groups = termStore.get_groups();
context.load(groups);
context.executeQueryAsync(onAddTerms,
onFailAddTerms);
}
function onAddTerms() {
listGroups();
}
function onFailAddTerms(sender, args) {
$('#report').children().remove();
$('#report').append("Failed to
add terms. Error: " + args.get_message());
}
function onFailTaxonomySession(sender, args) {
$('#report').children().remove();
$('#report').append("Failed to
get session. Error: " + args.get_message());
}
SP_Taxonomy_js -> right click -> Deploy
This is my Office 365 Developer Site Central Administration -> Term Store
This is my Office 365 Developer Site Central Administration -> Term Store
Now open Deployed SP_Taxonomy_js App
Now click on 'List Managed Metadata' button
Now click on 'Create Privacy Terms' button
Here I am getting error because i am using online office 365 developer site trail version. So I don't have chance to create new term store.
But actually code should work.
Thank You
No comments:
Post a Comment