Thủ thuật Sharepoint 2010 part 43 ppsx

8 252 0
Thủ thuật Sharepoint 2010 part 43 ppsx

Đang tải... (xem toàn văn)

Thông tin tài liệu

Using SharePoint Commands ❘ 275 FIGURE 1031 As you can see, the pipeline is very powerful. Be careful when you pipe objects into destructive commands. The WhatIf parameter and PowerShell’s confirm message will help to keep you out of trouble — that is, if you pay attention! 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. That does not mean that there is no way to access these and other items not exposed by the included commands. It means that we need to start thinking more like developers and attack the object model. This is exactly how we worked with SharePoint 2007 and PowerShell when we had no SharePoint cmdlets. We had to walk uphill both ways to and from school, and we liked it! This section does not delve too far into this subject, but it looks at how you can use PowerShell to list a web’s lists and libraries, and add and then remove a SharePoint list. That will provide you with the foundation to move outside the commands supplied by SharePoint 2010. Lists and libraries are children object of the SPWeb object we just looked at. The SPWeb object con- tains a single property, Lists, which is a collection of all its lists and libraries. To retrieve all the 276 ❘ CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell lists and libraries of a specifi c web, you can use the following command, as shown in Figure 10-32, within the Management Shell or a host with the SharePoint commands registered: (Get-SPWeb http://sharepoint/team/blog).Lists FIGURE 1032 It is likely that there is a lot of text fl ying 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 defi ned default format; therefore, PowerShell does its best formatting by dumping all the properties. Control+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 fl ying text. Run the same command, but this time send the lists and libraries out to Format-Table, which is another PowerShell formatting command, as shown in Figure 10-33. (Get-SPWeb http://sharepoint/team/blog).lists | Sort-Object Title | Format-Table Title, Id, ItemCount, hasUniqueRoleAssignments, EnabledAttachments, EnableThrottling Using SharePoint Commands ❘ 277 FIGURE 1033 Many of the lower-level objects such as SPList and SPListItem will 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 specifi c 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://sharepoint/team/blog).lists[2] | Format-Table Title, Id, ItemCount, hasUniqueRoleAssignments, EnabledAttachments, EnableThrottling In the preceding example, the value 2 is used, rather than 3, because developers like to start count- ing at 0. Therefore, the fi rst item in the collection is number 0, and the third item is 2. As mentioned previously, you can also get a list by using the Id or list title. Simply replace the num- ber 2 with the Id or Title. Go ahead and try: (Get-SPWeb http://sharepoint/team/blog).lists[“Links”] | 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 librar- ies. Want to create a new list? Well, that one 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 fi nd by looking in the SharePoint documentation. You can also get this information by running the following command: (Get-SPWeb http://sharepoint/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, 278 ❘ CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell and ListTemplateId. Armed with all of this information, add that list using the following command: (Get-SPWeb http://sharepoint/team/blog).Lists.Add(“Demo Links List”, “This is the description”, 103) Figure 10-34 shows the new, improved list of lists — now with even more Links lists. FIGURE 1034 Finally, to close out this section you will delete your list. Yes, delete and not remove. The Delete method is a method of the SPListCollection object (the Lists property), and not a command in PowerShell, which is why you can use it. The Delete method of the List collection requires the list Id, so we 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, and Figure 10-35 shows what it looks like. $listId = (Get-SPWeb http://sharepoint/team/blog).lists[“Demo Links List”].Id (Get-SPWeb http://sharepoint/team/blog).lists.Delete($listId) FIGURE 1035 Using SharePoint Commands ❘ 279 Again, you can verify that you did indeed remove the list by using the earlier command to list all your lists. We can’t close out this section without at least pointing you new admin developers to the key documentation that will help you with these more interesting creations: the SharePoint 2010 SDK, or Software Development Kit. Don’t let the name scare you. It is actually a compiled help file or a set of web pages that provides documentation on the various objects in SharePoint. The SharePoint 2010 SDK is currently located at http://msdn.microsoft.com/en-us/library/ ee557253(office.14).aspx . (Note that it might move later, as it contains “Office.14” in the URL. To find it after it is moved, simply search for “SharePoint 2010 SDK.” Disposing of SharePoint Variables No chapter on working with SharePoint and PowerShell would be complete without discussing a topic that just might keep your farm up and running: disposing of your SharePoint objects. It is no myth that certain SharePoint objects need to be disposed of properly or you might see memory leaks on your beloved farm. Although a few variables slipped into the examples, this chapter specifically avoided scenarios that required their use. It instead focused on single-line commands, which includes commands that are chained using the pipeline, because single-line commands will properly handle the disposal of SharePoint objects. The more you stick to one-liners, the less chance you have to forget how to prop- erly clean up after yourself. The disposal issue becomes a problem as soon as you start to capture certain SharePoint objects such as SPSite, SPSiteAdministration, and SPWeb, and hold on to them. They cannot be disposed of at the end of the pipeline because you are still using the object. Luckily, there are two commands to help you work with situations in which you might run into disposal issues: Start-SPAssignment and Stop-SPAssignment. The SPAssignment commands help you to both track objects and then dispose of them when they are no longer needed. This section covers two different ways of using these commands. The first command is the simple assignment method that uses the Global switch parameter. Basically, before you start to use objects that might need to be disposed of, you call Start-SPAssignment using the Global switch param- eter. This will start the tracking of all resources being used, which quite frankly can be a lot. Once you are done working with your variables and objects, you need to call Stop-SPAssignment with the same Global switch parameter. At this point, all the objects that were tracked will be released and properly disposed of. Once you call Stop-SPAssignment you should not use the resources for that block of commands, as they may not work properly as they are disposed of. Figure 10-36 demonstrates the use of Start and Stop-SPAssignment with the Global switch parameter. While using the simple assignment method is simple, it does have some drawbacks. Any trackable SharePoint object between the Start and Stop-SPAssignment will be managed by the commands. This means that if you run many one-liners that do not necessarily need to be tracked, they will get tracked anyway and that is more memory that is waiting to be released. If you know when you need to track a SharePoint object, you can manually assign your resources to be tracked. This enables you to be selective regarding the objects that are assigned for tracking 280 ❘ CHAPTER 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell and disposal. You can do this with the same Start and Stop-SPAssignment commands. With this technique, you create a new SPAssignmentCollection using Start-SPAssignment — for example, $spAssign = Start-SPAssignment. FIGURE 1036 When you need to track objects, you can use the $SPAssign in the pipeline. For example, you can assign all webs from a particular site collection for tracking and disposal: $Webs = $SPAssign | Get-SPSite http://sharepoint | get-SPWeb You can also throw in a few one-liners as we have been doing up until now. These do not need to be tracked because they will be disposed of properly. Since these objects will not be assigned to the SPAssignmentCollection object, they will be disposed of at the end of their lifetime and will not hold onto extra memory. Once you are done with your block of commands, you can clean up using the Stop-SPAssignment command, passing in the $SPAssign variable. Like the simple assign- ment, once you call Stop-SPAssignment you should not use the variables that were assigned to the SPAssignmentCollection. Figure 10-37 demonstrates the assignment of a collection of web objects and their proper disposal with the call to Stop-SPAssignment. FIGURE 1037 Summary ❘ 281 Note in Figure 10-37 that you do not assign the SPWeb object you used to display the SPList objects. This SPWeb object is in a one-liner and will be disposed of correctly without the need for any tracking. SUMMARY This PowerShell thing just might catch on. As demonstrated in this chapter, it adds a dimension of power to SharePoint administration that just wasn’t available to administrators in SharePoint 2007. This chapter has taken you from being a PowerShell newbie to using advanced techniques on your SharePoint farm, such as looping and object disposal. While we don’t recommend talking to devel- opers if it can be avoided, using PowerShell levels the playing field some. You’ll be able to tell amus- ing anecdotes about SPSites and SPWebs and they’ll laugh along. Oh, and you’ll be able to better administer your SharePoint 2010 farm too. . URL. To find it after it is moved, simply search for SharePoint 2010 SDK.” Disposing of SharePoint Variables No chapter on working with SharePoint and PowerShell would be complete without discussing. admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell lists and libraries of a specifi c web, you can use the following command, as shown in Figure 10-32, within the Management Shell or a host with the SharePoint. 10 admiNisteriNg sharePoiNt 2010 With WiNdoWs PoWershell and ListTemplateId. Armed with all of this information, add that list using the following command: (Get-SPWeb http:/ /sharepoint/ team/blog).Lists.Add(“Demo

Ngày đăng: 02/07/2014, 12:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan