C# 如果在数据库中插入新记录,如何更新SharePoint Online列表?
我有一个保存个人信息的MySQL数据库。每当新员工被雇佣时,他/她都会填写一些个人信息,并将这些信息存储在表格中 经过一些研究(由于我没有访问其他系统的权限,只能访问数据库),计划是构建一个C#console应用程序,检索数据并对照SharePoint列表进行检查。如果数据库中存在以前SharePoint列表中不存在的新记录,我希望更新列表(创建新项) 请注意,如果SharePoint列表包含更多列,则该表将包含其他手动信息 我已经针对数据库发布了连接代码,以及如何检索数据 如何检查SharePoint列表中是否存在该项目?是否有人能够提供一个答案,其中包括创建和插入新项目的代码?我有两列(在数据库和SP列表中)可以用作主键 有一个RESTAPI支持CRUD,所以我想这应该是一个很简单的问题 SharePoint列表:C# 如果在数据库中插入新记录,如何更新SharePoint Online列表?,c#,mysql,rest,sharepoint-online,sharepoint-designer,C#,Mysql,Rest,Sharepoint Online,Sharepoint Designer,我有一个保存个人信息的MySQL数据库。每当新员工被雇佣时,他/她都会填写一些个人信息,并将这些信息存储在表格中 经过一些研究(由于我没有访问其他系统的权限,只能访问数据库),计划是构建一个C#console应用程序,检索数据并对照SharePoint列表进行检查。如果数据库中存在以前SharePoint列表中不存在的新记录,我希望更新列表(创建新项) 请注意,如果SharePoint列表包含更多列,则该表将包含其他手动信息 我已经针对数据库发布了连接代码,以及如何检索数据 如何检查ShareP
然后我检索数据,如下所示:
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace daily_CC_SP_update
{
class Program
{
static void Main()
{
DbConnection mySQLConn = new DbConnection(dbName, serverAddress, pwd, userName);
mySQLConn.Connect();
string sqlQuery = "SELECT * FROM tbl_CC_SP";
MySqlCommand sqlCom = new MySqlCommand(sqlQuery, mySQLConn.getConnection());
MySqlDataReader reader = sqlCom.ExecuteReader();
Console.WriteLine("Following output from DB");
if(reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
//Keep the console alive until enter is pressed, for debugging
Console.Read();
mySQLConn.Close();
}
}
}
我将在数据库中创建一个视图来检索正确的数据。首先,我要澄清:)。。您正在prem上使用SharePoint,对吗?所以我们可以使用农场解决方案。
如果是的话,我会用休闲解决方案来解决这个问题。
我会开发一个SPJob(SharePoint定时器作业)。它只能包含在场解决方案中。基本上是这样的:
NetworkCredential _myCredentials = new NetworkCredential("user", "password", "companydomain");
- 可能会找到一篇很好的文章,其中有一些例子,说明如何做到这一点(我知道这篇文章是针对SP 2010的,但在2013年、2016年和2019年可能都是一样的(对于这个prem版本,我没有太多经验):)
- 具有相同解决方案的另一篇文章(此文章适用于SP 2013)
using (var clientContext = new ClientContext("url"))
{
CamlQuery camlQuery = new CamlQuery();
string query = "add some query here";
camlQuery.ViewXml = query;
collListItem = list.GetItems(camlQuery);
clientContext.Load(collListItem, items => items.Include( item => item["Title"], item => .... // add other columns You need here);
clientContext.ExecuteQuery();
if (collListItem.Count > 0)
{
// Your code here :)
}
}
还请注意,您可以使用不同用户(如某种管理员)的凭据运行CSOM,并提供如下网络凭据:
NetworkCredential _myCredentials = new NetworkCredential("user", "password", "companydomain");
另外,请注意阈值。。。在CSOM中,您始终可以使用分页查询,直到集合为空:)。
手动运行此控制台应用程序数次以确保其正常工作后,只需将此控制台应用程序作为任务库中的新任务添加到此服务器上的任务计划程序。此外,您还可以提供触发时间,如每小时或每天运行一次等。这是一篇关于如何添加此类任务的好文章
。。我希望现在的答案更适合您的环境:)因此我的c#程序取得了很大的进步。我使用MySql.datacsom在MySql数据库和SharePoint online之间建立了功能齐全的连接。可以操作和控制内部的所有列表和数据 但是我有一个问题,不确定这是否能解决。关于这个话题,我几乎找不到什么信息 我创建了一个新的ListItem。将所有字段设置为一个值。但是有一列是“Person”类型的。每个员工都有自己的网站,可以链接到这些网站,比如查找。向该字段添加值时,服务器会显示以下错误:
Microsoft.SharePoint.Client.ServerException: Invalid data has been used to update the list item. The field you are trying to update may be read only.
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at SPList.CreateNewItem(String userName, Int32 employeeNumber, String fullName, String firstName, String lastName, DateTime emplymentStart, DateTime employmentEnd, String department, String mobile, String address, String postcode, String postTown, String email) in C:\Users\fjs\source\repos\daily_CC_SP_update\SPList.cs:line 153
下面是我创建新项目的代码
using System;
using Microsoft.SharePoint.Client;
using System.Linq;
using System.Net;
public class SPList
{
private readonly ClientContext context;
private readonly List list;
private readonly ListItemCollection items;
private readonly Web web;
//Credentials may be needed, its commented out!
public SPList(String siteUrl, String listName, NetworkCredential netCred)
{
try
{
//NetworkCredential _myCredentials = netCred;
context = new ClientContext(siteUrl);
list = context.Web.Lists.GetByTitle(listName);
items = list.GetItems(CamlQuery.CreateAllItemsQuery());
web = context.Web;
context.Load(items);
context.Load(list);
context.Load(context.Web.Lists, lists => lists.Include(list => list.Title));
context.ExecuteQuery();
Console.WriteLine("Connected to SharePoint successfully!");
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
public void CreateNewItem(String userName, int employeeNumber, String fullName, String firstName, String lastName, DateTime emplymentStart, DateTime employmentEnd, String department, String mobile, String address, String postcode, String postTown, String email)
{
try
{
ListItemCreationInformation newItemSepc = new ListItemCreationInformation();
ListItem newItem = list.AddItem(newItemSepc);
newItem["Title"] = userName;
newItem["Employee_x0020_Number"] = employeeNumber;
newItem["Full_x0020_Name"] = fullName;
newItem["First_x0020_Name"] = firstName;
newItem["Last_x0020_Name"] = lastName;
newItem["_x000a_Employment_x0020_start_x0"] = emplymentStart.Date;
newItem["Employment_x0020_end_x0020_date"] = employmentEnd.Date;
newItem["Department"] = department;
newItem["Mobile"] = mobile;
newItem["Adress"] = address;
newItem["Postcode"] = postcode;
newItem["Post_x0020_town"] = postTown;
newItem["Email"] = email;
newItem["Person"] = fullName;
newItem.Update();
context.ExecuteQuery();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
如果我评论newItem[“Person”]=fullName;我们的代码运行良好这能以某种方式修复吗?否则,我必须在SharePoint中添加项目ID并添加值:/
奇怪的字段名称是因为SharePoint出于某种原因以这种方式存储它解决方案是将项[“LockUpColumn”]设置为锁定字段而不是字符串I未在premis上使用SP,而是在线,因此无法实现场解决方案。但你最后的建议与我之前的计划一致,所以米比,这是一条路。。。。对不起。。我从帖子上肯定这是预演的。。。请看我的在线方法更新帖子没问题,谢谢更新!我会关注这一点,然后带着我的解决方案回来!
Microsoft.SharePoint.Client.ServerException: Invalid data has been used to update the list item. The field you are trying to update may be read only.
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at SPList.CreateNewItem(String userName, Int32 employeeNumber, String fullName, String firstName, String lastName, DateTime emplymentStart, DateTime employmentEnd, String department, String mobile, String address, String postcode, String postTown, String email) in C:\Users\fjs\source\repos\daily_CC_SP_update\SPList.cs:line 153
using System;
using Microsoft.SharePoint.Client;
using System.Linq;
using System.Net;
public class SPList
{
private readonly ClientContext context;
private readonly List list;
private readonly ListItemCollection items;
private readonly Web web;
//Credentials may be needed, its commented out!
public SPList(String siteUrl, String listName, NetworkCredential netCred)
{
try
{
//NetworkCredential _myCredentials = netCred;
context = new ClientContext(siteUrl);
list = context.Web.Lists.GetByTitle(listName);
items = list.GetItems(CamlQuery.CreateAllItemsQuery());
web = context.Web;
context.Load(items);
context.Load(list);
context.Load(context.Web.Lists, lists => lists.Include(list => list.Title));
context.ExecuteQuery();
Console.WriteLine("Connected to SharePoint successfully!");
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
public void CreateNewItem(String userName, int employeeNumber, String fullName, String firstName, String lastName, DateTime emplymentStart, DateTime employmentEnd, String department, String mobile, String address, String postcode, String postTown, String email)
{
try
{
ListItemCreationInformation newItemSepc = new ListItemCreationInformation();
ListItem newItem = list.AddItem(newItemSepc);
newItem["Title"] = userName;
newItem["Employee_x0020_Number"] = employeeNumber;
newItem["Full_x0020_Name"] = fullName;
newItem["First_x0020_Name"] = firstName;
newItem["Last_x0020_Name"] = lastName;
newItem["_x000a_Employment_x0020_start_x0"] = emplymentStart.Date;
newItem["Employment_x0020_end_x0020_date"] = employmentEnd.Date;
newItem["Department"] = department;
newItem["Mobile"] = mobile;
newItem["Adress"] = address;
newItem["Postcode"] = postcode;
newItem["Post_x0020_town"] = postTown;
newItem["Email"] = email;
newItem["Person"] = fullName;
newItem.Update();
context.ExecuteQuery();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}