Creating Lists in SharePoint 2010
One of the most powerful features of SharePoint from an end user's perspective is the degree of creating and customizing lists, views, and items. lists store data. sharePoint 2010 nodes des a large number of list templates that can be used as they are or as a starting point to tailor them to fulfill your specific business requirements. lists have a set of configurable settings that apply to all lists, as well as custom settings that apply only to the specific type of list used.
Let's take a look at the templates available. We can do this usingListTemplatesProperty on an object of the typeSPWeb. First, we store an object of the typeSPWebIn a variable:
PS> $ spWeb = Get-SPWeb-Identity http: // SPServer
Next, we useListTemplatesProperty and select the name and description, as shown in the following image.
We can use a template when creating a new list (of course we'll do this by using Windows PowerShell). When creating a list in SharePoint 2010, you useAddMethod ofSPListCollectionClass. This method has seven overloads (method signatures, or ways in which the method can be called). The one we will be using for our example accepts three parameters: the listTitle,Description, AndTemplateType to be used.TitleAndDescriptionParameters are both of the typeSystem. String, AndTemplateParameter is of the typeSPListTemplateType, So we need to provide an instance of this type as a value for this parameter.
Here, we create a variable containingContactsList template type, which we obtain fromMicrosoft. SharePoint. SPListTemplateTypeEnumeration:
PS> $ listTemplate = [Microsoft. SharePoint. SPListTemplateType]: Contacts
PS> $ spListCollection = $ spWeb. Lists
PS> $ spListCollection. Add ("My Contacts", "Description", $ listTemplate)
Why do we need this intermediarySpListCollectionVariable? Why not callAddMethod directly$ SpWeb. Lists. Add? If we usedAddMethod directly and repeated the command ten times, the metadata for all available lists in the site wocould be loaded ten times. StoringListsCollection in a variable and working with that variable minimizes the amount of memory consumed, because the lists are loaded only once.
Modifying Lists in SharePoint 2010
Let's see how we can modify our new list. First, we get the list usingGetLists ()Method and store it in a variable:
PS> $ spList = $ spWeb. GetList ("/Lists/My Contacts ")
With the list stored in a variable, we can go ahead and set all kinds of cool stuff. Here's how to add a list to the Quick Launch:
PS> $ spList. OnQuickLaunch = "True"
PS> $ spList. Update ()
When you run the command above, the list appears on the Quick Launch. Setting the valueFalseHides the list from the Quick Launch. If we want to change the list's description, we can simply useDescriptionProperty:
PS> $ spList. Description = "My Contact List"
PS> $ spList. Update ()
Adding List Fields
It's also possible to add additional fields to a list. To create a field in a SharePoint 2010 list, useAddMethod provided bySPFieldCollectionClass. Here, we create a simpleTextType field in a SharePoint list:
PS> $ spFieldType = [Microsoft. SharePoint. SPFieldType]: Text
PS> $ spList. Fields. Add ("TextField", $ spFieldType, $ false)
In this example, we createSPFieldTypeObject with the valueTextAnd store it in the variableSpFieldType. We then useAddMethod and pass in the field's display name, followed by the variableSpFieldType, Followed by BooleanFalse. The last parameter in this overload ofAddMethod specifies whether the new field is required to always contain a value. Our example creates a new text field in the list with the display nameTextFieldThat will not require any input. An additional Boolean parameter you can use withAddMethod compacts the field name to eight characters, if setTrue.
SharePoint 2010 supports other types of fields as well. adding a Choice field is a little different since it requires additional information regarding the possible choices. you can store the choices in an instance of the System. collections. specialized. stringCollection class, as shown in this example:
PS> $ choices = New-Object System. Collections. Specialized. StringCollection
PS> $ choices. Add ("First Choice ")
PS> $ choices. Add ("Second Choice ")
PS> $ choices. Add ("Third Choice ")
Now that we have our choices stored in a variable, we can use the variable when creatingChoiceType field:
PS> $ spFieldType = [Microsoft. SharePoint. SPFieldType]: Choice
PS> $ spList. Fields. Add ("ChoiceField", $ spFieldType, $ false, $ false, $ choices)
We useChoicesVariable to associate a list of options with the field.
Managing List Views
Let's move on and look at how to manage list views in SharePoint 2010. views enable you to create customized representations of list data for specific purposes, such as displaying specific fields. when a new list in SharePoint is created, a default view is added. document Library lists get the All Documents view, Picture Library lists get the All Pictures view, and most of the other list types get the All Items view.
Before we can edit an existing view in SharePoint 2010, we need to retrieve it. We can useGetViewFromUrlMethod ofSPWebClass. Let's getContactsList we created earlier:
PS> $ spView = $ spWeb. GetViewFromUrl ("/Lists/My Contacts/AllItems. aspx ")
When new fields are created, they are not added to a view by default. we can, of course, add a field to a view by using Windows PowerShell. first, we need to create a reference to the field that we want to add to the view:
PS> $ spField = $ spList. Fields ["TextField"]
We then can useAddMethod provided bySPViewFieldCollectionClass to add the field to a view and finally useUpdate ()Method to set the changes:
PS> $ spView. ViewFields. Add ($ spField)
PS> $ spView. Update ()
You can do a lot more cool stuff with views such as set custom queries, create new views, and remove views.
Managing List Items
Let's move ahead and take a look at how to add list items to a list.SPListClass providesAddItemMethod, which is used to create new list items. When you callAddItemMethod, an object of the typeMicrosoft. SharePoint. SPListItemIs returned. Because we want to populate the properties of the new list item, we need to store the object in a variable in order to continue to work with it:
PS> $ spListItem = $ spList. AddItem ()
Now we can start assigning values to the different fields the corresponding list item inherits from the parent list.SPListItemClass provides a parameterizedItemProperty for accessing the value contained in a participant field. To specify the value ofTitleField, we can use the following:
PS> $ spListItem ["Title"] = "New Item"
We can assign additional values to fields as well. In the next example, we add values toTextFieldAndChoiceField, Which we created earlier in this example:
PS> $ spListItem ["TextField"] = "Hey hey"
PS> $ spListItem ["ChoiceField"] = "First Choice"
Finally, we useUpdate ()Method to create the new list item:
PS> $ spListItem. Update ()
What if we want to update an existing list item? Well,SPListClass provides a few methods we can use to retrieve a specific from a list. The most common methods areGetItemById ()AndGetItems ().
Wait a minute! Isn' t it simpler to loop throughItemsCollection and create a filter usingWhere-ObjectCmdlet? It is simpler and does feel like the Windows PowerShell way of doing things, but consider this: When you useItemsProperty onSPListObject, all the list items in the list are read into memory, meaning that large lists may consume a lot of memory. A better approach is to use eitherGetItemsById ()Method orGetItems ()Method to minimize memory consumption.
TheGetItemById ()Method requires that we know the ID of the particle item. in our cases, we have no idea of an item's ID, but we might know the item's title or some other value. this is whereGetItems ()Method becomes useful. The method returns all list items or a subset of list items as defined by search criteria in a CAML query.
To use a CAML query withGetItemsMethod, we first need to create an object of the typeMicrosoft. SharePoint. SPQuery:
PS> $ spQuery = New-Object Microsoft. SharePoint. SPQuery
TheSPQueryObject supportsQueryProperty, which we use to place a CAML query. Here's the CAML query we'll use in our example:
PS> $ camlQuery =
>>' <Where> <Eq> <FieldRef Name = "Title"/> <Value Type = "Text"> True </Value> </Eq> </Where>'
We create a new query containingWhereStatement using<Where>Tag. Next, we specify an equals expression using<Eq>Tag. For other types of searches, you can replace this tag with the appropriate one, such<Lt>Or<Gt>To search for list items where the value of this field is less than or greater than value, respectively.
We then specify the field we want to query against using<FieldRef>Tag. In this example, we want to look atTitleField in the list. Finally, we use<Value>Tag to specify that the value type isTextAnd that the value shoshould equalNew Item.
After we have created a CAML query and stored it in a variable, we assign it toQueryProperty of ourSPQueryObject:
PS> $ spQuery. Query = $ camlQuery
Before usingSPQueryWithGetItemsMethod, we shoshould specifyRowLimitProperty that is used to limit the amount of items returned per page. If we runSPQueryWithout setting the row limit, the query will select all items matching the criteria and might fail on lists with a large number of items:
PS> $ spQuery. RowLimit = 100
Here, we setRowLimitProperty to 100 so that only 100 list items are returned per page.RowLimitValue shocould be between 1 and 2000. Finally, we can callGetItems ()Method withSPQueryObject instance we created earlier for input:
PS> $ spListItem = $ spList. GetItems ($ spQuery)
Now it's a simple task to update the list item. In the example below, we change the item'sTitle. Note thatGetItems ()Method returnsListItemCollection, So even if only one value is returned, we still have to index the first element or useForEach-ObjectCmdlet to loop through each item:
PS> $ spListItemCollection | ForEach-Object {
>>$ _ ["Title"] = "New Value"; $ _. Update ()
>>}
Summary
In this post, we 've looked at how to manage lists, views, and items by using Windows PowerShell. we saw examples about how to create new lists, modify list properties, add fields, manage views, and how to work with items. chapter 14 inPowerShell for SharePoint 2010 Administrators(Available October 2010) takes list and view management one step further by showing how to manage lookup fields, how to retrieve lists using a simple function, how to automate list creation, and much more. in Chapter 15, you'll see detailed examples about how to create, manage, delete, and copy list Items.
NG, that is all there is to manage lists, views, and items by using Windows PowerShell and SharePoint. Guest Blogger Week with Niklas Goude and SharePoint will continue tomorrow.
We wocould love for you to follow us on Twitter and Facebook. if you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. see you tomorrow. until then, peace.
Http://blogs.technet.com/ B /heyscriptingguy/archive/2010/09/22/use-powershell-to-manage-lists-views-and-items-in-sharepoint.aspx