Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
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 Nov 5 '13 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 Nov 5 '13 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 Nov 5 '13 at 21:57

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
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 Nov 5 '13 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 Nov 5 '13 at 23:35

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

1;#FirstName,,LastName,#Domain\\FirstName.LastName,#FirstName.LastName@Domain.com,#,#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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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