当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 创建警报

作者:精品下载站 日期:2024-12-14 14:46:18 浏览:12 分类:玩电脑

SharePoint Online:使用 PowerShell 创建警报


要求:使用 PowerShell 创建 SharePoint 警报。

如何在 SharePoint Online 中创建警报?

SharePoint Online 中的警报有助于跟踪内容的添加/更改/删除等更改,例如列表和库、页面等。您可以指定希望接收警报的条件以及希望接收的频率他们。要为 SharePoint Online 文档库创建警报,请执行以下步骤:

  1. 转到您的 SharePoint Online 网站,导航到要为其创建警报的文档库
  2. 在工具栏中,单击省略号并选择“提醒我”。

    [玩转系统] SharePoint Online:使用 PowerShell 创建警报

  3. 在新警报创建页面中,输入警报标题,输入将接收通知的用户,并为“发送方式”选择电子邮件或短信。选择此警报的条件(例如“任何更改”)以及发送警报的时间。

    [玩转系统] SharePoint Online:使用 PowerShell 创建警报

  4. 滚动到页面底部,然后单击“确定”为文档库创建警报。

现在,让我们使用 PowerShell 创建 SharePoint Online 警报!

SharePoint Online:CSOM PowerShell 创建警报

添加新项目时,我们希望使用 PowerShell 以编程方式将警报添加到 SharePoint Online 列表。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com"
$ListName="Documents"
$UserID="Salaudeen@crescent.com"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
   
    #Get the List and User Objects
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $User = $Ctx.Web.EnsureUser($UserID)
    $Ctx.Load($List)
    $Ctx.Load($User)
    $Ctx.ExecuteQuery()

    #Create new alert Object and Set its Properties
    $Alert = New-Object Microsoft.SharePoint.Client.AlertCreationInformation
    $Alert.List = $List
    $Alert.User = $User
    $Alert.Title = "New Documents in Project Docs - Alert"
    $Alert.AlertFrequency = [Microsoft.SharePoint.Client.AlertFrequency]::Immediate
    $Alert.AlertType = [Microsoft.SharePoint.Client.AlertType]::List
    $Alert.DeliveryChannels = [Microsoft.SharePoint.Client.AlertDeliveryChannel]::Email
    $Alert.Status = [Microsoft.SharePoint.Client.AlertStatus]::On
    $Alert.EventType = [Microsoft.SharePoint.Client.AlertEventType]::AddObject #Or All
    $Alert.Filter = "0" #Anything Changes - Other values: 1, 2, 3

    # Add the alert for the user
    $AlertGuid = $User.Alerts.Add($Alert)
    $User.Update()
    $Ctx.ExecuteQuery()

    Write-host -f Green "New Alert Has been Created!"
}
Catch {
    write-host -f Red "Error Creating Alert!" $_.Exception.Message
}

PnP PowerShell 在 SharePoint Online 中创建新警报

如果您想在不使用 UI 的情况下在 SharePoint Online 中创建新警报,PowerShell 就是答案!让我向您展示如何使用 PnP PowerShell cmdlet Add-PnPAlert 在 SharePoint Online 中创建新警报。


Add-PnPAlert -Url <List or Library URL> -User <User or Group Email> -DeliveryMethod <Delivery method> -Frequency <Frequency> -ChangeType <Change Type>

这是它需要的参数列表:

  • :您要为其创建警报的列表或库的 URL。
  • :您要为其创建警报的用户或组的电子邮件地址。
  • :您要创建的警报类型,例如“电子邮件”或“短信”。
  • :您希望接收警报的频率,例如“立即”或“每周”。
  • :您希望接收警报的更改类型,例如“全部”或“新”。

例如,要在网站 URL“https://crescent.sharepoint.com/sites/Branding”上创建“文档”警报,并向电子邮件地址为“Steve@Crescent.com”的用户发送每日电子邮件警报所有更改,您可以使用以下命令:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Branding"
$UserId= "i:0#.f|membership|Steve@crescent.com"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
 
    #Create an Alert
    Add-PnPAlert -Title "Documents Alert" -List "Documents" -User $UserId -Frequency Daily -ChangeType All
      
    Write-host "New Alert has been Created for the given user!" -f Green    
}
Catch {
    Write-host -f Red "Error:" $_.Exception.Message
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