SharePoint 2013 Task Event : Create SubTask when new a master task

葛俊
2023-12-01

Note: I have a requirement about create a subtask and assigned to some person or group when user new a task.

here is the steps and code.

Create suabtask event:

        /// <summary>
        /// An item was added.
        /// </summary>public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            

            if (properties.ListItem["ParentID"] == null)
            {
                using (SPWeb oWebsite = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
                {
                    oWebsite.AllowUnsafeUpdates = true;
                    SPListItemCollection collItems = oWebsite.Lists[properties.ListTitle].Items;
                    SPListItem item = collItems.Add();
                    item["Title"] = "Sub-" + properties.ListItem["Title"];
                    item["StartDate"]= properties.ListItem["StartDate"];
                    item["DueDate"] = properties.ListItem["DueDate"];
                    item["Description"] = "success1";
                    //item["AssignedTo"] = oWebsite.EnsureUser("domain\\alias");
                    item["AssignedTo"] = oWebsite.SiteGroups["China"];
                    item["ParentID"] = properties.ListItemId;
                    item.Update();
                }
            }
        }

 

Update the subtask when the master is  changed

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
            
            using (SPWeb oWebsite = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
            {
                oWebsite.AllowUnsafeUpdates = true;
                SPListItemCollection collItems = oWebsite.Lists[properties.ListTitle].Items;
                foreach (SPListItem item in collItems) 
                {
                    if (item["ParentID"] != null)
                    {
                        if (item["ParentID"].ToString().Substring(0, item["ParentID"].ToString().IndexOf(";")) == properties.ListItemId.ToString())
                        {
                            item["Description"] = "success00";
                            item.Update();
                        }
                    }
                }
            }
        }

 

 

 类似资料:

相关阅读

相关文章

相关问答