programming4us
 
Applications Server
 

Microsoft Sharepoint 2013 : Using SharePoint Commands (part 5) - Working with Objects Below the Web Level

1/14/2015 8:38:32 PM

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:

FIGURE 16

image
(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:

FIGURE 17

image
(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.

FIGURE 18

image

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):

FIGURE 19

image
$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.
 
Others
 
- Microsoft Sharepoint 2013 : Using SharePoint Commands (part 4) - Working with Webs
- Microsoft Sharepoint 2013 : Using SharePoint Commands (part 3) - Working with Site Collections
- Microsoft Sharepoint 2013 : Using SharePoint Commands (part 2) - Web Applications
- Microsoft Sharepoint 2013 : Using SharePoint Commands (part 1) - Working with the Farm, Working with the Farm Configuration
- Microsoft LynServer 2013 : Dependent Services and SQL - Office Web Apps Server
- Microsoft LynServer 2013 : Dependent Services and SQL - Network Dependencies (part 2) - Defining Network Sites
- Microsoft LynServer 2013 : Dependent Services and SQL - Network Dependencies (part 1) - Supporting Lync Phone Edition with DHCP
- Microsoft Exchange Server 2013 : Designing a Successful Exchange Storage Solution (part 3) - Selecting the Right Storage Hardware, Storage Validation Using Jetstress
- Microsoft Exchange Server 2013 : Designing a Successful Exchange Storage Solution (part 2) - Making Sense of the Exchange Mailbox Server Role Requirements Calculator
- Microsoft Exchange Server 2013 : Designing a Successful Exchange Storage Solution (part 1) - Requirements Gathering
 
 
REVIEW
 
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- Sigma 24mm f/1.4 DG HSM Art

- Canon EF11-24mm f/4L USM

- Creative Sound Blaster Roar 2

- Alienware 17 - Dell's Alienware laptops

- Smartwatch : Wellograph

- Xiaomi Redmi 2
 
VIDEO TUTORIAL
 
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
 
Top 10
 
- Setup Free Media Server To Stream Videos To DLNA Compatible TV, Xbox 360 & PS3 (Play Station 3)
- How To Install Android Market & Google Apps On Kindle Fire
- How To Make Ubuntu Look Like Windows 7
- How To Add A New Account in MS Outlook 2013
- Get Android & Mac OS X Style Gadgets For Windows 7 & Windows 8 With XWidget
- How To Activate Microsoft Office 2013
- How To Install Actual Facebook App On Kindle Fire
- How To Create, View And Edit Microsoft Office Files On Kindle Fire
- Download Attractive Business PowerPoint Templates For Free At SlideHunter
- How To Use And Enable Hibernate & Sleep Mode In Windows 8