Hi,

I'm using PowerScript, PowerShell and SharePoint Online Management Shell (used with admin rights).
I'm trying to use that code to insert new enterprise keywords in my library Documents:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
cls
 
#Your SharwPoint online Credentials
$siteUrl = "<My site>"
$username = "<My username>"
[string]$pwd = "<My pwd>"
$password = $pwd | ConvertTo-SecureString -AsPlainText -Force
$listName = "Documents";
$taxonomyField = "Enterprise Keywords"
 
# Load the Client object Model Assemblies
 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Taxonomy")
 
function getContext()
{
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
    $context.Credentials = $credentials
 
    return $context;
}
$context = getContext;
 
# load the List
$mainList = $context.Web.Lists.GetByTitle($listName);
$context.Load($mainList);
$context.ExecuteQuery();
 
# load the Taxonomy Field
$TaxonomyKategorieFld = $mainList.Fields.GetByInternalNameOrTitle($taxonomyField)
$context.Load($TaxonomyKategorieFld)
$context.ExecuteQuery();
 
# Create the item object in the Taxonomy List
$itemCreationInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation;
$item = $mainList.AddItem($itemCreationInfo);
$item.set_item("Title","Test Taxonomy Item");
$item.Update();
 
# Create Taxonomy Session
$sTaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context);
$sTaxonomySession.UpdateCache();
$context.Load($sTaxonomySession);
$context.ExecuteQuery();
 
# Load Default Term Store (Site Settings --> Term Store Management)
$sTermStore = $sTaxonomySession.GetDefaultSiteCollectionTermStore();
$context.Load($sTermStore);
$context.ExecuteQuery();
 
 
if($sTermStore.IsOnline)
{
    $sGroup = $sTermStore.Groups.GetByName($sGroupName); # Get the Term Group
    $sTermSet = $sGroup.TermSets.GetByName($tsName); # Get the Term Set
    $context.Load($sTermSet);   
    $context.ExecuteQuery();
    $terms = $sTermSet.GetAllTerms(); # Load all the Terms in the TermSet
    $context.Load($terms);
    $context.ExecuteQuery();
 
    $txField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").
 
 
 
MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).    
    Invoke($context, $TaxonomyKategorieFld)   
    $termQuery = "Kunden";
    $termQuery1 = "Acme";
    $termValues = @();
    foreach($term in $terms)
    {
        # When Kunden or Aceme Terms found, add to the Taxonomy Field
        if(($term.Name -eq $termQuery) -or ($term.Name -eq $termQuery1))
        {
            $termValues +=  "-1;#" + $term.Name + "|" + $term.Id;
            $termValuesString = $termValues -join ";#"
        }   
    }   
 
    $termValues = new-object Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValueCollection($context, $termValuesString, $txField)
    $txField.SetFieldValueByValueCollection($item, $termValues);
    $item.Update();
    $context.Load($item);
    $context.ExecuteQuery();
}
 
$context = $null;
Read-Host "Press a key to exit..."
But I get the following error:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
Unable to find type [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession].
At C:\users\9206513E\Desktop\test.ps1:56 char 21
+ ... mySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::G ...
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : Invoke MethodOnNull
Do you have any idea why this datatype in particular does not exists?

Thanks a lot!

Jean