简体   繁体   中英

Adding a sub-task to a task in sharepoint online with c# (using Microsoft.SharePoint.Client)

I have managed to add tasks in Sharepoint online with c#, where I am struggling (maybe for the lack of the better search terms is the following):

How do I add a sub-task to an existing task/ assign a child task to a parent?

The answer seems to lay within the ParentID field, which shows a connection to another task, when entered manually online through the Sharepoint task list.

the field, in that case, shows for the child node the following entry:

-       [12]    {[ParentID, Microsoft.SharePoint.Client.FieldLookupValue]}&nbsp;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

I have the ID (eg 60) for the parent node to be and I understand the TypeID to be fix from research on the internet (hope my research on this is correct). I think the key might be the use of the KeyValuePair and assign the result to ParentID. This is my code on:

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();

The result I get or more precise the exception thrown is {"Unknown Error"}.

I am now clueless on where to turn to, or what to do next, do you have an idea? Is the idea of the KeyValuePair even the right one?

Sample code for your reference.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM