none
Sharepoint Online Powershell command ExecuteQuery doesn't run in the server RRS feed

  • Question

  • I have a script to update the custom User profile property for all Active directory groups using SharePoint PowerShell commands for the SharePoint Online tenancy.

    The query fails to execute getproperties method on the People Manager object and always returns null. Because of this the ExceuteQuery method also fails.

    The query runs fine in the local developer machine.

    Please let me know what configuration is required in the server machine to run the above script. Server is already configured with latest client SDK and SharePoint modules.

    partial Code snippet below:

    #Loop through AD group

     

    Get-ADGroupMember$ADGroup|ForEach-Object{

               

    $AccountName="i:0#.f|membership|"+$_."samAccountName"+"@xxx.xxxxxxxxxxxxxx.net"

       

    $userprofile=$people.GetPropertiesFor($AccountName)

       

    $context.Load($userprofile)

       

    $context.ExecuteQuery()

          

    If($userprofile.Email -ne$null)

           {

           

    $people.SetSingleValueProfileProperty($userprofile.AccountName,"Customprop",$CustProp)

           

    $context.ExecuteQuery()

           

    $userprofile.AccountName

           } 

        } 

    Error description:

    Exception calling "ExecuteQuery" with "0" argument(s): "The underlying connection was closed: An unexpected error occurred on a receive."

    Friday, April 21, 2017 11:14 AM

All replies

  • Hi,

    If you want to update the user profile properties in SharePoint Online, the following PowerShell script for your reference:

    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"  
    
    #Mysite URL
    $site = 'https://mysite-admin.sharepoint.com/' # This needs to be the "admin" site.
     
    #Admin User Principal Name
    $admin = 'lewis@mysite.onmicrosoft.com'
     
    #Get Password as secure String
    $password = Read-Host 'Enter Password' -AsSecureString
     
    #Get the Client Context and Bind the Site Collection
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
     
    #Authenticate
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password)
    $context.Credentials = $credentials
     
    #Fetch the users in Site Collection
    $users = $context.Web.SiteUsers
    $context.Load($users)
    $context.ExecuteQuery()
     
    #Create an Object [People Manager] to retrieve profile information
    $people = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($context)
     
    ForEach($user in $users)
    {
    	$userprofile = $people.GetPropertiesFor($user.LoginName)
    	$context.Load($userprofile)
    	$context.ExecuteQuery()
    	 
    	If($userprofile.Email -ne $null) { # do something a little more intelligent here.
    		$people.SetSingleValueProfileProperty($userprofile.AccountName, "AboutMe", "Updated by CSOM")
    		$context.ExecuteQuery()
    	}
    }
    

    More information:

    Script to Write and Get User Profile Properties in SharePoint Online with CSOM

    https://gallery.technet.microsoft.com/office/Script-to-Write-and-Get-d9a762bb

    Update User Profile Properties On SharePoint Online Using CSOM PowerShell

    http://www.c-sharpcorner.com/article/update-user-profile-properties-from-sharepoint-using-csom-po/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Sunday, April 23, 2017 2:52 AM
    Moderator
  • Thanks Dennis for looking into it. The problem might be with the system server itself as I am able to execute the same code in developer machine with no problem.

    If you see the error description below it could be something in the network or operating system level that is not allowing to execute the script successfully. If you know any configuration that needs to be checked please let me know. I can fetch the user names from AD group as well able to connect to SharePoint tenant site but it fails when it tries to execute the code for each object in the loop. Also the people manager object returns null for the $userprofile=$people.GetPropertiesFor($AccountName) method even though user exists.

    Error description:

    Exception calling "ExecuteQuery" with "0" argument(s): "The underlying connection was closed: An unexpected error occurred on a receive."

    Wednesday, May 3, 2017 3:07 PM
  • Hi,

    Please make sure you have installed the SharePoint Online Client Components SDK in you server.

    https://www.microsoft.com/en-us/download/details.aspx?id=42038

    Or try to open Windows PowerShell run as administrator.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, May 4, 2017 2:31 AM
    Moderator