public static void LockItem(SPListItem item) { if (!Records.IsLocked(item)) { Records.DeclareItemAsRecord(item); } }
/// <summary> /// 工作流已完成. /// </summary> public override void WorkflowCompleted(SPWorkflowEventProperties properties) { base.WorkflowCompleted(properties); if (properties.CompletionType != SPWorkflowEventCompletionType.Completed) { return; } SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite site = new SPSite(properties.WebUrl)) { string title = ""; try { using (SPWeb web = site.OpenWeb()) { SPWorkflow wf = new SPWorkflow(web, properties.InstanceId); SPList list = web.Lists[wf.ListId]; if (list.Title == "请假单") { SPListItem item = list.Items.GetItemById(wf.ItemId); title = item.Title; if (!Records.IsRecord(item)) { Records.DeclareItemAsRecord(item); log(site, "请假单流程结束", "消息", "请假单【" + title + "】流程结束,已经声明为记录。"); if (item["请假单审批流程"].ToString() == "16") // 通过了才扣年假,对吧。 { if (item["假别"].ToString() == "年假") { SPList annual_list = web.Lists["年假汇总"]; SPQuery query = new SPQuery(); DateTime date = DateTime.Parse(item["开始日期"].ToString()); string user_id = item["创建者"].ToString().Split(new char[] { ';', '#' })[0]; query.Query = "<Where><And><Eq><FieldRef Name='_x5e74__x4efd_'/><Value Type='Integer'>" + date.Year + "</Value></Eq><Eq><FieldRef Name='_x4eba__x5458_' LookupId='TRUE'/><Value Type='User'>" + user_id + "</Value></Eq></And></Where>"; SPListItemCollection annual_items = annual_list.GetItems(query); if (annual_items.Count > 0) { SPListItem annual_item = annual_items[0]; int annual_days_left = int.Parse(annual_item["剩余年假天数"].ToString()); int leave_days = int.Parse(item["请假天数"].ToString()); annual_item["剩余年假天数"] = annual_days_left - leave_days; annual_item.Update(); log(site, "年假汇总更新", "消息", "请假单【" + title + "】流程结束,对应的年假剩余天数已经更新。"); } } } } } } } catch (Exception ex) { log(site, "请假单流程结束", "错误", "请假单【" + title + "】流程结束,声明为记录时出错。" + ex.ToString()); } } }); }
/// <summary> /// Declares an SPListItem as a record /// </summary> /// <param name="item">The SPListItem to declare as a record</param> public static void LockItem(SPListItem item) { // Declare the item a record if it isn't already if (!Records.IsLocked(item)) { Records.DeclareItemAsRecord(item); } }
protected void declareRecordsButton_Click(object sender, EventArgs e) { //These integers are to count records and non-records int existingRecords = 0; int existingNonRecords = 0; //Get the list SPWeb currentWeb = SPContext.Current.Web; SPList list = currentWeb.Lists[listNameTextbox.Text]; if (list != null) { //Found a list, loop through the items foreach (SPListItem item in list.Items) { //Is this item already a record? Use the static Records.IsRecord method to find out if (Records.IsRecord(item)) { //This is already a record. Count it but take no action existingRecords++; } else { //This is not a record. existingNonRecords++; //Declare this as a record using the static Records.DeclareItemAsRecord method Records.DeclareItemAsRecord(item); } } //Tell the user what happened. resultsLabel.Text = String.Format("There were {0} records and {1} non-records. <br />", existingRecords, existingNonRecords); resultsLabel.Text += "All items are now declared as records."; } else { //Couldn't find the list resultsLabel.Text = "Could not find a list by that name"; } }