2

What is the difference between SPWeb and SPSite? I have this code using SPWeb

$web = Get-SPWeb http://mysite 
ForEach($list in $web.Lists)
{
    if($list.BaseType -eq "DocumentLibrary")
    {
        Write-Host $list.Fields
        if($list.Fields.ContainsField("marking") -eq $true)
        {
            Write-Host "found" $list.Title
        }
    }
}

Which doesn't produce the right result, as apparently GPMS marking is not a column for a Document Library. But when I use this code using SPSite

Get-SPSite mysite |
Select -ExpandProperty AllWebs |
Select -ExpandProperty Lists |
where {$_.GetType().Name -eq "SPDocumentLibrary"} |
Select -ExpandProperty Fields |
Select Title | out-file test.txt

This has the so called marking column that I want to search for in the previous bit of code. Is there anyway of changing the previous bit of code so it does find the marking column?

  • 1
    SPSite represents a site collection while SPWeb represents a specific web site within the site collection. Normally, you open a specific SPWeb within SPSite via SPSite.OpenWeb(). You can use SPSite.AllWebs to enumerate existing webs, or SPSite.RootWeb. – w128 Jun 6 '14 at 10:39
  • So your site name is same for both the cases?? – Aanchal Jun 6 '14 at 11:05
6

You need to start of with the tree ;)

You have a farm (SPFarm) and in that farm you have many web applications (SPWebApplication), within one of those web applications you would have a site collection(SPSite) and within that site collection you would have many sub sites (SPWeb) that hold many sites (SPWeb) or in your case webs (SPWeb) under the site colleciton.

from a url perspective:

http://webApplication(SPWebApplication)/siteCollection(SPSite)/web(SPWeb)

coding wise in your case your calling an spweb on a toplevel spsite. coding wise if you want to get the spweb object you need to note that its rootweb ;) as your mysite is based at the sitecollection and not at the subsite or site level!

http://msdn.microsoft.com/en-gb/library/microsoft.sharepoint.spsite.rootweb.aspx

$site = Get-SPSite http://mysite
$site.RootWeb.Lists
2

Your example is a bit ambiguous. One example deals with just 1 SPWeb at a specific location, the other with an SPSite, as you say, but you pipe ALL the subwebs of that SPSite (and even subwebs within subwebs I believe). So it may be that you find the field in a completely different SPWeb under the SPSite, where before you were just checking 1 specific SPWeb under the SPSite.

So most likely that field was not deployed to "http:// skynet" but a different SPWeb.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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