Here we copy all users from user profile to one site.
First, taken an object for your web in which you want to copy users.
You will need these references of assemblies.
using System;
using System.Windows.Forms;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;
using Microsoft.SharePoint;
and then writen down the following code. Just make sure that you have administrative rights to perform this operation.
After performing this operation, just wait for some time. This is because initially you may see users Account name as DomainName\UserName, However after some time, These user names will be converted to the Actual user names.(the one that we see after welcome {user name} on top right corner).
First, taken an object for your web in which you want to copy users.
You will need these references of assemblies.
using System;
using System.Windows.Forms;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;
using Microsoft.SharePoint;
and then writen down the following code. Just make sure that you have administrative rights to perform this operation.
SPSite objSite = new SPSite("{site URL}");
//Then obtain server context,
ServerContext svrContext = ServerContext.GetContext(objSite);
//Take User profile object
UserProfile myProfile = null;
UserProfileManager profileManager = new UserProfileManager(svrContext);
//Open the web.
SPWeb web = objSite.OpenWeb();
web.AllowUnsafeUpdates = true;
//Navigate through each user profile in profile manager
//and then add the users to the site with its login name.
foreach (UserProfile userprofile in profileManager)
{
if (profileManager.UserExists(userprofile.MultiloginAccounts[0]))
{
web.SiteUsers.Add(userprofile.MultiloginAccounts[0], "", "", "");
}
}
web.Update();
web.AllowUnsafeUpdates = false;
After performing this operation, just wait for some time. This is because initially you may see users Account name as DomainName\UserName, However after some time, These user names will be converted to the Actual user names.(the one that we see after welcome {user name} on top right corner).
No comments:
Post a Comment