I am using a custom powershell script to update SharePoint Online user profile property (mobile number) with a custom value reading from a text file. but it is always throwing the error as

Exception calling "ExecuteQuery" with "0" argument(s): "Operation is not valid due to the current state of the object."

Below is the script:

# Load assemblies to PowerShell session - Will try to resolve tenant dll status automatically, if possible
$a = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Online.SharePoint.Client.Tenant")
if( !$a ){
    # Let's try to load that from default location.
    $defaultPath = "C:\Program Files\SharePoint Client Components\16.0\Assemblies\Microsoft.Online.SharePoint.Client.Tenant.dll"
    $a = [System.Reflection.Assembly]::LoadFile($defaultPath)

# Get needed information from end user
$adminUrl = 'https://test-admin.sharepoint.com'
$userName = '[email protected]'
$pwd = Read-Host -Prompt 'Enter your password' -AsSecureString
$importFileUrl = 'https://test.sharepoint.com/sites/demo/Docs/sample.txt'

# Get instances to the Office 365 tenant using CSOM
$uri = New-Object System.Uri -ArgumentList $adminUrl
$context = New-Object Microsoft.SharePoint.Client.ClientContext($uri)

$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $pwd)
$o365 = New-Object Microsoft.Online.SharePoint.TenantManagement.Office365Tenant($context)

# Type of user identifier ["Email", "CloudId", "PrincipalName"] in the User Profile Service

# Name of user identifier property in the JSON

# Create property mapping between on-premises name and O365 property name
# Notice that we have here 2 custom properties in UPA called 'City' and 'OfficeCode'
$propertyMap = New-Object -type 'System.Collections.Generic.Dictionary[String,String]'
$propertyMap.Add('Property1', 'CellPhone')    

# Call to queue UPA property import 
$workItemId = $o365.QueueImportProfileProperties($userIdType, $userLookupKey, $propertyMap, $importFileUrl);

# Execute the CSOM command for queuing the import job

# Output unique identifier of the job
Write-Host "Import job created with following identifier:" $workItemId.Value

I am referring the article's from the below and share your thoughts.

http://dev.office.com/blogs/introducing-bulk-upa-custom-profile-properties-update-api https://github.com/OfficeDev/PnP/blob/master/Samples/UserProfile.BatchUpdate.API/CreateImportJob.ps1

  • What kind of user you have? External users? Commented Apr 21, 2016 at 19:46
  • just AD windows users
    – kesava
    Commented Apr 22, 2016 at 0:27

1 Answer 1


after thorough analysis, i have found that the CSOM version in my tenant is not up to date with the script provided in dev.office.com.

the recommendation here would be that you should make sure the newly released snippets/solutions are compatible with you tenant before we actually jump in. Especially, if you tenant is not in first release mode for changes then there is 100% chance that it will not work.

I have tested in the dev office 365 tenant linked to my msdn licence which has first release changes settings applied and it worked fine with no issues.

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.