Forum Discussion
Error while connecting to SharePoint online tenant through powershell
I am using below code to loop through all site collections in my tenant, search for a specific keyword and export the search results to csv.
cls
$host.Runspace.ThreadOptions = "ReuseThread"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Search") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Online.SharePoint.Client.Tenant") | Out-Null
$TimeStamp = (Get-Date).ToString("s").Replace(":","-")
$sSiteUrl = "https://site-admin.sharepoint.com/"
$sUserName = "username"
$sPassword=convertto-securestring "password" -asplaintext -force
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName,$sPassword)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)
$Context.Credentials = $Creds
$spoTenant= New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($Context)
$spoTenantSiteCollections=$spoTenant.GetSiteProperties(0,$true)
$Context.Load($spoTenantSiteCollections)
$Context.ExecuteQuery()
$exportlocation = "D:\SearchCount_$TimeStamp.csv"
$outputline="Title" +";"+ "Path" +";"+ "ViewsLifeTime" +";"+ "ViewsLifeTimeUniqueUsers" + ";"+ "CreatedBy" + ";"+ "Created" + ";"+"ModifiedBy" + ";"+ "LastModifiedTime"+";"+"ContentType"+";"+"contentclass"
Add-Content $exportlocation $outputline
foreach($spoSiteCollection in $spoTenantSiteCollections) {
$Context1 = New-Object Microsoft.SharePoint.Client.ClientContext($spoSiteCollection.Url)
$Context1.Credentials = $Creds
$keywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($Context1)
$queryText = "SharePoint"
$keywordQuery.QueryText = $queryText
$keywordQuery.RowLimit=9999
$keywordQuery.TrimDuplicates=$false
$keywordQuery.SelectProperties.Add("LastModifiedTime")
$keywordQuery.SelectProperties.Add("ViewsLifeTime")
$keywordQuery.SelectProperties.Add("ModifiedBy")
$keywordQuery.SelectProperties.Add("ViewsLifeTimeUniqueUsers")
$keywordQuery.SelectProperties.Add("Created")
$keywordQuery.SelectProperties.Add("CreatedBy")
$keywordQuery.SelectProperties.Add("ContentType")
$keywordQuery.SelectProperties.Add("contentclass")
$keywordQuery.SortList.Add("ViewsLifeTime","Asc")
$searchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($Context)
$results = $searchExecutor.ExecuteQuery($keywordQuery)
$Context.ExecuteQuery()
Write-Host $results.Value[0].ResultRows.Count
foreach($result in $results.Value[0].ResultRows) {
$outputline='"'+$result["Title"]+'"'+";"+'"'+$result["Path"]+'"'+";"+$result["ViewsLifeTime"]+";"+$result["ViewsLifeTimeUniqueUsers"]+";"+$result["CreatedBy"]+";"+$result["Created"]+";"+$result["ModifiedBy"]+";"+$result["LastModifiedTime"]+";"+$result["ContentType"]+";"+$result["contentclass"]
Add-Content $exportlocation $outputline
}
}
I get below error with this code
New-Object : Cannot find an overload for "Tenant" and the argument count: "1".
At line:14 char:13
+ $spoTenant= New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
You cannot call a method on a null-valued expression.
At line:15 char:1
+ $spoTenantSiteCollections=$spoTenant.GetSiteProperties(0,$true)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Cannot find an overload for "Load" and the argument count: "1".
At line:16 char:1
+ $Context.Load($spoTenantSiteCollections)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Can somebody please help me with this error ? I really need to make this code working asap.
2 Replies
- Manidurai MohanamariappanIron Contributor
It seems you are using old Microsoft.Online.SharePoint.Client.Tenant.dll, so you can get the new dll from new Sharepoint online PowerShell module.
https://www.microsoft.com/en-in/download/details.aspx?id=35588
After installation, you need to change assembly path in your script like
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"Note: Above script, I added my default installation path and you can find the SharePoint module installation path using Get-Module -ListAvailable
- paulpaschaBronze Contributor
The error message you get tells you're passing the wrong arguments to the Tenant Class's constructor. I don't have a quick fix in the code you've listed but I do suggest you to have a look at the available commands in the PnP-PowerShell module. I think this will make your script a lot easier to create and maintain.
For example, the command documented in the article below will list all site collections for you:
https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnptenantsite?view=sharepoint-ps
Hope this helps!