c# - 使用c#(使用Microsoft.SharePoint.Client)在线向sharepoint中的任务添加子任务
问题描述
我已经设法使用 c# 在 Sharepoint 中在线添加任务,我在其中苦苦挣扎(可能是因为缺少更好的搜索词如下):
如何将子任务添加到现有任务/将子任务分配给父任务?
当通过 Sharepoint 任务列表在线手动输入时,答案似乎位于 ParentID 字段中,该字段显示与另一个任务的连接。
在这种情况下,该字段为子节点显示以下条目:
- [12] {[ParentID, Microsoft.SharePoint.Client.FieldLookupValue]} System.Collections.Generic.KeyValuePair<string, object>
Key "ParentID" string
- Value {Microsoft.SharePoint.Client.FieldLookupValue} object {Microsoft.SharePoint.Client.FieldLookupValue}
LookupId 60 int
LookupValue "60" string
TypeId "{f1d34cc0-9b50-4a78-be78-d5facfcccfb7}" string
我有父节点的 ID(例如 60),并且我了解要通过互联网研究修复的 TypeID(希望我对此的研究是正确的)。我认为关键可能是使用 KeyValuePair 并将结果分配给 ParentID。这是我的代码:
var list = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("60", "f1d34cc0-9b50-4a78-be78-d5facfcccfb7"), };
listItem["Title"] = "do something";
listItem["ParentID"] = list;
listItem.Update();
context.ExecuteQuery();
我得到的结果或更准确地说是抛出的异常是{“未知错误”}。
我现在不知道该去哪里,或者下一步该做什么,你有什么想法吗?KeyValuePair 的想法是否正确?
解决方案
示例代码供您参考。
using (var clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = new SharePointOnlineCredentials(login, securePassword);
List myList = clientContext.Web.Lists.GetByTitle("MyTask");
ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();
ListItem newItem = myList.AddItem(listItemCreationInformation);
newItem["Title"] = "subTask";
newItem["Body"] = "body";
FieldLookupValue lookupParent = new FieldLookupValue();
//hardcode for test purpose
lookupParent.LookupId = 3;
newItem["ParentID"] = lookupParent;
newItem.Update();
clientContext.ExecuteQuery();
}