$siteUrl="SiteURLHere" $groupName="GroupNametoExport" $Output = @("GroupName|Name|Login|Email|Department|Title") $web = Get-SPWeb $siteUrl $site = $web.Site $rootWeb = $site.RootWeb $UserList = $rootWeb.Lists["User Information List"] $web.SiteGroups[$groupName].Users|%{$user = $UserList.GetItemById($_.ID) if($user -ne $null) { $JobTitle = $user["JobTitle"] $Department = $user["Department"] } $Output += ($groupName+"|"+$_.Name+"|"+$_.UserLogin+"|"+$_.Email+"|"+$ Department +"|"+$JobTitle) } $rootWeb.Dispose() $web.Dispose() $site.Dispose() $Output > "D:\MembersExport.csv"
Import Users from Excel to SharePoint using PowerShell
# Import the .csv file, and specify manually the headers, without column name in the file $userList=IMPORT-CSV C:\UserToUpload.csv -header("GroupName","UserName") #Get the site name to the variable $web = Get-SPWeb SiteURLHere foreach ($user in $userList) { $groupName = $web.SiteGroups[$userList.group] $user = $web.Site.RootWeb.EnsureUser($userList.user) $groupName.AddUser($user) } Write-Host -ForegroundColor green "Users Added Successfully" } $Web.Dispose()
Add Users to SharePoint Group using PowerShell
#Get the site name to the variable $web = Get-SPWeb SitURLHere #Get the group name to the variable $groupName = $web.SiteGroups["Group Name Here"] #Assign user to variable $user = $web.Site.RootWeb.EnsureUser(“User ID Here”) #Add user to group $groupName.AddUser($user) Write-Host -ForegroundColor green "User Added Successfully";
Programmatically get SharePoint Users with Group Name
In this
article, we will come to know how to programmatically list all the users from
the SharePoint site.
Scenario:
We have to
retrieve all the Users from the
SharePoint site with the Group Name using object
model.
Solution:
I have created
a console application which will group the users with the Group Name and also
display the User details.
Code (.cs):
Code (.cs):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; namespace UserPermissions_Console { class Program { static void Main(string[] args) { try { GetUsersGroups(); } catch (Exception ex) { Console.WriteLine("Error Occured: " + ex.Message); } } private static void GetUserRoles() { using (SPSite site = new SPSite("http://sitename")) { SPWeb web = site.OpenWeb(); Console.WriteLine("\n\n Roles Assignments:"); foreach (SPRoleAssignment roleA in web.RoleAssignments) { Console.WriteLine("The following Role definition bindings exist for" + roleA.Member.Name); foreach (SPRoleDefinition roledef in roleA.RoleDefinitionBindings) { Console.WriteLine(roledef.Name); } } Console.ReadLine(); } } private static void GetUsersGroups() { using (SPSite site = new SPSite("http://sitename")) { SPWeb web = site.OpenWeb(); SPGroupCollection groupCollection = web.SiteGroups; foreach (SPGroup group in groupCollection) { SPUserCollection userCollection = group.Users; Console.WriteLine("Group Name :" + group.Name+"\n"); foreach (SPUser user in userCollection) { Console.WriteLine("User Name: " + user.Name + " Email: " + user.Email + " Login: " + user.LoginName); } } //Iterate the owners group SPGroup ownerGroup = web.AssociatedOwnerGroup; foreach (SPUser ownerUser in ownerGroup.Users) { Console.WriteLine("User Name: " + ownerUser.Name + " Email: " + ownerUser.Email + " Login: " + ownerUser.LoginName); } } Console.ReadLine(); } } }
You can download the full solution using this link.
Hope this helps you! Please free to comment and share this post.
Exporting SharePoint User Groups with Group Names
In the article Exporting
SharePoint User Groups into Excel, we have learned about exporting the
SharePoint user groups into excel. Here we are able to export all the users in
a SharePoint site but we are unable to sort the
users with the group name.
In this article, we will learn a small work
around filter the users with the group name.
We can use SQL Query to
fetch this data.
Connect to the SQL server and open a new
query in the content database where your SharePoint site resides.
The SharePoint
content DB does not expose the tables
(for lists, libraries, users, etc.) in the database directly.
The below is the query which will fetch all the users in the SharePoint site with the user groups in a separate column in the result.
SELECT dbo.Groups.ID, dbo.Groups.Title, dbo.UserInfo.tp_Title, dbo.UserInfo.tp_Login FROM dbo.GroupMembership INNER JOIN dbo.Groups ON dbo.GroupMembership.SiteId = dbo.Groups.SiteId INNER JOIN dbo.UserInfo ON dbo.GroupMembership.MemberId = dbo.UserInfo.tp_ID
SELECT dbo.Groups.ID, dbo.Groups.Title, dbo.UserInfo.tp_Title, dbo.UserInfo.tp_Login FROM dbo.GroupMembership INNER JOIN dbo.Groups ON dbo.GroupMembership.SiteId = dbo.Groups.SiteId INNER JOIN dbo.UserInfo ON dbo.GroupMembership.MemberId = dbo.UserInfo.tp_ID
Hope this helps you!
Please free to comment. Always your
comments makes me to write more!
Export SharePoint User Groups in Excel
In this article, we are going to discuss about a SharePoint tip which is not present in the User Groups by default.
Read this article to know How to Programmatically get the users with the Group Name in SharePoint
Scenario:
From any list in the SharePoint, you can export the whole list in to a excel file or you can edit and save the excel file.
But is that possible in SharePoint User Groups?
Yes, it is possible.
Solution:
Solution:
1. Go to Site Settings (http://sitname/_layouts/settings.aspx)
-> Under Users and Permissions Select People and groups
2. You will be seeing a URL like this http://sitname/_layouts/people.aspx?MembershipGroupId=5
3. Then navigate to the List Settings in the people and groups of a particular group which you want to export to the excel file
4. Now, in the URL you will see like this:
http://sitname/_layouts/listedit.aspx?List=72bcbfee-c10e-41ba-b128-2769c8fe9cc9&Source=%2Fpeople%2Easpx%3FMembershipGroupId%3D5
5. If you want you retrieve the users list from any view, then click on the view from the views list as shown below:
Scroll to the bottom of the page and click on the view you want to export
6.Now, copy the URL from the address bar and keep it in a notepad which is something like below:
http://sitname/_layouts/listedit.aspx?List=72bcbfee-c10e-41ba-b128-2769c8fe9cc9&Source=%2Fpeople%2Easpx%3FMembershipGroupId%3D5
5. If you want you retrieve the users list from any view, then click on the view from the views list as shown below:
Scroll to the bottom of the page and click on the view you want to export
6.Now, copy the URL from the address bar and keep it in a notepad which is something like below:
http:// sitname
/_layouts/ViewEdit.aspx?List=%7B72BCBFEE%2DC10E%2D41BA%2DB128%2D2769C8FE9CC9%7D&View=%7B4A52DE00%2D9B7C%2D4FDC%2DAC39%2D960FDF18F1F3%7D&Source=%252F%255Flayouts%252Flistedit%252Easpx%253FList%253D72bcbfee%252Dc10e%252D41ba%252Db128%252D2769c8fe9cc9%2526Source%255Flayouts%25252Fpeople%25252Easpx%25253FMembershipGroupId%25253D5
From the above URL, we have to copy the LIST ID and VIEW ID (marked bold in the above URL)
7. Now, we can easily export the people and groups in to excel using the following dll from the SharePoint server.
http://[sitename]/_vti_bin/owssvr.dll?CS=109&Using=_layouts/query.iqy&List=[LISTID]&View=[VIEWID]&CacheControl=1
8. Now, replace the site name, LIST ID and VIEWID in the above URL:
I will get the final URL as:
http://sitename/_vti_bin/owssvr.dll?CS=109&Using=_layouts/query.iqy&List=%7B64DAB4AC%2DD043%2D4DA4%2D85C7%2D9D4521C3F4CC%7D&View=%7B0FAE8D90%2D59D5%2D48F4%2DA54E%2DF02333777C7F%7D&CacheControl=1
Note:
To know more about the owssvr.dll please read this article
Open the above URL in the address bar, you will get an option to save an EXCEL FILE.
9. Open the Excel file, you will be asked for authentication, enable the external content and open it.
10. That’s it. You will see all the users in the groups with their details exported to the excel file.
11. This excel file exported using this method has all the users from the user groups but it is not filtered by user group name.
Read this article to achieve exporting the SharePoint user groups with the group name Exporting SharePoint User Groups with Group Names.11. This excel file exported using this method has all the users from the user groups but it is not filtered by user group name.
Read this article to know How to Programmatically get the users with the Group Name in SharePoint
Comments