Saturday, April 25, 2015


Metadata is data about data; additional information about data. In SharePoint we can manage them centrally which is nothing but Managed Metadata service or Term Store service. In SharePoint you can organize the metadata in a way which suits your business.

Let’s have quick overview on some key terminology:

Group:
In SharePoint groups in context of Managed Metadata is nothing but set of Term Sets which share common security requirements. Only users who have contributor permission on Group can manage Term Sets belongs to that group.

Term Set:
Term Set is nothing but group of related Terms. Term Sets have different scope based on location of their creation: local or global.

Terms:
A term is specific word or phrase which you can associate with list item or documents. It is single item in term set. A term has a unique ID and it can have many text labels (synonyms). If you work on a multilingual site, the term can have labels in different languages.

Enough theory. Now let’s see how to work with them in SharePoint App using JSOM or ECMA scripting.

So to start with implementation, in App you first need to get host and app web URL. You can do this using following code snippet:

        var hostweburl = getQueryStringParameter("SPHostUrl");
        var appweburl = getQueryStringParameter("SPAppWebUrl");
        hostweburl = decodeURIComponent(hostweburl);
        appweburl = decodeURIComponent(appweburl);

        function getQueryStringParameter(paramToRetrieve) {
                var params =
                    document.URL.split("?")[1].split("&");
                for (var i = 0; i < params.length; i = i + 1) {
                    var singleParam = params[i].split("=");
                    if (singleParam[0] == paramToRetrieve)
                        return singleParam[1];
                }
      }

Once you get host and app web URLs, next step is to load the ‘SP.RequestExecutor.js’ and ‘SP.Taxonomy.js’ libraries.  Following code snippet is useful:

            var scriptbase = hostweburl + "/_layouts/15/";
                                                                 $.getScript(scriptbase + "SP.Runtime.js",
                    function () {
                        $.getScript(scriptbase + "SP.js",
                        function () { $.getScript(scriptbase + "SP.RequestExecutor.js",
                                                                                                function () {
                                                                                                                $.getScript(scriptbase + "SP.Taxonomy.js", LoadData);
                                                                                                }
                                                                                                );}
                     );
                  }
             );

The Request Executor library(SP.RequestExecutor.js) enables cross-domain client-side calls, and ‘SP.Taxonomy.js’ is required to interact with managed metadata services. Once both are loaded you are ready to go to make calls for Taxonomies.

Load the Groups:

Now let’s see how to interact with taxonomy. You first needs to load the Taxonomy Session -> then get the term stores with this session -> then get the particular term store -> the load the group collection of term store. Following is the complete snippet to get the groups of term store.

function LoadData () {   
       context = new SP.ClientContext(appweburl);
        var factory =
            new SP.ProxyWebRequestExecutorFactory(
                appweburl
            );
        context.set_webRequestExecutorFactory(factory);
        appContextSite = new SP.AppContextSite(context, hostweburl);
        web = appContextSite.get_web();
        context.load(web);                
        taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
        termStores = taxSession.get_termStores();
        termStore = termStores.getById(termStoreID);
        context.load(termStore);
        termGroups = termStore.get_groups();
       context.load(termGroups);
        context.executeQueryAsync(onSuccess, onFail);
}
function onSuccess() {
        var termGroupEnumerator = termGroups.getEnumerator();
        groupsArr = new Array();
        //iterate through the enum and collect data in array - all groups of term store.
        while (termGroupEnumerator.moveNext()) {
                var group = termGroupEnumerator.get_current();                                         
                alert(“Label: ” + group.get_name() + "id: " + group.get_id());
                }
}
function onFail(sender, args) {
         alert( ‘Error’);
}

In this way you can get the groups of the term store, also you can get basic properties of the group.

Load the Term Sets

Once group is retrieved you can get the all term sets under the group. To get all term sets use following code snippet:

         taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
         termStores = taxSession.get_termStores();
         termStore = termStores.getById(termStoreID);
         termGroups = termStore.get_groups();
         var termGroup = termGroups.getById(id);
         termSets = termGroup.get_termSets();
         context.load(termSets);

        context.executeQueryAsync(function () {
                                                var termSetsEnumerator = termSets.getEnumerator();
                                                termSetsArr = new Array();
                                                while (termSetsEnumerator.moveNext()) {
                                                var termSet = termSetsEnumerator.get_current();                                         
                                                alert("label: " + termSet.get_name() +" id: " + termSet.get_id());
                                                }
        }
        , onFail);

Load the Terms within Term Set

You can get the all terms of a particular Term Set using following code snippet:

       taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
       termStores = taxSession.get_termStores();
       termStore = termStores.getById(termStoreID);
       termSet = termStore.getTermSet(id);
       termSets = termSet.get_terms();
       context.load(termSets);
       context.executeQueryAsync(function () {
                                                var termSetsEnumerator = termSets.getEnumerator();
                                                termSetsArr = new Array();
                                                while (termSetsEnumerator.moveNext()) {
                                                var termSet = termSetsEnumerator.get_current();                                         
                                                alert("label: " + termSet.get_name() +" id: " + termSet.get_id());
                                                }
        }
        , onFail);

Load the Terms within Term

