Forum Discussion

Dhi_V1800's avatar
Dhi_V1800
Copper Contributor
Mar 28, 2022

SharePoint online CSOM authentication fails in prod works in dev and test tenants

Hi,

 

I tried the below csom script to undeclare all the existing records in a sharepoint online library. It works fine in dev and test tenants but fails in the production tenant. Any suggestions to fix this issue ?.

 

I tried another option to use pnp instead of csom, it works fine to connect to the site & iterate the library documents. But i dont see any option to undeclare the record documents using pnp powershell and to get the client run time context using pnp.

 

Thanks in advance for your help on this issue.

 

$listname = 'ListName'
$Username = 'Email address removed'
$password = Read-Host -Prompt "Enter password" -AsSecureString

$url = 'https://abcsharepoint.com/sites/test'
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
[Microsoft.SharePOint.Client.ClientRuntimeContext] $runTimectx = [Microsoft.SharePOint.Client.ClientRuntimeContext] $ctx.Web.Context;
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
$web = $ctx.Web;
$ctx.Load($web);
$ctx.Load($web.Webs);
$ctx.ExecuteQuery();

$listUpdate = $web.Lists.GetByTitle($listname)
$ctx.Load($listUpdate)
$ctx.ExecuteQuery()

#CAML Query to get all items inclusing sub-folders
$spQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$spQuery.ViewXml = "<View Scope='RecursiveAll' />";
$item = $listUpdate.GetItems($spQuery)
$ctx.Load($item)
$ctx.ExecuteQuery()

for($j=0; $j -lt $item.Count; $j++)
{
if($item[$j].FieldValues["_vti_ItemHoldRecordStatus"] -eq 273)
{
[Microsoft.SharePoint.Client.RecordsRepository.Records]::UndeclareItemAsRecord($runTimectx,$item[$j])
}
}

 

Error :

Exception calling "ExecuteQuery" with "0" argument(s): "The sign-in name or password does not match one in the Microsoft account system."

At line:12 char:9

+         $ctx.ExecuteQuery()

+         ~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : IdcrlException

3 Replies

    • Dhi_V1800's avatar
      Dhi_V1800
      Copper Contributor

      Leavii 

       

      As i said in my initial post i already tried this option and it is working to connect using pnp & to iterate the document.

       

      But it doesnt have an option to undeclare record using pnp, also to use the csom code i need to client run time context. so stuck there.

       

      Thanks

      • Kevin_Morgan's avatar
        Kevin_Morgan
        Iron Contributor

        Dhi_V1800 

         

        We can use the OfficeDevPnP.Core assembly in PowerShell to create a SharePointContext object with different authentication types (ex: MFA). You can refer to this post : https://morgantechspace.com/2021/09/connect-to-sharepoint-site-with-mfa-account-using-csom-and-powershell.html

         

        Try the below commands.

         

        #Add required references to OfficeDevPnP.Core and SharePoint client assembly
        [System.Reflection.Assembly]::LoadFrom("C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\3.29.2101.0\OfficeDevPnP.Core.dll")
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
        
        $siteURL = "https://contoso.sharepoint.com/sites/siten_name"
        
        $AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
        $ctx = $AuthenticationManager.GetWebLoginClientContext($siteURL)
        $ctx.Load($ctx.Web)
        $ctx.ExecuteQuery()
        
        Write-Host "Title: " $ctx.Web.Title -ForegroundColor Green
        Write-Host "Description: " $ctx.Web.Description -ForegroundColor Green

         

Resources