Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

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()
} 
share|improve this answer
    
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 Mar 29 '12 at 21:32

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.

share|improve this answer

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

share|improve this answer
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 Mar 28 '12 at 20:02
    
you should use Add-PSSnapin Microsoft.SharePoint.PowerShell comand before, in other case you cannot work with sharepoint in powershell –  Alexander Mar 28 '12 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 Mar 28 '12 at 20:48
    
the powershell scripts runs in powershell.exe, not cmd. –  Alexander Mar 29 '12 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 Mar 29 '12 at 14:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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