Pages

Sunday, January 24, 2016

Migrate Users Across Site Collections in SharePoint Programatically

When you are working with SharePoint site collections you have experienced with managing contents in site collections from another site collection. (access root site contents from a sub site within the same web application). In this case when you need to copy user values from one site to another, then you need to ensure the user between two sites.

Using "EnsureUser"method:
SPUser currentWebUser = SPContext.Current.Web.EnsureUser(User.LoginName);

The EnsureUser method returns a valid SPUser object which can be use within site..

Ex: Add to a UserGroup:
SPGroup group = web.Groups[GroupName];
group.AddUser(currentWebUser);

No comments:

Post a Comment