Question

Which is the best way to copy item with person column to another site ? Situation: I have source site A and destination site B with the same structured custom list. I have to copy item from A to B. Problem is with field "Boss" - it's person field type. A and B are on the same SP farm, synchronized with the same AD database.

code (simplified, sourceItem is current processing item from A and destItem is newly created item in B):

string sBoss = sourceItem["Boss"].ToString();
SPFieldUserValue user = new SPFieldUserValue(sourceItem.Web, sBoss);
SPUser userdest =   contextdestination.web.EnsureUser(user.User.LoginName);
destItem["Boss"] = userdest;

Everything goes ok, till I am iterating item with Boss value, who is no longer valid in AD. On destItem the value of Bossfield is wrong. On sourceItem field on all views is displayed correctly and have right url to person. No error is fired. Is there some way to handle it , or is my solution totally wrong and must be done other way ?

Was it helpful?

Solution

Does the "Boss" have access to that specific site? I think the EnsureUser method goes in this order:

  • Checks if user has access to site
  • If user does not it validates that the user, then gives access

EnsureUser does validate against AD, so if you really want this to work, I would suggest to enable his/her account, run this command, then disable it again.

EDIT: Alternatively, if the boss is present in the User Information List, I think you may be able to pull it off in this way:

SPFieldUserValue bossFUV = new SPFieldUserValue(destweb, bossID, bossLoginName);

//where bossID = the userID pulled from the User Information List
//where bossLoginName = the LoginName pulled from the User Information List
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top