1

I'm working in a Sharepoint 2007 environment. I'm trying to loop through a site, and it's subsites, and add a webpart to any pages found.

#Add SharePoint assemblies
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint.publishing")

#Get site and web objects
$site = New-Object Microsoft.SharePoint.SPSite(http://test.fake.com/en-us/testsite/)

#Walk through each Publishing Web and add the content editor webpart
foreach ($web in $site.AllWebs) { #add webpart here }

My problem is that $site returns http://test.fake.com.. so when I hit my foreach loop it starts hitting anything under fake.com, and not /en-us/testsite/ and whatever sites below it. Is there something other than the SPSite object I should be using to accomplish what I want? Saying I'm new to this would be an understatement.. so bear with me.

Thanks for any help/insight.

3 Answers 3

2

You can start from the SPWebApplication

[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint.publishing") 

$wa = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://test.fake.com")
foreach ($site in $wa.Sites) {
 foreach ($web in $site.AllWebs) {
  #add webpart here 
  $web.Dispose()
 }
 $site.Dispose()
} 
1
  • I didn't check to see if this would work for me because I ended up taking a slightly different approach. Part of my problem was not realizing only sites have .AllWebs, and webs only have .Webs. I'll post my solution as a separate answer.
    – snowBlind
    Commented Mar 29, 2012 at 21:32
1

I ended up doing the below:

$site = new-Object Microsoft.SharePoint.SPSite("http://fake.test.com")
$web = $site.OpenWeb("/en-us/testsite")
foreach ($subweb in $web.Webs)
{
 #do checking if publishing page / if checked out / add webpart / checkin and publish
 #dispose subweb
}
#dispose web && site

This allows me to target the site I was aiming for, and then go through all of the subwebs and add webparts to the pages within those. I did run into a problem checking a file in, which has me baffled as to why it's throwing an error, but I'll save that for another post & another day.

1
  • Is it preferred to use new-Object Microsoft.SharePoint.SPSite in place of Get-spsite ?
    – Daniel
    Commented Feb 24, 2017 at 10:58
0

Try to use Get-SPSite instead of New-Object Microsoft.SharePoint.SPSite(http://test.fake.com/en-us/testsite/)

5
  • 1
    It used Get-SPSite originally, but I was getting errors related to that...Saying that it isn't a recognized name of a cmdlet, function, script file, or operable program. Someone told me that Get-SPSite is part of SP2010, but not SP2007 (which I'm using). [Not saying that's true, just what I was told.]
    – snowBlind
    Commented Mar 28, 2012 at 20:02
  • you should use Add-PSSnapin Microsoft.SharePoint.PowerShell comand before, in other case you cannot work with sharepoint in powershell
    – Alexander
    Commented Mar 28, 2012 at 20:10
  • Not to stray too far off topic, but I tried running that command, but it throws an error. I found steps to fix the error, but even those steps throw errors in the cmd prompt. I have another script that creates sites, subsites, and pages that works just fine, without using the PSSnapin command.
    – snowBlind
    Commented Mar 28, 2012 at 20:48
  • the powershell scripts runs in powershell.exe, not cmd.
    – Alexander
    Commented Mar 29, 2012 at 6:13
  • Yeah, I'm running the scripts in Powershell. However, the 'how-to get ps snapin to work' sites I found required the use of the command prompt to navigate to a certain file in the .net folder. (InstallUtil.exe) and run that with a specified parameter. The parameters the sites gave were causing problems, however.
    – snowBlind
    Commented Mar 29, 2012 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.