In my work today, I need to write a script to create two thousand and one splistitems. The features are as follows:
1. The values of all fields of 1st items and 2001st items are the same except the title.
2. Two fields are the term in the managed metadata service.
3. A field is of the choice type.
The script has been written. After testing, it has been successfully run.
The script contains the following knowledge points:
1. How to Use powershell to obtain the term in Sharepoint. The answer is taxonomysession-> termstore-> term group-> termset-> term.
2. How to assign values to fields of choice type. The answer is surprisingly simple, and the strings are the same.
3. How to assign values to fields of the managed metadata term type. Pay attention to the use of the setfieldvalue method.
4. How to use two conditions in the IF statement to determine the condition? Pay attention to this line if ($ termset_country-EQ $ null)-Or ($ termset_company-EQ $ null ))
5. How to enable a non-SharePoint powershell prompt to run the SharePoint powershell cmdlet. Note that add-pssnapin Microsoft. Sharepoint. powershell-erroraction "silentlycontinue"
6. How to promptly dispose the objects that occupy resources such as spsite used in powershell. Pay attention to start-spassignment and stop-spassignment.
7. How to clear the screen in powershell. Pay attention to clear-host.
Clear-hostadd-pssnapin Microsoft. Sharepoint. powershell-erroraction "Silentlycontinue" # For object dispose $ spassignment = start-spassignment # Get termstore $ taxonomysession = Get-sptaxonomysession-site "Http: // sps2010 /" $ Termstore = $ taxonomysession. termstores [ "Managed metadata service application" ] $ Group = $ termstore. Groups [ "My group" ] If ($ Group-EQ $ Null ){Return } # Get termset $ termsetname_country = "Country" $ Termsetname_company = "Company" $ Termset_country = $ group. termsets | where-object {$ _. name-EQ $ termsetname_country} $ termset_company = $ group. termsets | where-object {$ _. name-EQ $ termsetname_company} If ($ Termset_country-EQ $ Null )-Or ($ termset_company-EQ $ Null )){ Return } # Get terms $ term_aus = $ termset_country.getallterms () | where-object {$ _. Name-EQ "Australia" } $ Term_chi = $ termset_country.getallterms () | where-object {$ _. Name-EQ "China" } $ Term_usa = $ termset_country.getallterms () | where-object {$ _. Name-EQ "USA" } $ Term_app = $ termset_company.getallterms () | where-object {$ _. Name-EQ "Apple" } $ Term_goo = $ termset_company.getallterms () | where-object {$ _. Name-EQ "Google" } $ Term_mic = $ termset_company.getallterms () | where-object {$ _. Name-EQ "Microsoft" } # Get list $ mylist = (get-spweb-identity http: // Sps2010-assignmentcollection $ spassignment). Lists ["term3"] # Create 1st listitem $ newitem1 = $ mylist. additem ("" , [Microsoft. Sharepoint. spfilesystemobjecttype]: file, $ Null ) $ Newitem1 [ "Title" ] = "Item 1" $ Newitem1 [ "Activity" ] = "Jump" $ Taxfield1 = [microsoft. Sharepoint. Taxonomy. taxonomyfield] $ newitem1.fields [ "Country" ] $ Taxfield1.setfieldvalue ($ newitem1, $ term_chi) $ taxfield2 = [microsoft. Sharepoint. Taxonomy. taxonomyfield] $ newitem1.fields [ "Companym" ] $ Taxfield2.setfieldvalue ($ newitem1, $ term_goo) $ newitem1.update (); # create 2nd ~ 2000th listitem For ($ L = 2; $ L-Le 2000; $ l ++) {$ newitem = $ mylist. additem ( "" , [Microsoft. Sharepoint. spfilesystemobjecttype]: file, $ Null ) $ Newitem [ "Title" ] = "Item $ l" $ Newitem [ "Activity" ] = "Run" $ Taxfield3 = [microsoft. Sharepoint. Taxonomy. taxonomyfield] $ newitem. Fields [ "Country" ] $ Taxfield3.setfieldvalue ($ newitem, $ term_aus) $ taxfield4 = [microsoft. Sharepoint. Taxonomy. taxonomyfield] $ newitem. Fields [ "Companym" ] $ Taxfield4.setfieldvalue ($ newitem, $ term_app) $ newitem. Update ()} # create 2001st list item $ newitem2001 = $ mylist. additem ( "" , [Microsoft. Sharepoint. spfilesystemobjecttype]: file, $ Null ) $ Newitem2001 [ "Title" ] = "Item 2001" $ Newitem2001 [ "Activity" ] = "Jump" $ Taxfield5 = [microsoft. Sharepoint. Taxonomy. taxonomyfield] $ newitem1.fields [ "Country" ] $ Taxfield5.setfieldvalue ($ newitem2001, $ term_chi) $ taxfield6 = [microsoft. Sharepoint. Taxonomy. taxonomyfield] $ newitem1.fields [ "Companym" ] $ Taxfield6.setfieldvalue ($ newitem2001, $ term_goo) $ newitem2001.update (); stop-spassignment $ spassignment
Running result
In addition, the testCodeWill try to delete all the list items that have been created. However, it is very slow to try to delete the list items. Finally, the whole list is deleted and re-launched, which will be faster.
References
========================================
Updating SharePoint managed metadata columns with powershell
Http://blogs.msdn.com/ B /kaevans/archive/2012/02/15/updating-sharepoint-managed-metadata-columns-with-powershell.aspx
Using the write-host cmdlet
Http://technet.microsoft.com/library/ee177031.aspx
Start-spassignment
Http://technet.microsoft.com/en-us/library/ff607664
Powershell script to delete all items in a list
Http://blogs.msdn.com/ B /spses/archive/2011/11/10/powershell-script-to-delete-all-items-in-a-list.aspx