SharePoint PowerShell Learning three uses PowerShell management list

Source: Internet
Author: User
Tags foreach

Let me take a look at what list templates we have available, and we can use the ListTemplates parameter to set the list template under the SPWeb object. First, let's start by initializing a SPWeb object.

PS > $spWeb = get-spweb-identity http://localhost

We then use the ListTemplates parameter and use the Select-object parameter to see what list templates we have that can be used

We can use these list templates when we create a new list. When we want to create a list in SharePoint2010, you need to use the Add method in SPListCollection. In the following example we will use three parameters, Title,description, the type of these two parameters is System.String, and there is a template the type of this parameter is Splisttemplatetype. So we need to initialize an object to use these parameters.

PS > $listTemplate = [microsoft.sharepoint.splisttemplatetype]::contacts

PS > $spListCollection = $spWeb. Lists

PS > $spListCollection. ADD ("My Contacts", "Description", $listTemplate)

We are using the Add method to be sure to avoid using $spweb. Lists.add. If we use this method to create a lot of lists in a row, we will repeatedly get the lists data of the website, which will increase the memory consumption, if the call once and save in a variable, so that only need to get a lists of the data, and can be reused.

To update a SharePoint list by using PowerShell

Let's modify the list we just created, and first we need to use the GetLists () method to get to this list

PS > $spList = $spWeb. GetList ("/lists/my Contacts")

PS > $spList. onquicklaunch = "True"

PS > $spList. Update ()

When we use the above command, the link to the list is displayed in the Quick launch of the site, and if set to false, it will be hidden in the Quick Launch. If we want to modify the descriptive information for the list, we need to use the description parameter.

PS > $spList. Description = "My Contacts List"

PS > $spList. Update ()

Add a column to a SharePoint list by using PowerShell

If you want to add a new column to the list, we need to use the Add method in the Spfieldcollection class. Now let's create a column with a simpler single-line text.

PS > $spFieldType = [microsoft.sharepoint.spfieldtype]::text

PS > $spList. Fields.Add ("TextField", $spFieldType, $false)

The Add method used above has three parameters, the first is the name of the column, and the second argument is the type of the column, where we use a variable to hold the column type of the single line of text, and the third argument we enter false, which indicates whether the new column must enter a value when saving the form. That is, the "Require this column contains information" option selected in the Site Creation column.

SharePoint 2010 also supports other types of columns, and there is some need to add an option type of column, because it needs to set up some data to choose from. We can save the options bar to the System.Collections.Specialized.StringCollection class, and we'll look at the example below.

PS > $choices = New-object System.Collections.Specialized.StringCollection

PS > $choices. ADD ("Choice")

PS > $choices. ADD ("Second Choice")

PS > $choices. Add ("Third Choice")

Now we can use this variable to create a column of type "Choice" type

PS > $spFieldType = [microsoft.sharepoint.spfieldtype]::choice

PS > $spList. Fields.Add ("Choicefield", $spFieldType, $false, $false, $choices)

Using PowerShell to manage list views

Let's go ahead and see if you use PowerShell to manage a view of the list. We can customize the view to show the content of the data we need. When we create a new list, there is a default view. We can edit the default view by PowerShell. We can use Getviewfromurl to get a link to the view.

PS > $spView = $spWeb. Getviewfromurl ("/lists/my contacts/allitems.aspx")

When I create a new column, it will not be added to the default view to be displayed. We can add this column to this default view through PowerShell. We need to use a variable to save the column first.

PS > $spField = $spList. fields["TextField"]

We can add a column to the view by using the Add method in the Spviewfieldcollection class, and then use the update () method to save the update.

PS > $spView. Viewfields.add ($spField)

PS > $spView. Update ()

You can use more methods to manage list views, such as creating views and deleting views.

Using PowerShell to manage list items

Let's continue to see how to add a list item to a list. The Splists class provides a AddItem method that we can use to add a list item. When we use the AddItem method, it returns a Microsoft.SharePoint.SPListItem object. Because we need to set each column data for this list item.

PS > $spListItem = $spList. AddItem ()

Now we start to set its data on each column, and the SPListItem class provides a parameterized item property that we can use to set the value of a particular column. For example:

PS > $spListItem ["Title"] = "New Item"

PS > $spListItem ["TextField"] = "Hey hey"

PS > $spListItem ["choicefield"] = "Choice"

PS > $spListItem. Update ()

What should we do if we want to update an existing list item? The SPList class provides a number of methods that you can provide to me to find a list item. The most commonly used command-GetItemByID () and GetItems ()

The GetItemByID () method is to get this list item when we know the ID of the item. But in many cases, we don't know the ID of the list item, but we just know the title, or some other column information. At this time we need to use the GetItems () method. This method will return all list items or get list items when using CAML query definition

PS > $spQuery = New-object Microsoft.SharePoint.SPQuery

The SPQuery object is the class used to support query queries, at which point we can CAML query statements,

PS > $camlQuery =

>> ' <where><eq><fieldref name= "Title"/><value type= "Text" >true</value></eq ></Where> '

PS > $spQuery. Query = $camlQuery

After we use the GetItems method, we need to use some parameters to set the row number limit for the returned item, and if we don't use parameters with the row number limit, we may fail when we get some data that is very large.

PS > $spQuery. rowlimit = 100

PS > $spListItem = $spList. GetItems ($spQuery)

Now we can edit it by using the GetItems () method to get the data. The GetItems method returns a ListItemCollection type. This time we need to use the Foreach-object command to iterate through the list data.

PS > $spListItem | Foreach-object {

>> $_["Title"] = "New Value"; $_. Update ()

>>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.