用PowerShell 批量修改Lookup字段

2019-04-29  本文已影响0人  黑胡椒工厂

有人说Excel里面用惯了VBA的话,函数就记不住了,这个我深有同感,当然了,本来我的函数也就不怎么记得住。不过写过几个三脚猫的VBA之后呢, 就觉得有些本来可以用函数解决的也懒了,撸个VBA吧,不要再套来套去了。

SharePoint里面的一些管理也是,这个东西比Excel还要吃脚本一些,Excel 函数好歹还占个快,不信你试试用函数 lookup 比VBA手写查找要快很多的,更不用提透视表的神优化了。所以如果追求效率,另外不想限制文档类型的话,函数还是非常有用的。

但是SharePoint里面的一些操作就不一样了,网页版本来就操作起来疙疙瘩瘩的,然后还慢,所以能用PowerShell搞出来的就懒得用鼠标来点啦。

比如说更新list,一次更新上千个item是要死人的事。但是搞个脚本就只是等的事了。

但是呢,List 里面的column有一个类型是Lookup, 就有点麻烦了,String啊 Number 类型的只要简单的 

listitem["fieldname"] = "value" 

就可以了,但是Lookup的不行,会报一个类型错误。因为在SharePoint里面,这些lookup的字段是作为Object储存的。

不过呢,总是有办法的,既然是Object嘛,就可以抓Object,或者用更简单的办法使用一种特殊的格式:

listitem["lookupfieldname"] = "ID;#DisplayName"

这里的ID是这个字段在原来的list里面的ID,DisplayName就是显示的值,这样就简单的赋值给对应的lookup column的值了。

English Version:

Update lookup fields in SharePoint list by PowerShell

PowerShell is a powerful tool for manage SharePoint.

If you have a list with hundreds or even more thousands of items and you want to update all of them, you can use PowerShell, it will save lot of your time.

But if the field in the list is a lookup filed, things are a bit different.

If the field is a text or number field, you can update it directly by using listitem["fieldname"] = "value", very easy.

For the lookup field, you can't set value directly, you have two ways:

You can following this format for set value  listitem["lookupfieldname"] = "ID;#DisplayName". 

ID is the lookup value ID in its original list and display name is it's display name. So if you have a field lookup a value shows "Shanghai" and the id for "Shanghai" value is 15 you can set your code as listitem["lookupfieldname"] = "15;#Shanghai". Then you can update your lookup fields.

You can also use SharePoint object do this, but trust me, use "ID;#DisplayName" is the easiest way. You can create a hash table for helping you do it better.

上一篇下一篇

猜你喜欢

热点阅读