友人を助けるために Powershell や Sharepoint を使用したことがないので、私の間違いは明らかである可能性があります。あらかじめお詫び申し上げます。
-すべてのユーザー プロファイルをループし、... -ネストされた foreach ループで既存のリストをループ -現在のユーザーの DisplayName を現在のリスト アイテムの「Employee」フィールドと比較します。一致する場合は、そのリスト アイテムのすべてのフィールドを更新します。-リスト全体をループして一致するものがない場合は、新しいリスト項目を追加します。
これは私が見つけたもので、私を大いに助けてくれました:
http://mysharepointwork.blogspot.no/2010/09/addupdatedelete-list-items-using.html
これが私のコードです:
#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$site = new-object Microsoft.SharePoint.SPSite("http://sp2016:85/");
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
#Get UserProfileManager from the My Site Host Site context
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$AllProfiles = $ProfileManager.GetEnumerator()
#Open SharePoint List
$SPServer="http://sp2016:85/"
$SPAppList="/Lists/UPList2/"
$spWeb = Get-SPWeb $SPServer
$spData = $spWeb.GetList($SPAppList)
foreach($profile in $AllProfiles)
{
#Add properties to this list item
$DisplayName = $profile.DisplayName
$Office = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::Office].Value
$WorkPhone = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::WorkPhone].Value
foreach($Items in $spData)
{
$SPItem = $spData.Items
if ($DisplayName -match $SPItem["Employee"] )
{
$SPItem["Office"] = $Office
$SPItem["Extension"] = $WorkPhone
$SPItem.Update()
{break}
}
}
#Create a new item
$newItem = $spData.Items.Add()
#Fill
$newItem["Employee"] = $DisplayName
$newItem["Office"] = $Office
$newItem["Extension"] = $WorkPhone
write-host "Profile for account ", $DisplayName
$newItem.Update()
}
write-host "Finished."
$site.Dispose()
- 新しいアイテムの追加部分は機能しているようですが、更新部分は機能していません。if ステートメントまたは 2 番目の for ループが問題だと思います。
より効率的に質問を投稿する方法について、すべてのヘルプおよび/またはフィードバックをお待ちしております。スタック Exchange ルーキー アラート。皆さん、ありがとうございました!