Power Shell 操作 SharePoint 常用脚本 1

Power Shell 操作 SharePoint 常用脚本 1

1. 使用网站模板新建站点
New-SPWeb -url $newWebUrl -name $webName -template "BLOG#0"

常用模板名称 http://www.funwithsharepoint.com/Lists/Posts/Post.aspx?ID=2, 也可以用Powershell命令查看当前farm可用的site模板: Get-SPWebTemplate


2. 向列表中添加图片列

$categoryList = $Web.Lists["Categories"]
$fldXml = "<Field Type='URL' DisplayName='Icon' Format='Image' Name='Icon'/>"
$categoryList.Fields.AddFieldAsXml($fldXml,$true,0)


3.清除列表内容

$collListItems = $categoryList.Items
$count = $collListItems.Count - 1
for($intIndex = $count; $intIndex -gt -1; $intIndex--) 
{
   write-host "Deleting record: " $intIndex 
   $collListItems.Delete($intIndex);
}

4.向列表中添加新条目

 $newItem = $categoryList.Items.Add()
   $newItem["Title"] = $title
   $newItem["Icon"] = $iconUrl
   $newItem.Update()

5. 添加Meta data service 列:

$TaxonomySession = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($siteCollection)
$Termstore = $TaxonomySession.TermStores["Managed Metadata Service"]
$TaxonomyGroup = $Termstore.Groups[$termGroupName]
$currentTermSet = $TaxonomyGroup.TermSets[$termSetName]
$metadataField = $postsList.Fields.CreateNewField("TaxonomyFieldType",$newColumnName)  
$metadataField.AllowMultipleValues = $false  
$categoryList.Fields.Add($metadataField)
$categoryList.Update()
$metadataField = $postsList.Fields[$newColumnName]
$metadataField.TermSetId = $currentTermSet.Id
$metadataField.AnchorId = "00000000-0000-0000-0000-000000000000"
$metadataField.SspId = $Termstore.Id
$metadataField.Update() 

6. 添加时间类型的列:

$fldXmlStickyExpireDate = "<Field Type='DateTime' DisplayName='Sticky Expire Date' Name='StickyExpireDate' Format='DateOnly' />"
$categoryList.Fields.AddFieldAsXml($fldXmlStickyExpireDate,$true,0)


7. 开启列表的like功能(SharePoint 2013)

write-host "Enable like for list" 
 $assembly=[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
 $reputationHelper =$assembly.GetType("Microsoft.SharePoint.Portal.ReputationHelper");
 
$bindings = @("EnableReputation", "NonPublic", "Static");
 [System.Reflection.BindingFlags]$flags = [System.Reflection.BindingFlags]::Static -bor [System.Reflection.BindingFlags]::NonPublic;
 
 $methodInfo = $reputationHelper.GetMethod("EnableReputation", $flags); 
 
 #for enabling Likes
 $values = @($categoryList, "Likes", $false);
 $methodInfo.Invoke($null, @($values));

8.创建文档库或列表

 $libraryName = "Attachments"
 $libraryDescription = "Storage for post attachments"
 $libraryTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary;
 $Web.Lists.Add($libraryName,$libraryDescription,$libraryTemplate);
 $Web.Update();

9.更新MaxQueryLookup 限制

 write-host "Update the List View Lookup Threshold for current web application"
 if($siteCollection.WebApplication.MaxQueryLookupFields -ne 1024)
		{
		   $siteCollection.WebApplication.MaxQueryLookupFields = 1024
		   $siteCollection.WebApplication.Update();
		}

10.隐藏列表中的某一列(非content type列)

$columnNeedToHide = $postsList.Fields[$cloumnName]
$columnNeedToHide.ShowInNewForm  = 0
$columnNeedToHide.ShowInDisplayForm = 0
$columnNeedToHide.ShowInViewForms  = 0
$columnNeedToHide.ShowInEditForm = 0
$columnNeedToHide.Update()  

11. 允许选择列可以有多个值

$choiceColumn = $List.Fields[$cloumnName]
$choiceColumn.AllowMultipleValues = $true
$choiceColumn.Update()

12. 共享父站点导航

write-host "Share parent site's navigation"
$SPPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
$AllowUnsafeUpdatesStatus = $SPWeb.AllowUnsafeUpdates
$Web.AllowUnsafeUpdates = $true
$SPPubWeb.Navigation.InheritCurrent = $true
$SPPubWeb.Navigation.ShowSiblings = $true
$SPPubWeb.Update()
$Web.AllowUnsafeUpdates = $AllowUnsafeUpdatesStatus

13.给列表设置权限

$rootWeb = $siteCollection.RootWeb
$spGroups = @("home Visitors", "home Members")
$userInfoList = $rootWeb.Lists["User Information List"]
$roles = @($rootWeb.RoleDefinitions["Contribute"], $rootWeb.RoleDefinitions["Read"])
$userInfoList.BreakRoleInheritance($true)
for($i=0; $i -lt $spGroups.length; $i++)
{
    for($j=0; $j -lt $roles.length; $j++)
       {
	        $role = $roles[j]
			$spGroup = $rootWeb.Groups[$spGroups[$i]]
			$assignment = New-Object Microsoft.SharePoint.SPRoleAssignment($spGroup)
			$assignment.RoleDefinitionBindings.Add($role)
			$userInfoList.RoleAssignments.Add($assignment)
	    }
}

$userInfoList.Update()
$rootWeb.Dispose()

14.隐藏contentType 列

write-host "Hide contentType column"
$contentType = $categoryList.ContentTypes[$contentTypeName]
$contentTypeColumn = $contentType.FieldLinks[$contentTypeColumnName]
$contentTypeColumn.Hidden = $true
$contentType.Update();
$categoryList.Update()

15. 更新服务帐号密码(在Windows中改完密码后再SharePoint power shell 内执行下列命令)
Set-SPManagedAccount -UseExistingPassword




你可能感兴趣的:(脚本,SharePoint,powershell,metadata,20132010)