0

I am trying to develop a CSOM console app for a SharePoint 2019 site and I'm having trouble authenticating.

I've tried:

context.Credentials = new NetworkCredential("username", "password", "domain");

and

ICredentials credentials = CredentialCache.DefaultCredentials;
context.Credentials = credentials;

(CredentialCache.DefaultCredentials had empty strings for Domain, Password, and UserName)

I've also tried

var credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(username, securePassword);

after having created the secure password.

Any other suggestions?

2
  • I've used both NetworkCredential and CredentialCache.DefaultCredentials successfully in many programs in the past. Are you on the same network as the SharePoint server(s)? Not being on the same network is the only thing I can think of that may cause problems. Sep 27, 2023 at 18:19
  • I'm on a virtual desktop in the same domain as the server, I can connect with the server using PnP Powershell
    – Rogerb
    Sep 28, 2023 at 7:01

2 Answers 2

0

Be sure to include SharePoint client DLL before, for example:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

Or with a direct path:

Add-Type -Path "E:\Apps\CSOM\Microsoft.SharePoint.Client.dll"
Add-Type -Path "E:\Apps\CSOM\Microsoft.SharePoint.Client.Runtime.dll"

And then you can try :

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$credentials = New-Object System.Net.NetworkCredential($Login, $Password, $Domain)
$ctx.Credentials = $credentials

I tested it with SharePoint 2019.

$rootWebsite = $ctx.Web
$ctx.Load($rootWebsite) 
$ctx.ExecuteQuery() 
2
  • This looks like PowerShell which I might have to consider using. I am changing permissions on list items which is why I decided to use a console application instead
    – Rogerb
    Sep 28, 2023 at 7:02
  • Oh sorry, I didn't answer with C# code but the syntax is almost the same! Maybe you could try in PowerShell to check if the error is the same or not.
    – jleture
    Sep 28, 2023 at 16:05
0

I've used PnP PowerShell and it was a piece of cake.

Connect-PnPOnline -Url "<site url>" -CurrentCredential    

$list = "account"
$startId = 0
$endId = 5000

for($i = $startId; $i -le $endId; $i++){

    $item = Get-PnPListItem -List $list -Id $i

    if($item["DocumentType"] -eq "Protection"){    
        if(-not(Get-PnPProperty -ClientObject $item -Property "HasUniqueRoleAssignments")){    
            Set-PnPListItemPermission -List $list -Identity $i -AddRole "Full Control" -Group "CRM Owners" -ClearExisting
            Set-PnPListItemPermission -List $list -Identity $i -AddRole "Contribute" -Group "CRM Protection Members"
        }
    }
}

I think some network setting was blocking the communication protocol my console app was using

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.