Monday, February 10, 2014

Programmatically provisioning Term Places, Conditions, and Handled Meta-data content in SharePoint 2013


In this publish I describe how to supply Term Places, Conditions, and Handled Meta-data content in SharePoint 2013 using C#.

This is for SharePoint 2013 complete believe in alternatives where they handled Meta-data Support is provisioned

The Conditions and Phrase Places are provisioned in the standard website selection term shop, but they could just as quickly be provisioned in another term shop with the appropriate task of rights.





For the reasons of this publish, I designed a site scoped function and supply all of the conditions in a function recipient. This is not intended to be a description of function devices so I don't protect a lot of the facts of how to create a function recipient.

Provisioning SharePoint 2013 Term Sets in Code


In order to supply phrase sets in rule, you first need to get a reference to the phrase shop. In the example below, I use a function recipient to supply the phrase set and associated conditions. The function is a website scoped function and I use the standard website selection phrase shop. When triggered on a website selection, it will run the rule below to supply the conditions.


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<span class="a66lzyl" id="a66lzyl_4">public class</span> MyFeatureReceiver : SPFeatureReceiver
{
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    
        //Get a reference to the current site
        var site = properties.Feature.Parent as SPSite;
 
        //Get the Default Site Collection Term Store
        var taxonomySession = new TaxonomySession(site);
        var termStore = taxonomySession.DefaultSiteCollectionTermStore;
        var termStoreGroup = termStore.GetSiteCollectionGroup(site);
 
        //Create Department Term Set with associate Terms
        var termSet = termStoreGroup.CreateTermSet("Department");
        termSet.CreateTerm("Finance", CultureInfo.CurrentCulture.LCID);
        termSet.CreateTerm("Accounting", CultureInfo.CurrentCulture.LCID);
        var humanResources = termSet.CreateTerm("<span class="a66lzyl" id="a66lzyl_2">Human Resources</span>", CultureInfo.CurrentCulture.LCID);
        humanResources.CreateTerm("Recruiting", CultureInfo.CurrentCulture.LCID);
        humanResources.CreateTerm("Benefits", CultureInfo.CurrentCulture.LCID);
        humanResources.CreateTerm("Compensation", CultureInfo.CurrentCulture.LCID);
        humanResources.CreateTerm("Payroll", CultureInfo.CurrentCulture.LCID);
        termSet.CreateTerm("Operations", CultureInfo.CurrentCulture.LCID);
 
        termStore.CommitAll();
    }
}



Observe that I allocate the Individual Sources phrase to a varying ("human Resources") because I need to make kid conditions. Once I make "human Resources", I can make kid conditions by using the Create Term method on the recently designed "human Resources" varying.


This is what the site collection term store looks like once the solution has been deployed and the feature has been activated:


 

Provisioning a Managed Metadata Site Column

In order to provision a managed metadata site column that uses the Human Resources term as an anchor, I just add a little code below "termStore.CommitAll();". Here is the full feature receiver code with the added managed metadata site column linked to the new Human Resources term.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class MyFeatureReceiver : SPFeatureReceiver
{
    public <span class="a66lzyl" id="a66lzyl_19">override</span> void FeatureActivated(SPFeatureReceiverProperties properties)
    
        //Get a reference to the current site
        var site = properties.Feature.<span class="a66lzyl" id="a66lzyl_10">Parent</span> as SPSite;
 
        //Get the Default Site Collection Term Store
        var taxonomySession = new TaxonomySession(site);
        var termStore = taxonomySession.DefaultSiteCollectionTermStore;
        var termStoreGroup = termStore.GetSiteCollectionGroup(site);
 
        //Create Department Term Set with associate Terms
        var termSet = termStoreGroup.CreateTermSet("Department");
        termSet.CreateTerm("<span class="a66lzyl" id="a66lzyl_9">Finance</span>", CultureInfo.CurrentCulture.LCID);
        termSet.CreateTerm("<span class="a66lzyl" id="a66lzyl_7">Accounting</span>", CultureInfo.CurrentCulture.LCID);
        var humanResources = termSet.CreateTerm("Human Resources", CultureInfo.CurrentCulture.LCID);
        humanResources.CreateTerm("Recruiting", CultureInfo.CurrentCulture.LCID);
        humanResources.CreateTerm("Benefits", CultureInfo.CurrentCulture.LCID);
        humanResources.CreateTerm("Compensation", CultureInfo.CurrentCulture.LCID);
        humanResources.CreateTerm("<span class="a66lzyl" id="a66lzyl_6">Payroll</span>", CultureInfo.CurrentCulture.LCID);
        termSet.CreateTerm("Operations", CultureInfo.CurrentCulture.LCID);
 
        termStore.CommitAll();
 
        var web = site.RootWeb;
        if (!web.Fields.ContainsFieldWithStaticName("HumanResourcesDepartment"))
        {
             var humanResourcesDepartment = web.Fields.CreateNewField("TaxonomyFieldType", "HumanResourcesDepartment") as TaxonomyField;
             if (humanResourcesDepartment != null)
             {
                 humanResourcesDepartment.StaticName = "HumanResourcesDepartment";
                 humanResourcesDepartment.Title = "Human Resources Department";
                 humanResourcesDepartment.SspId = termStore.Id;
                 humanResourcesDepartment.TermSetId = termSet.Id
                 humanResourcesDepartment.TargetTemplate = string.Empty;
                 humanResourcesDepartment.AnchorId = humanResources.Id;
                 humanResourcesDepartment.Group = "Custom Columns";
                 humanResourcesDepartment.AllowMultipleValues = true;
                 humanResourcesDepartment.Open = false;
             }
             web.Fields.Add(humanResourcesDepartment);
             web.Update();
        }
    }
}

Note that since I have the Human Resources term saved in the humanResources variable, I am able to grab it's id to set the anchor point for my managed metadata column. 

Here's what the site column looks like after the feature has been deployed and activated:







Observe how the Phrase Set Configurations are attached to the Individual Sources term in the Division Phrase Set. Here's how the qualities on the new handled meta-data area should be set:

The Sapid should be set to the id of the Phrase Shop to use. In this situation I occur to have only one Handled Meta-data Support and one Phrase Shop to use. In the rule above, I just get the id from my term Store varying.

The TermSetId should be set to the id of the Phrase Set to use. In this situation, my Phrase Set is "Department". In the rule above, I just get the id from my termSet vaiable..

The Anchored should be set to the id of the phrase that you would like to core on. In a lot of situations, this may be set to zero if you want to core on the Term Set itself. However, in this situation, I want to make a line that allows me to decide on a division within Individual Sources. So, I am going to core on the "Human Resources" Term. In the rule above, I just get the id from my human Resources varying.




No comments:

Post a Comment