In my portal I have a list with a "person" field that contains usernames.
I am using a powershell script to update this field.
You can see my function below. I've already loaded the list, and am cycling through each item. This works fine if I also create a user in the same loop - that is, the function works just fine.
The problem is that if the account already exists in AD that is, it isn't new - the function confirms that the account is valid, but the list never seems to get updated, even though is says it is.
function updateSPlistUsername($whatweb, $whatitem, $whatusername){
try{
#update the username in the list
[Microsoft.SharePoint.SPUser]$thisusername = $whatweb.EnsureUser($whatusername) #double-check to see that username exists
if($thisusername){
write-host "Username: $thisusername is confirmed."
$whatitem["Username"] = $thisusername
$whatitem.Update()
write-host "Sharepoint List at $portal_list has been updated."
}else{
write-host "ERROR: Could not aquire a username with which to update the list."
}
}catch{
write-host "ERROR: Could not update portal list -" $_ -ForegroundColor red
}
}
Again, if I create a new account, the list gets updated. It's only when I try to add an existing valid username to the list that it fails to update. I've even manually run the sp timer jobs in hopes something gets pushed through.
What am I missing here?
[update]
Ah, okay, so I think it has something to do with Sharepoint picking up on AD changes. I manually created a new user in AD and the script didn't update that username to the list either. Then, after a few minutes, it picked up on the one I was having trouble with before. That suggests to me that before the powershell script works properly, either some sharepoint sync job needs to happen, or I put something else in my script.
[Edit]
This really escapes me. How is it that an account can exist in AD, but I can't use a script to add it to a sharepoint list? I can add the username manually just fine.
Here's another test. If I delete a username from the sharepoint list, and then run the script again, the script says that the username is still there!