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 need to copy People and group from one sharepoint site to another. I dont want to backup the entire site.

Any pointers would be appreciated.

Thanks, Lav

share|improve this question
add comment

1 Answer

I just did something very similar to this, should be plenty of pointers. Code below. You are welcome.

My code is running as a timer job and synchronizes user groups across several site collection sharing a managed path in a web application. The code uses the following variables: _webApplicationToSynch is the absolute url to the web application. _managedPaths is the csv of managed paths to synchronize. _groupsToSynch is the csv of user groups to synchronize.

using (var rootSiteCol = new SPSite(_webApplicationToSynch))
using (var rootWeb = rootSiteCol.OpenWeb(rootSiteCol.RootWeb.ID))
{
    var rootGroups = new List<SPGroup>();
    foreach (string groupName in _groupsToSynch)
    {
        if (!String.IsNullOrEmpty(groupName))
        {
            var group = rootWeb.SiteGroups[groupName];
            if (group != null && group.Users.Count > 0) rootGroups.Add(group);
        }
    }
    for (int siteCounter = 0; siteCounter < rootSiteCol.WebApplication.Sites.Count; siteCounter++)
    {
        using (var leafSiteCol = rootSiteCol.WebApplication.Sites[siteCounter])
        using (var leafWeb = leafSiteCol.OpenWeb(leafSiteCol.RootWeb.ID))
        {
            //Check if the sitecollection matches one or more valid managed paths
            var manPathMatches =
                _managedPaths.Where<string>(
                    manPath => leafSiteCol.Url.StartsWith(_webApplicationToSynch.TrimEnd('/') + "/" + manPath + "/"));

            if (manPathMatches.Count() > 0) continue; //Do not synch if man.path. is not "valid", i.e. inputted

            //Synch groups
            foreach (SPGroup rootGroup in rootGroups)
            {
                var leafGroup = leafWeb.SiteGroups[rootGroup.Name];
                foreach (SPUser rootGroupUser in rootGroup.Users)
                {
                    //A user exists in the rootgroup, but not in the leafgroup. Add to leafgroup.
                    if (leafGroup.Users.GetByEmail(rootGroupUser.Email) == null)
                    {
                        leafWeb.EnsureUser(rootGroupUser.LoginName);
                        leafGroup.AddUser(rootGroupUser);
                    }
                }
                //can't directly remove users because we're modifying the collection we're iterating over
                var usersToRemove = new List<SPUser>(); 
                foreach (SPUser leafGroupUser in leafGroup.Users)
                {
                    //A user exists in the leafgroup, but not in the rootgroup. Remove from leafgroup.
                    if (rootGroup.Users.GetByEmail(leafGroupUser.Email) == null)
                    {
                        usersToRemove.Add(leafGroupUser);
                    }
                }
                //Finally remove all "dead" users
                foreach (SPUser ghost in usersToRemove)
                {
                    leafGroup.RemoveUser(ghost);
                }
            }
        }
    }
    rootWeb.Update();
}
share|improve this answer
add comment

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.