powershell-sharepoint总结

本文介绍了如何使用PowerShell脚本在SharePoint中操作字段,特别是复制和更新查找字段。首先,文章强调了在运行SharePoint相关的PowerShell命令前需要引入的代码。接着,展示了声明参数、获取Web和列表的方法。然后,详细说明了如何创建新的查找字段,复制现有查找字段的附加字段,并将其添加到默认视图中。最后,提到了在处理依赖查找字段时的循环遍历和更新步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从的我博客文章可以看出我的涉猎范围比较广,没办法,打杂的就是一直做完这个立马就去学这个做,可是那个刚刚学着有点眉目了就要放弃研究另外一个东西,所以很多言论不成熟请见谅。在解决sharepoint的相关问题运行powershell之前 必须要运行这样的代码(类似于c#必须要引入namespace)

$ver = $host | select version if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"} Add-PsSnapin Microsoft.SharePoint.PowerShell Set-location $home或者这样一段代码 $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}if ($snapin -eq $null) { Add-PSSnapin "Microsoft.SharePoint.Powershell"} 

Powershell声明参数的方法Param( [parameter(Mandatory=$true, Position=1)] [alias("Web URL")] $webUrl, [parameter(Mandatory=$true, Position=1)] [alias("Name")] $StrName )

Powershell 获取webURLt的方法$web=Get-SPWeb $webUrl

获取list方法$listName=$web.Lists["Apple Store"]--Apple Store 是list的name

list 常用的几种属性$listName.DefaultView-----会把默认的defaultView都显示出来

$listName.DefaultView.ViewFields -- 输出列表中默认的column

在powershell脚本中如果要将一个类型转换为另外一个类型时需要用到as关键字比如C#代码:

 SPFieldLookup lookupField = field as SPFieldLookup可以这样转换为powershell脚本$lookupField=$field-as[Microsoft.SharePoint.SPFieldLookup]

例子:现在有个list里面有个column 是store,是个lookup字段。store有个附加字段Store Number现在我需要新建一列完全复制store列复制store列的列名为tempStore实现过程如下:

 Param( [parameter(Mandatory=$true, Position=1)] [alias("Web URL")] $webUrl ) # Add the PowerShell Snapin$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}if ($snapin -eq $null) { Add-PSSnapin "Microsoft.SharePoint.Powershell"} 

$web=Get-SPWeb $webUrl$relatedList = $web.Lists["app list two"] ###含有store 字段的list

$lookupList = $web.Lists["Apple Store"] ###store 这个lookup字段的关联的list是Apple Store

$relatedList.Fields.AddLookup("temp Store",$lookupList.ID,$true) ###添加一个关联了apple store list的lookup字段 

$relatedList.Update()

 $newField=$relatedList.Fields["temp Store"] ##获取新添加的字段并且关联Apple store 里面的store description 

$lookUpField=$lookupList.Fields["Store Description"] 

$newField.LookupField=$lookUpField.StaticName ###注意这里必须是staticName而不是title

$newField.Update() ###更新完字段之后,一定要update不然做的操作会出不来,我之前就一直疑惑明明代码是对的,结果一直不对,后来发现原来是没有做这步操作

做完以上操作之后会发现原来添加的column 没有添加到默认视图里面所以还必须把column添加进去

$relatedListDefaultView=$relatedList.DefaultView

 $relatedListDefaultView.ViewFields.Add($newField) 

 $relatedListDefaultView.Update() ###只要对list进行操作之后一定要update 基本上以上操作就已经完成了一大半,但是由于store 这个字段还有个附加字段所以我们必须也要给新加的temp Store 加上附加字段有时候一个字段可能不止有一个附加字段可能有许多所以我们必须要循环遍历出来

 $primaryCol=$relatedList.Fields["Store"]-as[Microsoft.SharePoint.SPFieldLookup]

 $dependentFieldsNames= $primaryCol.GetDependentLookupInternalNames() ###获取到所有的DependentLookupInternalNames

if($dependentFieldsNames.count-eq 0)

 write-host "No secondary columns"

}

else

 {

foreach($dependentFieldsName in $dependentFieldsNames){

$dependentField=$relatedList.Fields.GetFieldByInternalName($dependentFieldsName)

$dependentFieldTitle=$relatedList.Fields.GetFieldByInternalName($dependentFieldsName).Title

$tempDependentFieldTitle="temp"+$dependentFieldTitle

$tempDependentFieldInternalName=$relatedList.Fields.AddDependentLookup($tempDependentFieldTitle,$newField.Id) $tempDependentField = $relatedList.Fields.GetFieldByInternalName($tempDependentFieldInternalName)

$tempDependentField.LookupField = $lookupList.Fields[$tempDependentFieldTitle.Split(":"[1]].InternalName

$tempDependentField.Update() 

$relatedListDefaultView.ViewFields.Add($tempDependentField)

 $relatedListDefaultView.Update()

 }

}这样经过以上的代码就基本上实现了我们要实现的功能


下次再写怎样将store的值copy到temp store上面。

relevant url:https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.spfieldcollection.adddependentlookup.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值