我对 Sharepoint SOM 非常陌生,需要您对 Sharepoint 内部部署(2019 标准版)SPItemEventReceiver 的支持。 目标是在项目添加到列表后立即在 SQL DB 上执行更新或插入。我想将用户插入的值用于列出项目(来自属性)并发送到表格。 你可以在下面找到我的一段代码。它部署没有任何问题,但在调试时甚至不会返回错误。看起来根本没有打开连接。 是否允许在 EventReceiver 中执行这样的代码? 也许是一些安全问题?
using System;
using System.Text;
using System.Data.SqlClient;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace VATID_ListItem_Add.U_ER_VATID_ListItem_Add
{
public class U_ER_VATID_ListItem_Add : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
SPListItem currentItem = properties.ListItem;
string sqlserver = "dbserver01.contoso.com";
string sqldatabase = "CRM";
string sqltable = "DictIDTAX";
string sqlusername = "sp2sq";
string sqluserpwd = "Pa$$w0rd";
string usedb = string.Format("USE {0}; \n", sqldatabase);
SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder
{
DataSource = sqlserver,
UserID = sqlusername,
Password = sqluserpwd,
InitialCatalog = "CRM"
};
using (SqlConnection conn = new SqlConnection(sb.ConnectionString))
{
conn.Open();
StringBuilder sbin = new StringBuilder();
sbin.Append(usedb);
string cases = string.Format(
"if exists (select * from dbo.{0} where spID = '{1}') " +
"begin update dbo.{0} " +
"set company = {2}, taxid = {3}, email = {4} where spID = '{1}' " +
"end " +
"else begin " +
"insert into dbo.{0} (spID, company , taxid, email) " +
"VALUES ('{1}', '{3}', '{4}') end",
sqltable, currentItem["ID"], currentItem["Company Name"], currentItem["VAT ID"],
currentItem["EMAIL"]);
sbin.Append(cases);
String sql = sbin.ToString();
SqlCommand comm = new SqlCommand(sql, conn);
comm.ExecuteNonQuery();
conn.Close();
//base.ItemAdded(properties);
}
}
}
}