In one of my previous posts I showed how to provision managed metadata fields via sandbox solution and how to fix issues related with it: see Fix managed metadata fields after updating sandbox solution using client object model in Sharepoint. In this post I will show how to provision them via client object model. This method also can be used in Sharepoint Online and doesn’t require installations of any wsps.
First of all we need to declare taxonomy field itself and related note field:
1: <Field ID="{CA0AA6FD-EB29-4E99-86FD-49CD88F1E5BA}"
2: Type="Note"
3: Name="MyFieldTaxHTField"
4: StaticName="MyFieldTaxHTField"
5: Required="FALSE"
6: DisplayName="MyFieldTaxHTField"
7: ShowInViewForms="FALSE"
8: Hidden="TRUE"
9: CanToggleHidden="TRUE" />
10:
11: <Field ID="{E5874300-A621-4EFD-BC6F-B6E521B40311}"
12: Type="TaxonomyFieldType"
13: Name="MyField"
14: StaticName="MyField"
15: ShowField="Term1035"
16: Required="FALSE"
17: Group="Custom"
18: DisplayName="My Field">
19: <Default></Default>
20: <Customization>
21: <ArrayOfProperty>
22: <Property>
23: <Name>SspId</Name>
24: <Value xmlns:q1="http://www.w3.org/2001/XMLSchema" p4:type="q1:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">{f3ae3865-e5a0-4173-add8-2190e865ec47}</Value>
25: </Property>
26: <Property>
27: <Name>GroupId</Name>
28: </Property>
29: <Property>
30: <Name>TermSetId</Name>
31: <Value xmlns:q2="http://www.w3.org/2001/XMLSchema" p4:type="q2:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">{6F186952-8CAC-4099-B0E4-CCE3BAA3177F}</Value>
32: </Property>
33: <Property>
34: <Name>AnchorId</Name>
35: <Value xmlns:q3="http://www.w3.org/2001/XMLSchema" p4:type="q3:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">00000000-0000-0000-0000-000000000000</Value>
36: </Property>
37: <Property>
38: <Name>UserCreated</Name>
39: <Value xmlns:q4="http://www.w3.org/2001/XMLSchema" p4:type="q4:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
40: </Property>
41: <Property>
42: <Name>Open</Name>
43: <Value xmlns:q5="http://www.w3.org/2001/XMLSchema" p4:type="q5:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
44: </Property>
45: <Property>
46: <Name>TextField</Name>
47: <Value xmlns:q6="http://www.w3.org/2001/XMLSchema" p4:type="q6:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">{CA0AA6FD-EB29-4E99-86FD-49CD88F1E5BA}</Value>
48: </Property>
49: <Property>
50: <Name>IsPathRendered</Name>
51: <Value xmlns:q7="http://www.w3.org/2001/XMLSchema" p4:type="q7:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
52: </Property>
53: <Property>
54: <Name>IsKeyword</Name>
55: <Value xmlns:q8="http://www.w3.org/2001/XMLSchema" p4:type="q8:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
56: </Property>
57: <Property>
58: <Name>TargetTemplate</Name>
59: </Property>
60: <Property>
61: <Name>CreateValuesInEditForm</Name>
62: <Value xmlns:q9="http://www.w3.org/2001/XMLSchema" p4:type="q9:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">false</Value>
63: </Property>
64: <Property>
65: <Name>FilterAssemblyStrongName</Name>
66: <Value xmlns:q10="http://www.w3.org/2001/XMLSchema" p4:type="q10:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">Microsoft.SharePoint.Taxonomy, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Value>
67: </Property>
68: <Property>
69: <Name>FilterClassName</Name>
70: <Value xmlns:q11="http://www.w3.org/2001/XMLSchema" p4:type="q11:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">Microsoft.SharePoint.Taxonomy.TaxonomyField</Value>
71: </Property>
72: <Property>
73: <Name>FilterMethodName</Name>
74: <Value xmlns:q12="http://www.w3.org/2001/XMLSchema" p4:type="q12:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">GetFilteringHtml</Value>
75: </Property>
76: <Property>
77: <Name>FilterJavascriptProperty</Name>
78: <Value xmlns:q13="http://www.w3.org/2001/XMLSchema" p4:type="q13:string" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">FilteringJavascript</Value>
79: </Property>
80: </ArrayOfProperty>
81: </Customization>
82: </Field>
The following properties are most important:
- SspId – term store id. You may get it in Site settings > Term store management and select top level node in the left tree view;
- TermSetID – id of term set which current field will be bound to. You may also get it from Site settings > Term store management;
- TextField – should contain id of related note field (declared above).
Having this xml definition we may create fields using the following client object model code:
1: string fieldAsXml = "";
2:
3: var clientContext = new ClientContext("http://example.com");
4: var secure = new SecureString();
5: foreach (char c in "password")
6: {
7: secure.AppendChar(c);
8: }
9: var credentials = new SharePointOnlineCredentials("userame", secure);
10: clientContext.Credentials = credentials;
11:
12: var web = clientContext.Web;
13: var fields = web.Fields;
14: clientContext.Load(web, w => w.Fields);
15: clientContext.Load(fields);
16: clientContext.ExecuteQuery();
17: fields.AddFieldAsXml(fieldAsXml, false, AddFieldOptions.AddFieldInternalNameHint);
18: web.Update();
19: clientContext.ExecuteQuery();
After that field will be available in site columns.
No comments:
Post a Comment