public void Intercept(IInvocation invocation) { if (invocation.Method.Name.StartsWith("set_")) { _changedFields.Add(invocation.Method.Name.Substring(4)); invocation.Proceed(); return; } if (!invocation.Method.Name.StartsWith("get_")) { Assert.Inconsistent(); } string propName = invocation.Method.Name.Substring(4); if (_changedFields.Contains(propName)) { invocation.Proceed(); return; } switch (invocation.Method.Name) { case "get_ParentList": var qWeb = new QueryWeb(_listItem.ParentList.ParentWeb); invocation.ReturnValue = qWeb.GetById <Item>(_listItem.ParentList.ID); return; case "get_ConcreteParentList": var qWeb1 = new QueryWeb(_listItem.ParentList.ParentWeb); invocation.ReturnValue = CommonHelper.MakeParentList(invocation.TargetType, qWeb1, _listItem.ParentList.ID); return; case "get_ListItem": invocation.ReturnValue = _listItem; return; case "get_Folder": string folderUrl = _listItem.Url; folderUrl = folderUrl.Replace(_listItem.ParentList.RootFolder.Url + "/", string.Empty); var linkFileName = (string)_listItem[SPBuiltInFieldId.LinkFilename]; folderUrl = folderUrl.Replace(linkFileName, string.Empty); invocation.ReturnValue = folderUrl.TrimEnd('/'); return; } var prop = invocation.TargetType.GetProperty(propName); if (CommonHelper.IsPropertyNotMapped(prop)) { // skip props with [NotField] attribute invocation.Proceed(); return; } var value = EntityMapper.ToEntityField(prop, _listItem); invocation.ReturnValue = value; }
public void Intercept(IInvocation invocation) { if (invocation.Method.Name.StartsWith("set_")) { var methodName = invocation.Method.Name.Substring(4); _changedFields.Add(methodName); invocation.Proceed(); return; } if (!invocation.Method.Name.StartsWith("get_")) { Assert.Inconsistent(); } string propName = invocation.Method.Name.Substring(4); if (_changedFields.Contains(propName)) { invocation.Proceed(); return; } switch (invocation.Method.Name) { case "get_Id": if (_itemId != 0) { invocation.ReturnValue = _itemId; return; } break; case "get_Title": if (!string.IsNullOrEmpty(_itemTitle)) //empty string in '..ing' event receivers { invocation.ReturnValue = _itemTitle; return; } break; case "get_ParentWeb": LoadListItem(); invocation.ReturnValue = new QueryWeb(_listItem.ParentList.ParentWeb); return; case "get_ParentList": LoadListItem(); var qWeb = new QueryWeb(_listItem.ParentList.ParentWeb); invocation.ReturnValue = qWeb.GetById <Item>(_listItem.ParentList.ID); return; case "get_ConcreteParentList": LoadListItem(); var qWeb1 = new QueryWeb(_listItem.ParentList.ParentWeb); invocation.ReturnValue = CommonHelper.MakeParentList(invocation.TargetType, qWeb1, _listItem.ParentList.ID); return; case "get_ListItem": LoadListItem(); invocation.ReturnValue = _listItem; return; case "get_Folder": LoadListItem(); string folderUrl = _listItem.Url; folderUrl = folderUrl.Replace(_listItem.ParentList.RootFolder.Url + "/", string.Empty); var linkFileName = (string)_listItem[SPBuiltInFieldId.LinkFilename]; folderUrl = folderUrl.Replace(linkFileName, string.Empty); invocation.ReturnValue = folderUrl.TrimEnd('/'); return; } var prop = invocation.TargetType.GetProperty(propName); if (CommonHelper.IsPropertyNotMapped(prop)) { // skip props with [NotField] attribute invocation.Proceed(); return; } LoadListItem(); var value = EntityMapper.ToEntityField(prop, _listItem, reloadLookupItem: _reloadLookupItem); if (value is Item) { // for deleted lookup item we need return null // it can be found by empty Title // see the test: Get_Deleted_Lookup_Value_Returns_Null_Test try { var t = ((Item)value).Title; } catch (NullReferenceException) { value = null; } } invocation.ReturnValue = value; }
public void Intercept(IInvocation invocation) { var propName = invocation.Method.Name.Substring(4); var prop = invocation.TargetType.GetProperty(propName); var fieldAttrs = prop.GetCustomAttributes(typeof(FieldAttribute), true); string spPropName; if (fieldAttrs.Length != 0) { spPropName = ((FieldAttribute)fieldAttrs[0]).Name ?? propName; } else { spPropName = FieldMapper.TranslateToFieldName(propName); } propName = spPropName; if (invocation.Method.Name.StartsWith("set_")) { throw new SharepointCommonException("Set operations on AfterProperties mapped item not implemented yet!"); } if (!invocation.Method.Name.StartsWith("get_")) { Assert.Inconsistent(); } switch (invocation.Method.Name) { case "get_ParentWeb": invocation.ReturnValue = new QueryWeb(_list.ParentWeb); return; case "get_ParentList": var qWeb = new QueryWeb(_list.ParentWeb); invocation.ReturnValue = qWeb.GetById <Item>(_list.ID); return; case "get_ConcreteParentList": var qWeb1 = new QueryWeb(_list.ParentWeb); invocation.ReturnValue = CommonHelper.MakeParentList(invocation.TargetType, qWeb1, _list.ID); return; case "get_ListItem": { invocation.ReturnValue = null; return; } case "get_Folder": { invocation.ReturnValue = null; return; } } if (CommonHelper.IsPropertyNotMapped(prop)) { // skip props with [NotField] attribute invocation.Proceed(); return; } if (_afterProperties.ContainsKey(propName)) { var field = _list.Fields.GetFieldByInternalName(propName); var val = EntityMapper.ToEntityField(prop, null, field: field, value: _afterProperties[propName], reloadLookupItem: true); invocation.ReturnValue = val; return; } else if (_afterProperties.ContainsKey("vti_" + propName.ToLower())) { var field = _list.Fields.GetFieldByInternalName(propName); var val = EntityMapper.ToEntityField(prop, null, field: field, value: _afterProperties["vti_" + propName.ToLower()], reloadLookupItem: true); invocation.ReturnValue = val; return; } else { var defaultValue = CommonHelper.GetDefaultValue(invocation.Method.ReturnType); invocation.ReturnValue = defaultValue; return; } }