0

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!

1 Answer 1

2

When updating usernames, I preprend the username with a "-1;#" and that seems to work - This indicates to me, the format of the username column is expecting an ID but by putting in the -1, it seems to be happy with that and puts it in properly. YMMV

<Field Name="PeopleColumn">-1;#MyAccname</Field>
3
  • Really? That -1;# is what you get when you fetch a value from a lookup column.
    – bgmCoder
    Aug 14, 2013 at 14:18
  • Also, the function works fine if I use the script to update an entry with an account I just created - and that doesn't insert the `-1;#' part. I'll try it and say what happens.
    – bgmCoder
    Aug 14, 2013 at 14:20
  • I believe the ;# is a delimiter.
    – bgmCoder
    Sep 9, 2014 at 16:02

Your Answer

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

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