0

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
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$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 = 'firstname.lastname@hawaiianair.com'
$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)
$context.Load($o365)

# Type of user identifier ["Email", "CloudId", "PrincipalName"] in the User Profile Service
$userIdType=[Microsoft.Online.SharePoint.TenantManagement.ImportProfilePropertiesUserIdType]::Email

# Name of user identifier property in the JSON
$userLookupKey="IdName"

# 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
$context.ExecuteQuery();

# 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? – Aakash Maurya Apr 21 '16 at 19:46
  • just AD windows users – kesava Apr 22 '16 at 0:27
0

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, privacy policy and cookie policy

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