SharePoint 2013 Task Event : Create SubTask when new a master task
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(); } } } /// <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(); } } } } } /// <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(); } } } } }
补充:综合编程 , 其他综合 ,