2

I am writing an archival Feature for a SharePoint 2010 list using C#. I have it copying the list items and all version history over to the new site collection but the Modified By and Created By fields are out of whack because they are referring to users that do not exist in the new Site Collection. The Site Collections are on the same server but separate Web Applications and the users are AD based.

Is there a way that I can check if the user exists in the new Site Collection and if it does not to find the user in the source Site Collection and copy it over to the destination Site Collection?

The only other way that I can think of doing this is to make copy of the current Site Collection and restore it to the archival Site Collection. The problem that this brings up is that when new employees get added to the source Site Collection they will not be in the archival one and the problem exists again.

I have been Googling this topic to no avail so any assistance with this would be great!

3
  • 1
    Nice question @LukeP. Is User Profile Service Application configured and up an running? If not, is there a reason (aka policy) prohibiting you to use UPSA?
    – Benny Skogberg
    Commented Nov 5, 2013 at 21:35
  • @BennySkogberg, Thank you for the suggestion. It does appear that User Profile Services Application is up and running although I did not configure it myself. I'm working on a development server that I spun up via a backup/restore of the production site. I am unfamiliar with how the USPA works so I will do my due diligence and see if this fixes the problem!
    – LukeP
    Commented Nov 5, 2013 at 21:46
  • Good! Maybe you only need to add the groups or users to the root site collection your moving to? Check permissions in Site Settings > Users and Groups. I have confidence in that you will solve this :)
    – Benny Skogberg
    Commented Nov 5, 2013 at 21:57

2 Answers 2

2

You can use spweb.ensureuser

Ensure user checks whether the specified logon name belongs to a valid user of the website, and if the logon name does not already exist, adds it to the website.

MSDN Ensureuser

2
  • I believe that this is just what I was looking for. I just need to find a way to pull the proper "logon name" from the source field.
    – LukeP
    Commented Nov 5, 2013 at 22:34
  • For anyone that may find this thread in the future, please see my explanation below on how I used the EnsureUser method
    – LukeP
    Commented Nov 5, 2013 at 23:35
0

The SPListItem[Field] outputs the following string format when it is a Member/Group type:

1;#FirstName,,LastName,#Domain\\FirstName.LastName,#[email protected],#,#LastName,, FirstName

Due to the fact that EnsureUser needs a "Logon Name" I am using String.Split to pull the 3rd string segment from the string using '#' as the separator and then passing that into EnsureUser. It looks like this:

var sourceString = (string)version[sourceField.Title];
var sourceUserLogonName = sourceString.Split('#')[2].Replace(",","");
var destinationUser = destinationWeb.EnsureUser(sourceUserLogonName);
newItem[sourceField.Title] = destinationUser;

A little FYI on why I need to do it this way -- In order to duplicate the List Items with all version history I am iterating through each version of the source item and each of the fields to verify that the fields exist on the destination list in case the source list gets a column added and it is not added in the destination "archival" list.

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.