locked
SPWeb vs. SPSite.rootweb? RRS feed

  • Question

  • I used Get-SPWeb to get a handle on the web with the URL of the site collection ($site), and assigned it to $web.

    Then I assigned $site.rootweb to $rootweb.

    Can anyone explain this behavior to me?


    PS D:\sp2010\PowerShellScripts> $rootweb.id

    Guid
    ----
    edee2159-28e0-43c8-ba62-7a5b63492e55


    PS D:\sp2010\PowerShellScripts> $web.id

    Guid
    ----
    edee2159-28e0-43c8-ba62-7a5b63492e55


    PS D:\sp2010\PowerShellScripts> $web.webs
    PS D:\sp2010\PowerShellScripts> $rootweb.webs

    Url
    ---
    https://test/sites/test_Philo_...
    https://test/sites/test_Philo_...
    https://test/sites/test_Philo_...

    The two objects have the same ID, but $rootweb.webs returns the child webs of the root website, while $web.webs doesn't return anything. I'm seeing other indications that these are not the same object - for example the site administrators are different for the two of them.


    Philo Janus, MCP Bridging business & Technology: http://www.saintchad.org/ Telecommuter? http://www.homeofficesurvival.com/ Author: Pro InfoPath 2007 & Pro InfoPath 2010 Pro PerformancePoint 2007 Pro SQL Server Analysis Services 2008 Building Integrated Business Intelligence Solutions

    Thursday, July 12, 2012 4:03 PM

Answers

  • Hrm. If you look at what I posted, you'll see that $web and $rootweb have the same ID, but their .webs properties are different.

    However, I just duplicated the test on another farm and got the expected result (the .webs properties match)

    More investigation is needed...

    ---

    [edit]

    Okay, it looks like what happened is that when I instantiated $web, it was before I created the subsites under that site, and I was working with a "cached" copy of the site object, instead of a live instance of the site. So I was seeing the properties from when I instantiated it, not current.


    Philo Janus, MCP Bridging business & Technology: http://www.saintchad.org/ Telecommuter? http://www.homeofficesurvival.com/ Author: Pro InfoPath 2007 & Pro InfoPath 2010 Pro PerformancePoint 2007 Pro SQL Server Analysis Services 2008 Building Integrated Business Intelligence Solutions


    • Edited by Philo Janus Thursday, July 12, 2012 6:55 PM
    • Marked as answer by Philo Janus Tuesday, July 17, 2012 8:20 PM
    Thursday, July 12, 2012 6:34 PM

All replies

  • Get-SPWeb can be used to retrieve a SharePoint web site anywhere in the hierarchy.  the rootweb property is always the root web site of the site collection.  Both are SPWeb objects.  Its like all squares are rectangles, but not all rectangles are squares.  If you used Get-spweb to return the root web site of the site collection then $web.webs should have returned the same as $rootweb.webs.

    Paul Stork SharePoint Server
    MVP Senior Solutions Architect: BlueChip Consulting Group
    Blog: http://dontpapanic.com/blog
    Twitter: Follow @pstork
    Please remember to mark your question as "answered" if this solves your problem.

    • Proposed as answer by Devang Bhavsar Thursday, July 12, 2012 5:44 PM
    Thursday, July 12, 2012 5:39 PM
  • Hrm. If you look at what I posted, you'll see that $web and $rootweb have the same ID, but their .webs properties are different.

    However, I just duplicated the test on another farm and got the expected result (the .webs properties match)

    More investigation is needed...

    ---

    [edit]

    Okay, it looks like what happened is that when I instantiated $web, it was before I created the subsites under that site, and I was working with a "cached" copy of the site object, instead of a live instance of the site. So I was seeing the properties from when I instantiated it, not current.


    Philo Janus, MCP Bridging business & Technology: http://www.saintchad.org/ Telecommuter? http://www.homeofficesurvival.com/ Author: Pro InfoPath 2007 & Pro InfoPath 2010 Pro PerformancePoint 2007 Pro SQL Server Analysis Services 2008 Building Integrated Business Intelligence Solutions


    • Edited by Philo Janus Thursday, July 12, 2012 6:55 PM
    • Marked as answer by Philo Janus Tuesday, July 17, 2012 8:20 PM
    Thursday, July 12, 2012 6:34 PM
  • That would definately do it.  Objects do get cached in Powershell and you have to be careful to clear things out if you make changes.  Glad you found the issue.

    Paul Stork SharePoint Server
    MVP Senior Solutions Architect: BlueChip Consulting Group
    Blog: http://dontpapanic.com/blog
    Twitter: Follow @pstork
    Please remember to mark your question as "answered" if this solves your problem.

    Friday, July 13, 2012 11:57 AM