Powershell base operate about list

Source: Internet
Author: User

Let me see which List Templates Can be used first. We can use the listtemplates parameter in the spweb object to set the list template. First, let's initialize a spweb object.

PS> $ spweb = Get-spweb-identityhttp: // localhost

Then, we use the listtemplates parameter and the select-object parameter to view which List Templates can be used.

You can use these List Templates when creating a new list. 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 and description. The two parameters are of the system. string type, and a template parameter is of the splisttemplatetype type. Therefore, 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)

The add method is used to avoid using $ spweb. Lists. Add. If we use this method to create multiple consecutive lists, we will repeat the website's
The lists data will increase the memory consumption. If it is called once and saved in a variable, you only need to obtain the lists data once and can reuse it.

Update Sharepoint list using powershell

Next, let's modify the list we just created. First, we need to use the getlists () method to obtain this list.

PS> $ splist = $ spweb. getlist ("/lists/My
Contacts ")

PS> $ splist. onquicklaunch =
"True"
PS> $ splist. Update ()

After the above command is used, the link to the list will be displayed in the Quick Start of the website. If it is set to false, it will be hidden in the Quick Start. If we want to modify the description information of the list, we need to use the description parameter.

PS> $ splist. Description = "My
Contact List"
PS> $ splist. Update ()

Use powershell to add a column to the Sharepoint list

To add a new column to the list, we need to use the add method in the spfieldcollection class. Now we create a simple single line text column.

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 column name, and the second is the column type. Here we use a variable to save the column type of a single line of text, the third parameter is false, which indicates whether the newly created column must enter a value when saving the form, that is, the "require this column to contain information" option selected in the website creation column.

At the same time, Sharepoint 2010
Other types of columns are also supported. If you want to add a "option" type column, there will be some unnecessary, because it needs to set some available data. We can save the data used in the option bar to the system. Collections. Specialized. stringcollection class. Let's take a look at the following example.

PS> $ choices = New-ObjectSystem.Collections.Specialized.StringCollection

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

PS> $ choices. Add ("second choice ")

PS> $ choices. Add ("third choice ")

Now we can use this variable to create a column of the "choice" type.

PS> $ spfieldtype = [microsoft. Sharepoint. spfieldtype]: Choice

PS> $ splist. Fields. Add ("choicefield", $ spfieldtype, $ false, $ false, $ choices)

Use powershell to manage the List View

Let's continue to look at the view that uses powershell to manage the list. We can customize the view to display the data content we need. When we create a new list, there will be a default view. You can use powershell to edit the default view. You can use getviewfromurl to obtain the link of 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. We can use powershell to add this column to this default view. We need to use a variable to save the column.

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 and deleting views.

Use powershell to manage list projects

Let's continue to see how to add a list project to a list. The splists class provides an additem method. We can use this method to add a list project. When we use the additem method, it returns an object of Microsoft. Sharepoint. splistitem. Because we need to set data in each column for this list item.

PS> $ splistitem = $ splist. additem ()

Now we start to set its data for each column. The splistitem class provides a parameterized item attribute, through which we can set the value of a specific column. For example:

PS> $ splistitem ["title"] =
"New item"

PS> $ splistitem ["textfield"] =
"Hey hey"
PS> $ splistitem ["choicefield"] =
"First Choice"

PS> $ splistitem. Update ()

What should we do if we want to update an existing list project? The splist class provides many methods for you to find a list project. The most common imperative getitembyid ()
And getitems ()

The getitembyid () method is when we know this item
You can obtain the list items. However, in many cases, we do not know the ID of the list project, but only the title or other column information. At this time, we need to use the getitems () method. This method returns all list items or
List items obtained during query Definition

PS> $ spquery = New-ObjectMicrosoft.SharePoint.SPQuery

Spquery objects are classes used to support query queries. At this time, we can use the caml query statement,

PS> $ camlquery =

>>' <Where> <EQ> <fieldrefname = "title"
/> <Valuetype = "text"> true </value> </EQ> </where>'

PS> $ spquery. query = $ camlquery

After using the getitems method, we need to use some parameters to set the limit on the number of returned items. If we do not use the parameter with the limit on the number of rows, when obtaining a list of data with a large amount of data, it may fail.

PS> $ spquery. rowlimit = 100

PS> $ splistitem = $ splist. getitems ($ spquery)

Now we can edit the data obtained by using the getitems () method. The getitems method returns the type of a listitemcollection. In this case, we need to use the foreach-object command to obtain the list data cyclically.

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.