尝试使用引用 Microsoft.Sharepoint.Client 添加新列表项时出现问题
Issues when trying to add new list items using the reference Microsoft.Sharepoint.Client
我正在尝试使用 winforms C# 应用程序将新项目添加到 Sharepoint 列表中,但出现错误,好像该应用程序没有找到列表字段。
我的代码:
using SPC = Microsoft.SharePoint.Client;
(...)
string siteUrl = "https://sharepoint.company.com/sites/ProjectX";
SPC.ClientContext clientContext = new SPC.ClientContext(siteUrl);
string userName = "someone.surname";
SecureString password = new SecureString();
foreach (char c in "MyPaSsWoRd".ToCharArray()) password.AppendChar(c);
clientContext.Credentials = new NetworkCredential(userName, password, "MyDomain");
SPC.Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
SPC.List oList = web.Lists.GetByTitle("List Y");
clientContext.Load(oList);
clientContext.ExecuteQuery();
SPC.ListItemCreationInformation listCreationInformation = new SPC.ListItemCreationInformation();
SPC.ListItem oListItem = oList.AddItem(listCreationInformation);
oListItem["field 1"] = "a";
oListItem["field 2"] = "b";
oListItem["user"] = "someone.surname";
oListItem["date 1"] = "01/01/2015";
oListItem["field 3"] = "99";
oListItem.Update();
clientContext.ExecuteQuery();
最后一行代码出现错误:
Column 'field 1' does not exist. It may have been deleted by another
user. /sites/ProjectX/Lists/List_Y
有什么建议吗?
PS.: 是的,"List Y".
上有一个名为 "field 1" 的字段
谢谢!
我找到了问题原因:我使用的是字段的 "Display Name",但我需要改用 "Internal Name"。
所以我在列表设置中检查了字段的内部名称,然后我在这部分代码中使用了:
oListItem["field 1"] = "a";
oListItem["field 2"] = "b";
oListItem["user"] = "someone.surname";
oListItem["date 1"] = "01/01/2015";
oListItem["field 3"] = "99";
例如字段"field 1"的内部名称是"f1"; "field 2" 的内部名称是 "f2";字段 "date 1" 的内部名称是 "date_1";等等...
因此,当我将显示名称更改为内部名称(在 "oListItem["..."] 时,错误消息 "Column 'field 1' does not exist" 不再出现,因为字段现在找到了。
此时我必须做的其他更改是:
oListItem["user"] = "someone.surname";
这种方式不行(直接设置用户名)。我不得不使用变量来获取当前用户。这样:
oListItem["user"] = oUser.Id.ToString();
其中 "oUser" 变量是:
SPC.User oUser = oWeb.CurrentUser;
clientContext.Load(oUser);
clientContext.ExecuteQuery();
就是这样!现在好了:) 谢谢大家!
我正在尝试使用 winforms C# 应用程序将新项目添加到 Sharepoint 列表中,但出现错误,好像该应用程序没有找到列表字段。
我的代码:
using SPC = Microsoft.SharePoint.Client;
(...)
string siteUrl = "https://sharepoint.company.com/sites/ProjectX";
SPC.ClientContext clientContext = new SPC.ClientContext(siteUrl);
string userName = "someone.surname";
SecureString password = new SecureString();
foreach (char c in "MyPaSsWoRd".ToCharArray()) password.AppendChar(c);
clientContext.Credentials = new NetworkCredential(userName, password, "MyDomain");
SPC.Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
SPC.List oList = web.Lists.GetByTitle("List Y");
clientContext.Load(oList);
clientContext.ExecuteQuery();
SPC.ListItemCreationInformation listCreationInformation = new SPC.ListItemCreationInformation();
SPC.ListItem oListItem = oList.AddItem(listCreationInformation);
oListItem["field 1"] = "a";
oListItem["field 2"] = "b";
oListItem["user"] = "someone.surname";
oListItem["date 1"] = "01/01/2015";
oListItem["field 3"] = "99";
oListItem.Update();
clientContext.ExecuteQuery();
最后一行代码出现错误:
Column 'field 1' does not exist. It may have been deleted by another user. /sites/ProjectX/Lists/List_Y
有什么建议吗?
PS.: 是的,"List Y".
上有一个名为 "field 1" 的字段谢谢!
我找到了问题原因:我使用的是字段的 "Display Name",但我需要改用 "Internal Name"。
所以我在列表设置中检查了字段的内部名称,然后我在这部分代码中使用了:
oListItem["field 1"] = "a";
oListItem["field 2"] = "b";
oListItem["user"] = "someone.surname";
oListItem["date 1"] = "01/01/2015";
oListItem["field 3"] = "99";
例如字段"field 1"的内部名称是"f1"; "field 2" 的内部名称是 "f2";字段 "date 1" 的内部名称是 "date_1";等等...
因此,当我将显示名称更改为内部名称(在 "oListItem["..."] 时,错误消息 "Column 'field 1' does not exist" 不再出现,因为字段现在找到了。
此时我必须做的其他更改是:
oListItem["user"] = "someone.surname";
这种方式不行(直接设置用户名)。我不得不使用变量来获取当前用户。这样:
oListItem["user"] = oUser.Id.ToString();
其中 "oUser" 变量是:
SPC.User oUser = oWeb.CurrentUser;
clientContext.Load(oUser);
clientContext.ExecuteQuery();
就是这样!现在好了:) 谢谢大家!