You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

不使用Power Automate,如何基于SharePoint Online列表发送提醒邮件?

不使用Power Automate实现SharePoint Requests列表End Date后2天发送提醒邮件的方法

以下是几种可行的实现方案,均无需依赖Power Automate:

方案1:Azure Functions定时触发 + Graph API/SharePoint REST API

通过Azure云服务的定时任务触发逻辑,查询符合条件的列表项并发送邮件,适合生产环境长期稳定运行。

实施步骤:

  1. 创建定时器触发类型的Azure Function,设置触发频率(例如每天上午9点运行,表达式为0 0 9 * * *)。
  2. 在Function代码中完成以下逻辑:
    • 计算目标日期:当前日期往前推2天,筛选出End Date等于该日期的列表项。
    • 使用Graph API或SharePoint REST API查询Requests列表的符合条件项。
    • 调用Graph API发送提醒邮件给对应收件人(如请求创建者)。
  3. 配置Azure Function的应用权限:需授予Sites.ReadWrite.All(访问SharePoint列表)和Mail.Send(发送邮件)的应用权限。

C#代码示例:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System.Net.Http.Headers;
using System.Threading.Tasks;

public static class RequestReminderFunction
{
    [FunctionName("RequestReminderFunction")]
    public static async Task Run([TimerTrigger("0 0 9 * * *")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        // 配置站点和列表信息
        string siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite";
        string listName = "Requests";
        DateTime targetDate = DateTime.Today.AddDays(-2);
        string filterDate = targetDate.ToString("yyyy-MM-dd");

        // 获取Graph API访问令牌
        var clientId = "你的应用客户端ID";
        var clientSecret = "你的应用客户端密钥";
        var tenantId = "你的租户ID";
        var scopes = new[] { "https://graph.microsoft.com/.default" };

        var app = ConfidentialClientApplicationBuilder.Create(clientId)
            .WithClientSecret(clientSecret)
            .WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}"))
            .Build();

        var tokenResult = await app.AcquireTokenForClient(scopes).ExecuteAsync();
        var accessToken = tokenResult.AccessToken;

        // 初始化Graph客户端
        var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(request =>
        {
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            return Task.FromResult(0);
        }));

        // 查询符合条件的列表项
        var listItems = await graphClient.Sites[siteUrl.Replace("https://", "")].Lists[listName].Items
            .Request()
            .Filter($"EndDate eq {filterDate}") // 注意字段内部名称需与列表实际一致
            .Select("Title,Description,CreatedBy")
            .GetAsync();

        // 发送提醒邮件
        foreach (var item in listItems)
        {
            var title = item.Fields.AdditionalData["Title"]?.ToString();
            var description = item.Fields.AdditionalData["Description"]?.ToString();
            var creatorEmail = item.CreatedBy.User.Email;

            var mailMessage = new Message
            {
                Subject = $"提醒:请求「{title}」已结束2天",
                Body = new ItemBody
                {
                    ContentType = BodyType.Html,
                    Content = $"<p>请求 <strong>{title}</strong> 已于{filterDate}结束。<br><br>描述:{description}</p>"
                },
                ToRecipients = new[]
                {
                    new Recipient
                    {
                        EmailAddress = new EmailAddress { Address = creatorEmail }
                    }
                }
            };

            await graphClient.Users[creatorEmail].SendMail(mailMessage, true).Request().PostAsync();
            log.LogInformation($"已发送提醒邮件:{title}");
        }
    }
}

方案2:经典SharePoint工作流

如果你的租户支持经典工作流(部分新租户可能禁用),可以直接在列表内配置工作流实现定时提醒。

实施步骤:

  1. 进入Requests列表的设置 → 工作流设置,创建经典工作流。
  2. 设置触发条件:当项目被创建或修改时启动工作流。
  3. 添加暂停动作,设置暂停时长为「End Date字段值 + 2天」。
  4. 添加发送电子邮件动作,配置收件人(如请求创建者)、邮件主题和内容,插入列表项的字段值(如Title、Description)。
  5. 发布工作流,即可自动执行定时提醒逻辑。

方案3:PnP PowerShell脚本 + Azure Automation Runbook/Windows任务计划

通过脚本查询列表并发送邮件,配合自动化任务定时执行,适合轻量需求或临时场景。

实施步骤:

  1. 编写PnP PowerShell脚本,实现连接站点、筛选列表项、发送邮件的逻辑。
  2. 部署脚本到Azure Automation Runbook并设置定时触发(云端运行,无需本地服务器);或在本地服务器使用Windows任务计划定时运行(需保持服务器在线)。

PowerShell代码示例:

# 配置参数
$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
$listName = "Requests"
$targetDate = (Get-Date).AddDays(-2).ToString("yyyy-MM-dd")

# 连接到SharePoint Online
Connect-PnPOnline -Url $siteUrl -ClientId "你的应用客户端ID" -ClientSecret "你的应用客户端密钥"

# 查询符合条件的列表项
$query = @"
<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='EndDate'/>
                <Value Type='DateTime' IncludeTimeValue='FALSE'>$targetDate</Value>
            </Eq>
        </Where>
    </Query>
</View>
"@
$items = Get-PnPListItem -List $listName -Query $query

# 遍历发送邮件
foreach ($item in $items) {
    $title = $item["Title"]
    $description = $item["Description"]
    $creatorEmail = $item["CreatedBy"].Email

    # 使用Graph API发送邮件
    $graphToken = Get-PnPGraphAccessToken
    $mailBody = @{
        message = @{
            subject = "提醒:请求「$title」已结束2天"
            body = @{
                contentType = "HTML"
                content = "<p>请求 <strong>$title</strong> 已于$targetDate结束。<br><br>描述:$description</p>"
            }
            toRecipients = @(
                @{
                    emailAddress = @{ address = $creatorEmail }
                }
            )
        }
        saveToSentItems = $true
    } | ConvertTo-Json

    Invoke-RestMethod -Method Post -Uri "https://graph.microsoft.com/v1.0/users/$creatorEmail/sendMail" `
        -Headers @{ "Authorization" = "Bearer $graphToken"; "Content-Type" = "application/json" } `
        -Body $mailBody

    Write-Host "已发送提醒邮件:$title"
}

内容的提问来源于stack exchange,提问作者user20404265

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 02:35:12