/// <summary> /// Gets the property value. /// </summary> /// <param name="fieldName">Name of the field.</param> /// <param name="properties">The properties.</param> /// <returns></returns> private static object GetPropertyValue(string fieldName, SPItemEventProperties properties) { try { var value = properties.AfterProperties[fieldName]; if (value == null) { var listItem = SaveDataJobExecuteCache.GetListItem(properties); if (listItem != null) { if (listItem.Fields.ContainsFieldWithInternalName(fieldName)) { value = listItem[fieldName]; var list = SaveDataJobExecuteCache.GetList(properties); var spField = list.Fields.GetFieldByInternalName(fieldName); if (spField.Type == SPFieldType.DateTime) { value = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(value.ToString())); } } } } return(value ?? string.Empty); } catch { return(string.Empty); } }
public static SaveDataJobExecuteCache InitializeCache(SPSite site) { if (_cache != null) { throw new InvalidOperationException(); } _cache = new SaveDataJobExecuteCache(site); return(_cache); }
private static void GetOldPercentComplete(SPItemEventProperties properties, ref string oldPercent) { try { oldPercent = SaveDataJobExecuteCache.GetListItem(properties)["PercentComplete"].ToString(); } catch (Exception ex) { Trace.TraceError("Exception Suppressed {0}", ex); } }
private void processItem(SPItemEventProperties properties, bool isAdd) { // It should not execute if Status and PercentComplete is not changed otherwise it will revert the old values. if (!isAdd && !VerifyRequiredColumns(properties)) { return; } if (!VerifyHiddenItems(properties)) { return; } string newPercent = null; string newComplete = null; string newStatus = null; string oldPercent = null; string oldStatus = null; var method = string.Empty; GetPercentComplete(properties, ref newPercent); GetCompleteStatus(properties, ref newComplete); GetStatus(properties, ref newStatus); GetOldPercentComplete(properties, ref oldPercent); GetOldStatus(properties, ref oldStatus); SPSecurity.RunWithElevatedPrivileges( delegate { var site = SaveDataJobExecuteCache.GetSiteFromCache(properties.SiteId, true, () => new SPSite(properties.SiteId)); try { using (var web = site.OpenWeb(properties.Web.ID)) { ProcessMethod(properties, web, ref newPercent, out method); } } finally { SaveDataJobExecuteCache.DisposeSite(site); } }); UpdatePercent(properties, ref newPercent, newComplete, newStatus, oldPercent); UpdateStatus(properties, ref newStatus, newComplete, newPercent); UpdateComplete(ref newComplete, newStatus, newPercent); UpdateNewStatus(method, ref newStatus, oldStatus, oldPercent, ref newPercent); SetNewProperties(properties, newPercent, newComplete, newStatus); }
void IDisposable.Dispose() { foreach (var web in _websCache.Values) { web.Dispose(); } // NOTE: Site should be disposed by caller, only elevated site is disposed here if (_elevatedSite.IsValueCreated) { _elevatedSite.Value.Dispose(); } _cache = null; }
private static void ProcessMethod(SPItemEventProperties properties, SPWeb web, ref string newPercent, out string method) { method = ReflectionMethods.GetStatusMethod(web, properties.ListTitle); if (method == "1") { try { var workDone = float.Parse(properties.AfterProperties["ActualWork"].ToString()); var remainingWork = float.Parse(properties.AfterProperties["RemainingWork"].ToString()); newPercent = workDone == 0 && remainingWork == 0 ? "0" : (workDone / (remainingWork + workDone)).ToString(); } catch (Exception ex) { Trace.TraceError("Exception Suppressed {0}", ex); } } if (method == "2") { try { var Work = float.Parse(SaveDataJobExecuteCache.GetListItem(properties)["Work"].ToString()); var rWork = float.Parse(properties.AfterProperties["RemainingWork"].ToString()); if (Work == 0) { newPercent = "0"; } else { newPercent = ((Work - rWork) / Work).ToString(); } } catch (Exception ex) { Trace.TraceError("Exception Suppressed {0}", ex); } } }
private void UpdatePercent( SPItemEventProperties properties, ref string newPercent, string newComplete, string newStatus, string oldPercent) { if (newPercent == null) { var listItem = SaveDataJobExecuteCache.GetListItem(properties); if (newComplete != null) { if (newComplete == "1" || string.Equals(newComplete, "true", StringComparison.OrdinalIgnoreCase)) { newPercent = "1"; } else { newPercent = !string.IsNullOrWhiteSpace(newStatus) ? getPercentFromStatus(newStatus, oldPercent) : listItem["PercentComplete"] != null ? listItem["PercentComplete"].ToString() : "0"; } } else if (!string.IsNullOrWhiteSpace(newStatus)) { newPercent = getPercentFromStatus(newStatus, oldPercent); } else { try { newPercent = listItem["PercentComplete"].ToString(); } catch (Exception ex) { Trace.TraceError("Exception Suppressed {0}", ex); } } } }
private static void GetStatus(SPItemEventProperties properties, ref string status) { try { if (properties.AfterProperties["Status"] == null) { var list = SaveDataJobExecuteCache.GetList(properties); if (list.Fields["Status"].DefaultValue != null) { status = list.Fields["Status"].DefaultValue; } } else { status = properties.AfterProperties["Status"].ToString(); } } catch (Exception ex) { Trace.TraceError("Exception Suppressed {0}", ex); } }
private void UpdateStatus(SPItemEventProperties properties, ref string newStatus, string newComplete, string newPercent) { if (string.IsNullOrWhiteSpace(newStatus)) { if (newComplete != null) { const string CompleteFlag = "1"; if (newComplete == CompleteFlag || string.Equals(newComplete, bool.TrueString, StringComparison.OrdinalIgnoreCase)) { newStatus = "Completed"; } else if (newPercent != null) { newStatus = getStatusFromPercent(newPercent); } else { newStatus = "Not Started"; } } else if (newPercent != null) { newStatus = getStatusFromPercent(newPercent); } else { try { newStatus = SaveDataJobExecuteCache.GetListItem(properties)["Status"].ToString(); } catch (Exception ex) { Trace.TraceError("Exception Suppressed {0}", ex); } } } }
private static bool VerifyHiddenItems(SPItemEventProperties properties) { var allHidden = true; try { var list = SaveDataJobExecuteCache.GetList(properties); foreach (DictionaryEntry field in properties.AfterProperties) { if (!list.Fields.GetFieldByInternalName(field.Key.ToString()).Hidden) { allHidden = false; break; } } } catch (Exception ex) { Trace.TraceError("Exception Suppressed {0}", ex); allHidden = false; } return(!allHidden); }
/// <summary> /// Copies the schedule field value to PC field. /// </summary> /// <param name="properties">The properties.</param> private static void CopyScheduleFieldValueToPCField(SPItemEventProperties properties) { var list = SaveDataJobExecuteCache.GetList(properties); if (list.Fields.ContainsFieldWithInternalName("ProjectUpdate")) { var propertyValue = GetPropertyValue("ProjectUpdate", properties); //SKYVERA-455: if project update is schedule driven but project is closed, we should avoid automatic updates for project fields if (propertyValue != null && (!propertyValue.ToString().Equals(TypeScheduleDriven) || (list.Fields.ContainsFieldWithInternalName(StateFieldName) && properties.AfterProperties[StateFieldName].ToString().EndsWith(ClosedState, StringComparison.OrdinalIgnoreCase)))) { return; } foreach (SPField spField in list.Fields) { if (spField.InternalName.IndexOf("project", StringComparison.OrdinalIgnoreCase) < 0) { continue; } Match match = Regex.Match(spField.InternalName, @"^Project(.*?)$", RegexOptions.IgnoreCase); if (!match.Success) { continue; } string field = match.Groups[1].Value; if (!list.Fields.ContainsFieldWithInternalName(field)) { continue; } properties.AfterProperties[spField.InternalName] = GetPropertyValue(field, properties); } } else if (list.Fields.ContainsFieldWithInternalName("PortfolioUpdate")) { var propertyValue = GetPropertyValue("PortfolioUpdate", properties); if (propertyValue != null && !propertyValue.ToString().Equals("Schedule Driven")) { return; } foreach (SPField spField in list.Fields) { if (spField.InternalName.IndexOf("portfolio", StringComparison.OrdinalIgnoreCase) < 0) { continue; } Match match = Regex.Match(spField.InternalName, @"^Portfolio(.*?)$", RegexOptions.IgnoreCase); if (!match.Success) { continue; } string field = match.Groups[1].Value; if (!list.Fields.ContainsFieldWithInternalName(field)) { continue; } properties.AfterProperties[spField.InternalName] = GetPropertyValue(field, properties); } } }
public override void ItemUpdating(SPItemEventProperties properties) { if (properties.AfterProperties["ChildItem"] != null || properties.AfterProperties["ParentItem"] != null) { return; } bool bWorkspaceDriven = true; string pcGuid = ""; bool isCopyingToParent = false; var listItem = SaveDataJobExecuteCache.GetListItem(properties); try { pcGuid = listItem["ChildItem"].ToString(); } catch { } try{ pcGuid = listItem["ParentItem"].ToString(); isCopyingToParent = true; try { bWorkspaceDriven = bool.Parse(properties.AfterProperties["WorkspaceDriven"].ToString()); } catch { try { bWorkspaceDriven = bool.Parse(listItem["WorkspaceDriven"].ToString()); } catch { } } } catch { } if (pcGuid != "" && bWorkspaceDriven) { string[] itemInfo = pcGuid.Split('.'); try { Guid WebId = properties.Web.ID; SPWeb pWeb = properties.Web; Guid ListId = properties.ListId; int ListItemId = properties.ListItemId; SPList parentList = properties.List; ArrayList IgnoreFields = new ArrayList(); SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = properties.OpenSite()) { using (SPWeb web = site.OpenWeb(new Guid(itemInfo[0]))) { SPList list = web.Lists[new Guid(itemInfo[1])]; SPListItem li = list.GetItemById(int.Parse(itemInfo[2])); if (isCopyingToParent) { li["ChildItem"] = WebId + "." + ListId + "." + ListItemId; IgnoreFields = new ArrayList(CoreFunctions.getConfigSetting(web, "EPMLiveCPSyncIgnore").ToLower().Split(',')); } else { li["ParentItem"] = WebId + "." + ListId + "." + ListItemId; IgnoreFields = new ArrayList(CoreFunctions.getConfigSetting(pWeb, "EPMLiveCPSyncIgnore").ToLower().Split(',')); } foreach (SPField f in parentList.Fields) { if (!f.ReadOnlyField && f.Reorderable && !IgnoreFields.Contains(f.InternalName.ToLower())) { try { SPField parentField = list.Fields.GetFieldByInternalName(f.InternalName); if (properties.AfterProperties[f.InternalName] != null) { li[parentField.Id] = properties.AfterProperties[f.InternalName]; } else { if (properties.List.Fields.ContainsFieldWithInternalName(f.InternalName)) { try { li[parentField.Id] = listItem[f.Id]; } catch { } } } } catch { } } } li.SystemUpdate(); } } }); } catch { } } }
public override void ItemUpdating(SPItemEventProperties properties) { bool allHidden = true; try { var list = SaveDataJobExecuteCache.GetList(properties); foreach (System.Collections.DictionaryEntry sField in properties.AfterProperties) { if (!list.Fields.GetFieldByInternalName(sField.Key.ToString()).Hidden) { allHidden = false; break; } } } catch { allHidden = false; } if (properties.AfterProperties["Publisher_x0020_Approval_x0020_S"] != null) { return; } if (!allHidden && properties.AfterProperties["IsPublished"] == null) { properties.AfterProperties["IsPublished"] = "0"; try { SPFieldLookupValue lv = new SPFieldLookupValue(SaveDataJobExecuteCache.GetListItem(properties)["Project"].ToString()); SPField f = SaveDataJobExecuteCache.GetList(properties).Fields.GetFieldByInternalName("Project"); XmlDocument doc = new XmlDocument(); doc.LoadXml(f.SchemaXml); string listid = doc.FirstChild.Attributes["List"].Value; var web = properties.Web; SPList list = web.Lists[new Guid(listid)]; SPListItem li = list.GetItemById(lv.LookupId); if (list.Fields.ContainsFieldWithInternalName("PendingUpdates")) { SPField fPend = list.Fields.GetFieldByInternalName("PendingUpdates"); string pending = ""; try { pending = fPend.GetFieldValueAsText(li[fPend.Id].ToString()); }catch {} if (pending != "Yes") { li[fPend.Id] = 1; li.SystemUpdate(); ArrayList arr = new ArrayList(); SPFieldUserValue uv = new SPFieldUserValue(web, li["Author"].ToString()); arr.Add(uv.LookupId); try { SPFieldUserValueCollection uvc = new SPFieldUserValueCollection(web, li["Planners"].ToString()); foreach (SPFieldUserValue u in uvc) { if (!arr.Contains(u.LookupId)) { arr.Add(u.LookupId); } } }catch {} try { SPFieldUserValueCollection uvc = new SPFieldUserValueCollection(web, li["Project Manager"].ToString()); foreach (SPFieldUserValue u in uvc) { if (!arr.Contains(u.LookupId)) { arr.Add(u.LookupId); } } } catch { } Hashtable hshProps = new Hashtable(); hshProps.Add("ProjectName", li.Title); hshProps.Add("ListId", list.ID.ToString()); hshProps.Add("ItemId", li.ID.ToString()); //string body = Properties.Resources.txtPendingUpdateEmail; //body = body.Replace("{ProjectName}", li.Title); //body = body.Replace("{ListId}", list.ID.ToString()); //body = body.Replace("{ItemId}", li.ID.ToString()); foreach (int i in arr) { try { //SPSecurity.RunWithElevatedPrivileges(delegate() //{ SPUser u = web.SiteUsers.GetByID(1073741823); API.APIEmail.QueueItemMessage(5, true, hshProps, new string[] { i.ToString() }, null, false, true, li, u, false); //}); //API.APIEmail.sendEmailHideReply(5, properties.SiteId, properties.Web.ID, properties.Web.CurrentUser, properties.Web.SiteUsers.GetByID(i), hshProps); } catch { } } } } } catch { } } }