Joseph Stronks
asked on
Export users in a SharePoint 2013 site to csv file
Hi,
We need to export members of a SharePoint user group to a text file on a schedule. I found below sites with a script that can do that.
(http://sharepoint-works.blogspot.com/2013/01/export-sharepoint-group-to-excel-using.html)
(https://chaddtalks.wordpress.com/2015/10/05/export-a-list-of-users-from-a-specific-sharepoint-group/)
$SiteUrl="https://portal.site.com";
$GroupName="Site Members";
$Output = @("GroupName|Name|Login|Em ail|Depart ment|Title ")
$web = Get-SPWeb $siteUrl
$site = $web.Site
$rootWeb = $site.RootWeb
$UserList = $rootWeb.Lists["User Information List"]
$web.SiteGroups[$groupName ].Users|%{ $user = $UserList.GetItemById($_.I D)
if($user -ne $null)
{
$JobTitle = $user["JobTitle"]
$Department = $user["Department"]
}
$Output += ($groupName+"|"+$_.Name+"| "+$_.UserL ogin+"|"+$ _.Email+"| "+$Departm ent +"|"+$JobTitle)
}
$rootWeb.Dispose()
$web.Dispose()
$site.Dispose()
$Output > "GroupMembers.csv"
When I run the script , it dumps the users to csv file but I'm getting a bunch of below errors. Is there a way to fix the script so it won't have these errors?
You cannot call a method on a null-valued expression.
At C:\export.ps1:9 char:37
+ $web.SiteGroups[$groupName ].Users|%{ $user = $UserList.GetItemById($_.I D)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Thank you.
We need to export members of a SharePoint user group to a text file on a schedule. I found below sites with a script that can do that.
(http://sharepoint-works.blogspot.com/2013/01/export-sharepoint-group-to-excel-using.html)
(https://chaddtalks.wordpress.com/2015/10/05/export-a-list-of-users-from-a-specific-sharepoint-group/)
$SiteUrl="https://portal.site.com";
$GroupName="Site Members";
$Output = @("GroupName|Name|Login|Em
$web = Get-SPWeb $siteUrl
$site = $web.Site
$rootWeb = $site.RootWeb
$UserList = $rootWeb.Lists["User Information List"]
$web.SiteGroups[$groupName
if($user -ne $null)
{
$JobTitle = $user["JobTitle"]
$Department = $user["Department"]
}
$Output += ($groupName+"|"+$_.Name+"|
}
$rootWeb.Dispose()
$web.Dispose()
$site.Dispose()
$Output > "GroupMembers.csv"
When I run the script , it dumps the users to csv file but I'm getting a bunch of below errors. Is there a way to fix the script so it won't have these errors?
You cannot call a method on a null-valued expression.
At C:\export.ps1:9 char:37
+ $web.SiteGroups[$groupName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Your suggestion stopped the errors.
Thank you for your help.
Thank you for your help.
ASKER
Thank you very much for your help, it's working perfectly with no errors now.
Thank you again.