PowerWebpart / List all Sitecollections

May 6, 2009 at 9:27 AM
Hi,

first of all: many thanks for this great webpart!! :)

As i'm new to PowerShell i'm not sure how to realize this:

I like to use the powerwebpart to create a list of hyperlinks to all Sitecollections in the webapp where the user has access to. (At least Read Permissions)

Can you give me any CodeSnippets / Examples how i can realise this!? I'm not sure how to pass the User and also render the result as html...

Thanks!

Mich
May 6, 2009 at 2:08 PM
After "Playing around" i have the following script:

[

 

string]$siteUrl = http://MOSSURL
[
string]$userToFind
= "DOMAIN\USER"
function showAllApplication
{
if($args[0].Sites -ne $Null){foreach($spSiteColl in $args[0].Sites){showAllSiteCollection $spSiteColl ($args[1] + 2)
$spSiteColl.Dispose()
}
}
}
function showAllSiteCollection
{
$sc = $args[0].OpenWeb()
$output = ($sc.URL)
if ($sc.DoesUserHavePermissions($userToFind, 131072) -eq "True")
{
Write-Output $output}
$output | out-file "C:\MYSiteCollections.txt" -append
$sc.Dispose()
}
$rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$spWebApp = $rootSite.WebApplication
showAllApplication
$spWebApp 1
$rootSite.Dispose()

This script works fine from the ps commandline... Now i need to know the following to inculde it into the powerwebpart:

 

  1. How can i use the RunAs-System Statement within this Script? Because I need it for "DoesUserHavePermissions"
  2. How can I pass the Username who has opened the site with the powerwebpart to the Script
  3. How can i render the results?

Many Questions ;) I hope somebody can help!

Thanks!

Mich

Coordinator
May 7, 2009 at 7:07 PM
Hi Mich,

just one question before I'll post an example. Do you want to get the sub webs for current user?
Maybe this would be the right method: GetSubWebsForCurrentUser http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getsubwebsforcurrentuser.aspx
MSDN says it looks for member but reflector says looks for View Pages permissions.

Bye,
Christian 
May 7, 2009 at 7:28 PM
Hi Christian,

in my scenario i have many different sitecollections and i like to list all sitecollections for the current user (in debendency of different permissons: e.g. all Sitecollections with view permissons, all with edit permissons...)
The Code i posted only looks if the user has permissons on the sitecollection, if the user has e.g. view permissons in a subweb of the sitecollection its not shown...
As i understand getsubwebs only returns subwebs in the current web, or does it list all webs in all sitecollections?

Can you please explain me how to use the RunAs-System function!

Bye,
Mich
Coordinator
May 7, 2009 at 8:25 PM
Hi,
try this:

$url = "http://localhost"
$userToFind = $web.CurrentUser.LoginName

function LoopSites
{
 $webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url)
 
 foreach($site in $webApp.Sites)
 {
  $web = $site.RootWeb
  
  if($web.DoesUserHavePermissions($userToFind, 131072) -eq $true)
  {
   $writer.Write($("<a href='{0}'>{1}</a><br/>" -f $web.Url, $web.Title))
  }
  $web.Dispose()
 }
 $site.Dispose()
}

function render($writer)
{
 runas-system "LoopSites"
}

Coordinator
May 8, 2009 at 6:07 AM

Hi Mich,

I've just tested with an another user and unfortunately seems the runas-system function is broken. The script abover is right, but the impersonation fails. I'll fix this this weekend.

Bye, christian

Coordinator
May 10, 2009 at 8:23 AM

Hi Mich,

fixed with Build 0905101015. Now the above script should work as expected.

Bye, Christian

May 11, 2009 at 9:31 AM
Edited May 11, 2009 at 9:33 AM

Hi Christian,

thanks a lot! But unfortunatly i get the following error message, when i enter the site as "normal" user:

Error on render
PowerShell Exception:
Ausnahme beim Aufrufen von "RunAsSystem" mit 2 Argument(en): "Ausnahme beim Aufrufen von "DoesUserHavePermissions" mit 2 Argument(en): "Es wurde versucht, einen nicht autorisierten Vorgang auszuführen.""

 

I get this Error message with all user when i was using your last version! Now the function works as admin user but not as normal user... Did I miss anything? I have uninstalled the solution and deployed it again (just to be sure the update is no problem)

 

Bye,

Mich

 

Edit: Is there a way to impersonate a special user within the webpart? So i could test with some other users... And sometimes it would be helpful to use a special users (not the system account!).

May 11, 2009 at 11:44 AM

Hi Christian,

Sorry, i forgot to modify the aspnet.config again...

Now everything works fine!!

Thanks a lot for your work and for your help!!

Bye,

Mich

May 15, 2009 at 9:02 AM

Hi Christian,

is it possible that i get permisson problems with the power event recievers after modifing the asp.net config?
Is there a way to use the run-as function also in the event recievers?

I tested to wriste something with e.g. Out-File "C.\tmt.txt" and get a permission denied error (when i'm a normal user!)
Could you please give me a example to impersonate a script to any user!?

 

Bye,

Mich