Following code snippet fetches terms within any term, this is confusing statement but I have this one for now.

      taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
      termStores = taxSession.get_termStores();
      termStore = termStores.getById(termStoreID);
      term = termStore.getTerm(id);
      termSets = term.get_terms();
      context.load(termSets);
      context.executeQueryAsync(function () {
                                                var termSetsEnumerator = termSets.getEnumerator();
                                                termSetsArr = new Array();
                                                while (termSetsEnumerator.moveNext()) {
                                                var termSet = termSetsEnumerator.get_current(); 
                                                alert("label: " + termSet.get_name() +" id: " + termSet.get_id());
                                                }
        }
        , onFail);


Get the Details of Group

Following code snippet helps to get the details of group:

taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = taxSession.get_termStores();
termStore = termStores.getById(termStoreID);
termObject = termStore.getGroup(id);                
context.load(termObject);
context.executeQueryAsync(function () {
                var properties = termObject.get_objectData().get_properties();
                alert(properties.Id);
                alert(properties.Name);
                alert(properties.Description);                                   
                alert (properties.CreatedDate);
                alert (properties.LastModifiedDate);
                alert(properties.IsSiteCollectionGroup);
             alert(properties.IsSystemGroup);
                }
                , onFail);


Get the Details of Term Sets

Following code snippet helps to get the details of Term Sets:

taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = taxSession.get_termStores();
termStore = termStores.getById(termStoreID);
termObject = termStore.getTermSet(id);                            
context.load(termObject);
context.executeQueryAsync(function () {
                                                var properties = termObject.get_objectData().get_properties();
                                                alert(properties.Id);
                                                alert(properties.Name);
                                                alert(properties.Description);                                   
                                                alert(properties.CreatedDate);
                                                alert(properties.LastModifiedDate);                                      
                                                alert(properties.IsAvailableForTagging);
                                                alert(properties.IsOpenForTermCreation);
                                                alert(properties.Owner);
                                                alert(properties.Contact);
}
, onFail);

Get the Details of Term

Following code snippet helps to get the details of Term:

taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = taxSession.get_termStores();
termStore = termStores.getById(termStoreID);
termObject = termStore.getTerm(id);                   
context.load(termObject);

context.executeQueryAsync(function () {
                                                var properties = termObject.get_objectData().get_properties();
                                                alert(properties.Name);
                                                alert(properties.Id);
                                                alert(properties.Description);
                                               
                                                alert(properties.CreatedDate);
                                                alert(properties.LastModifiedDate);                                      
                                                alert(properties.IsAvailableForTagging);
                                                alert(properties.IsDeprecated);
                                                alert(properties.IsKeyword);
                                                alert(properties.IsPinned);                         
                                                alert(properties.IsPinnedRoot);
                                                alert(properties.IsReused);
                                                alert(properties.IsRoot);
                                                alert(properties.IsSourceTerm);
                                                alert(properties.Owner);
                                                alert(properties.PathOfTerm);
                                                alert(properties.TermsCount);                                 
                                }
                                , onFail);

Delete Group

Following code snippet helps to delete the group :

taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = taxSession.get_termStores();
termStore = termStores.getById(termStoreID);
var groupTodelete = termStore.getGroup(id);
groupTodelete.deleteObject();
context.executeQueryAsync(function () {
                alert("Removed Successfuly.");
},
onFail);

Delete Term Set

Following code snippet helps to delete the Term Set :

taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = taxSession.get_termStores();
termStore = termStores.getById(termStoreID);
var termSetTodelete = termStore.getTermSet(id);
termSetTodelete.deleteObject();           
context.executeQueryAsync(function () {
                alert("Removed Successfuly.");
},
onFail);

Delete Term

Following code snippet helps to delete the Term:

taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = taxSession.get_termStores();
termStore = termStores.getById(termStoreID);
var termTodelete = termStore.getTerm(id);
termTodelete.deleteObject();
context.executeQueryAsync(function () {
                alert("Removed Successfuly.");
},
onFail);


Add New Term Set in Group

Following code snippet helps to add the group:

taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = taxSession.get_termStores();
termStore = termStores.getById(termStoreID);
var group = termStore.getGroup(id);
var newTermSet = group.createTermSet(“termName”, newGuid.toString(),1033);
context.load(newTermSet);
context.executeQueryAsync(function () {
                alert("Done Successfuly.");
},
onFail);

Add New Term in Term Set

Following code snippet helps to add the Term Set:

taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = taxSession.get_termStores();
termStore = termStores.getById(termStoreID);
var termSet = termStore.getTermSet(id);
var newTerm = termSet.createTerm(termName, 1033, newGuid.toString());
context.load(newTerm);
context.executeQueryAsync(function () {
                alert("Done Successfuly.");
},
onFail);
 

Add New Term under another Term

Following code snippet helps to add the Term:

taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
termStores = taxSession.get_termStores();
termStore = termStores.getById(termStoreID);
var parentTerm = termStore.getTerm(id);
var newCTerm = parentTerm.createTerm(termName, 1033, newGuid.toString());
context.load(newCTerm);
context.executeQueryAsync(function () {
                alert("Done Successfuly.");
},
onFail);


In this way you can perform those activities with the term store in SharePoint App using ECMA scripting.

0 comments:

Post a Comment