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

[玩转系统] SharePoint Online:使用 PowerShell 添加日历项目

作者:精品下载站 日期:2024-12-14 15:34:19 浏览:13 分类:玩电脑

SharePoint Online:使用 PowerShell 添加日历项目


要求:使用 PowerShell 在 SharePoint Online 中添加日历事件。

[玩转系统] SharePoint Online:使用 PowerShell 添加日历项目

PowerShell 在 SharePoint Online 日历中添加新项目

PowerShell 是一种功能强大的脚本语言,可以自动执行 SharePoint Online 中的许多任务。让我们使用 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"

#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/"
$CalendarName = "Team Calendar"

Try {
    #Setup Credentials to connect
    $Cred= Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get the Calendar list
    $CalendarList = $Ctx.web.lists.GetByTitle($CalendarName)
    $Ctx.load($CalendarList)
    $Ctx.ExecuteQuery()

    #Add Calendar Item
    $ListCreationInfo = New-object Microsoft.SharePoint.Client.ListItemCreationInformation
    $ListItem = $CalendarList.AddItem($ListCreationInfo)
    $ListItem.ParseAndSetFieldValue("Title", "Townhall Meeting")
    $ListItem.ParseAndSetFieldValue("Description", "Global Townhall Meetings - Yearly schedule")
    $ListItem.ParseAndSetFieldValue("Location", "Ballroom")
    $ListItem.ParseAndSetFieldValue("EventDate", "01/01/2019 09:00 AM")
    $ListItem.ParseAndSetFieldValue("EndDate", "01/01/2019 06:00 PM")
    $ListItem.Update()
    $Ctx.ExecuteQuery()
    Write-host "Calendar Event Added Successfully!" -f Green
}
Catch {
    write-host -f Red "Error Adding Calendar Item!" $_.Exception.Message
}

要设置“全天事件”,请设置标志“fAllDayEvent”= $true;

PnP PowerShell 在 SharePoint Online 中添加日历事件

如果您正在寻找一种将事件快速添加到 SharePoint 在线日历的方法,PowerShell 可能是一个不错的选择!以下是向日历添加条目的 PnP PowerShell 版本:


#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/"
$CalendarName = "Team Calendar"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Add Event to Calendar
Add-PnPListItem -List $CalendarName -Values @{"Title" = "Team Meeting"; "Description"= "Monthly Team Meeting"; "Location"= "Ballroom"; "EventDate" = [datetime]"01/01/2019 8AM"; "EndDate"=[datetime]"01/01/2019 6PM";}

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

取消回复欢迎 发表评论:

关灯