【问题标题】:Issues when trying to add new list items using the reference Microsoft.Sharepoint.Client尝试使用参考 Microsoft.Sharepoint.Client 添加新列表项时出现问题
【发布时间】:2015-03-11 17:43:32
【问题描述】:

我正在尝试使用 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();

最后一行代码出错:

列“字段 1”不存在。它可能已被其他人删除 用户。 /sites/ProjectX/Lists/List_Y

有什么建议吗?

PS.:是的,“列表 Y”上有一个名为“字段 1”的字段。

谢谢!

【问题讨论】:

  • 也许您应该为您的列表write out the field names,以确保“字段 1”存在于 oList 对象中。
  • 谢谢@wooters,但我已经这样做了,使用下面的行代码并且所有字段名称都可以:SPC.FieldCollection FLDlist = oList.Fields; clientContext.Load(FLDlist); clientContext.ExecuteQuery(); string mmmsg = string.Empty; foreach (SPC.Field fld in FLDlist) { mmmsg += fld.Title + Environment.NewLine; } MessageBox.Show(mmmsg);
  • 看看这个SO post,如果你还没有。
  • 对不起@wooters,但我不明白这一点:“sharepoint 询问你关于'信任'的问题,然后你必须选择你想要操作的列表”。我看不到任何来自共享点的“询问”或消息提示。
  • 是的,说实话,我也不完全理解。它看起来与您正在处理的错误直接相关。

标签: c# winforms sharepoint sharepoint-2013


【解决方案1】:

我找到了问题原因:我使用的是字段的“显示名称”,但我需要改用“内部名称”。

所以我在列表设置中检查了字段的内部名称,然后我在这部分代码中使用了:

oListItem["field 1"] = "a";
oListItem["field 2"] = "b";
oListItem["user"] = "someone.surname";
oListItem["date 1"] = "01/01/2015";
oListItem["field 3"] = "99";

例如,字段“field 1”的Internal Name为“f1”; “字段 2”的内部名称是“f2”;字段“date 1”的内部名称是“date_1”;等等……

因此,当我将显示名称更改为内部名称(在“oListItem[”...“] 处)时,不再出现错误消息“列 '字段 1' 不存在”,因为字段现已找到。

此时我必须做的其他更改是:

oListItem["user"] = "someone.surname";

这种方式行不通(直接设置用户名)。我不得不使用一个变量来获取当前用户。这样:

oListItem["user"] = oUser.Id.ToString();

“oUser”变量在哪里:

SPC.User oUser = oWeb.CurrentUser;
clientContext.Load(oUser);
clientContext.ExecuteQuery();

就是这样!现在没事了:)谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多