Share via


SharePoint : User Management - SPS

Here you go for the sample code snippets for user management in SPS

- //Add users to the portal area or portal site level

                                    TopologyManager tm = new TopologyManager();

                                    PortalSite ps = tm.PortalSites[ new Uri("https://karthickmain:9092") ];

                                    Microsoft.SharePoint.Portal.PortalContext ctx = Microsoft.SharePoint.Portal.PortalApplication.GetContext(ps);

                                    Guid NewsGuid = AreaManager.GetSystemAreaGuid(ctx,SystemArea.News);

                                    PermissionCollection pc = SecurityManager.ManageAreaSecurity(ctx,NewsGuid);

                                    pc.AddUser("fareast\\ramkathi","ramkarthi@microsoft.com","Ram","",PortalRight.AddListItems);

                                    MessageBox.Show("User Added");

- //Remove user from portal Area

                                    TopologyManager tm = new TopologyManager();

                                    PortalSite ps = tm.PortalSites[ new Uri("https://karthickmain:9092") ];

                                    Microsoft.SharePoint.Portal.PortalContext ctx = Microsoft.SharePoint.Portal.PortalApplication.GetContext(ps);

                                    Guid NewsGuid = AreaManager.GetSystemAreaGuid(ctx,SystemArea.News);

                                    PermissionCollection pc = SecurityManager.ManageAreaSecurity(ctx,NewsGuid);

                                    Area news = AreaManager.GetArea(ctx, AreaManager.GetSystemAreaGuid(ctx, SystemArea.News));

                                    SPUser user = news.Web.SiteUsers["fareast\\ramkathi"];

                                    pc.Remove(user);

                                    MessageBox.Show("User Removed");

- //Add a user to the portal area level at site group

                                    TopologyManager tm = new TopologyManager();

                                    PortalSite ps = tm.PortalSites[ new Uri("https://karthickmain:9092") ];

                                    Microsoft.SharePoint.Portal.PortalContext ctx = Microsoft.SharePoint.Portal.PortalApplication.GetContext(ps);

                                    Guid homeGuid = AreaManager.GetSystemAreaGuid(ctx,SystemArea.Home);

                                    Area home = AreaManager.GetArea(ctx, homeGuid);

                                    PermissionCollection spc = SecurityManager.ManageSiteSecurity(ctx);

                                    SPRole reader = home.Web.Roles["Reader"];

                                    //reader.AddUser("domain_name\\alias","email_address","user_name","display_name");

                                    reader.AddUser("fareast\\pavank", "pavank@microsoft.com", "Pavan", "Reader");

                                    MessageBox.Show("User Added");

- //Remove a user from the portal area level at site group

                                    TopologyManager tm = new TopologyManager();

                                    PortalSite ps = tm.PortalSites[ new Uri("https://karthickmain:9092") ];

                                    Microsoft.SharePoint.Portal.PortalContext ctx = Microsoft.SharePoint.Portal.PortalApplication.GetContext(ps);

                                    Guid homeGuid = AreaManager.GetSystemAreaGuid(ctx,SystemArea.Home);

                                    Area home = AreaManager.GetArea(ctx, homeGuid);

                                    PermissionCollection spc = SecurityManager.ManageSiteSecurity(ctx);

                                    SPRole reader = home.Web.Roles["Reader"];

                                    //reader.AddUser("domain_name\\alias","email_address","user_name","display_name");

                                    reader.RemoveUser(home.Web.Users["fareast\\pavank"]);

                                    MessageBox.Show("User removed");

- //Add role to the portal area

                                    TopologyManager tm = new TopologyManager();

                                    PortalSite ps = tm.PortalSites[ new Uri("https://karthickmain:9092") ];

                                    Microsoft.SharePoint.Portal.PortalContext ctx = Microsoft.SharePoint.Portal.PortalApplication.GetContext(ps);

                                    SecurityManager.AddRole(ctx,"Writer","",PortalRight.AddListItems);

                                    MessageBox.Show("Role Added");

- //Remove role from the portal area

                                    TopologyManager tm = new TopologyManager();

                                    PortalSite ps = tm.PortalSites[ new Uri("https://karthickmain:9092") ];

                                    Microsoft.SharePoint.Portal.PortalContext ctx = Microsoft.SharePoint.Portal.PortalApplication.GetContext(ps);

                                    Guid homeGuid = AreaManager.GetSystemAreaGuid(ctx,SystemArea.Home);

                                    Area home = AreaManager.GetArea(ctx, homeGuid);

                                    home.Web.Roles.Remove("Writer");

                                    MessageBox.Show("Role removed");