Working with Objects Below the Web Level
As mentioned earlier, SharePoint PowerShell commands generally work from the SPWeb object and above. That means you will not find commands such as Get-SPList or New-SPLibrary in the out-of-the-box commands. However, with a little digging, and liberal usage of the Get-Member
cmdlet, you can use PowerShell and the object model to list a web’s
lists and libraries, as well as add and then remove a SharePoint list.
That provides you with the foundation to move beyond the commands
supplied by SharePoint 2013.
Lists and libraries are child objects of the SPWeb object you just looked at. The SPWeb object contains a single property, Lists,
which is a collection of all its lists and libraries. To retrieve all
the lists and libraries of a specific web, you can use the following
command, as shown in Figure 16, within the Management Shell or a host with the SharePoint commands registered:
(Get-SPWeb http://portal.contoso.com/team/blog).Lists
In all likelihood, a lot of text is flying across
your screen now as all the properties of all the lists and libraries
are displayed. Unlike the previous SharePoint objects you have worked
with, the SPList list object
that is returned from the preceding command does not have a defined
default format; therefore, PowerShell dumps all the properties.
NOTE Ctrl+C will exit the current processing and return you to your prompt.
None of us can read that fast. Fortunately, you
can control how each list is formatted and slow down some of that
flying text. Run the same command, but this time send the lists and
libraries out to Format-Table, another PowerShell formatting command, shown in Figure 17:
(Get-SPWeb http://portal.contoso.com/team/blog).lists | Sort-Object Title |
Format-Table Title, Id, ItemCount, hasUniqueRoleAssignments,
EnabledAttachments, EnableThrottling
NOTE Many of the lower-level objects such as SPList and SPListItem do not save their changes to the content database until the Update method is called on the object.
Now that you know how to retrieve all the lists
and libraries contained within a SharePoint web, the following example
demonstrates how to get just one specific list. The Lists property on the SPWeb
object returns a collection of lists, like many of the properties
associated with SharePoint objects. You can retrieve any list by using
the index, ID, or title. For example, to get the third item in the
lists collection, use the following:
(Get-SPWeb http://portal.contoso.com/team/blog).lists[2] |
Format-Table Title, Id,
ItemCount, hasUniqueRoleAssignments,
EnabledAttachments, EnableThrottling
In the preceding example, note that the value 2
is used, rather than 3, because developers like to start counting at 0.
Therefore, the first item in a collection is number 0, so the third
item is 2.
As mentioned previously, you can also get a list
by using the ID or list title. Simply replace the number 2 with the ID
or Title:
(Get-SPWeb http://portal.contoso.com/team/blog).lists["Posts"] |
Format-Table Title, Id, ItemCount, hasUniqueRoleAssignments,
EnabledAttachments, EnableThrottling
At this point, you know how to get down to the
list level and enumerate all of your lists and libraries. Creating a
new list is a little tricky. First, you need to decide what type of
list you will create. To keep it simple, the next example creates a
Links list. The Links list template has an ID of 103, which is
information you can find by looking in the SharePoint documentation.
You can also get this information by running the following command:
(Get-SPWeb http://portal.contoso.com/team/blog).ListTemplates |
Where-Object {$_.Name –eq "Links"}
To create the Links list, you need to call the Add method of the List collection you have already been working with. The Add method requires three parameters for this example: Title, Description, and ListTemplateId. Armed with all this information, add that list using the following command:
(Get-SPWeb http://portal.contoso.com/team/blog).Lists.Add("Demo Links List",
"This is the description", 103)
Figure 18 shows the new, improved list of lists — now sporting a new Links list.
Finally, to close out this section you will delete your list. Yes, delete; not remove. The Delete method is a method of the SPListCollection object (the Lists property), not a command in PowerShell, which is why you can use it. The Delete
method of the List collection requires the list ID, so you are going to
use a variable this time to grab the list ID in one line and use it in
the Delete on the next line. Following are the two lines needed to delete the list you just created (Figure 19 shows what it looks like):
$listId = (Get-SPWeb http://portal.contoso.com/team/blog).
lists["Demo Links List"].Id
(Get-SPWeb http://portal.contoso.com/team/blog).lists.Delete($listId)
Again, you can verify that you did indeed remove the list by using the following command to list all your lists.
(Get-SPWeb http://portal.contoso.com/team/blog).lists | Sort-Object Title |
Format-Table Title, Id, ItemCount, hasUniqueRoleAssignments,
EnabledAttachments, EnableThrottling
A USEFUL RESOURCE
This section can’t close without at
least pointing new admin developers to the key documentation that will
help you with these more interesting creations: the SharePoint 2013
developer reference site, currently located at http://msdn.microsoft.com/en-us/library/jj193038.aspx.
This set of web pages provides documentation on the various objects in
SharePoint. PowerShell uses the SharePoint 2013 object model, so any
references about it, regardless of the target language, will be
helpful.