public virtual void Dispose() { if (ShouldDispose) { QueryWeb.Dispose(); } }
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 TList Ensure <TList>(Expression <Func <T, TList> > listSelector) { var visitor = new MemberAccessVisitor(); var selectedPropertyName = visitor.GetMemberName(listSelector); var selectedProperty = typeof(T).GetProperty(selectedPropertyName); if (selectedProperty == null) { throw new SharepointCommonException(string.Format("property {0} not found!", selectedPropertyName)); } var propertyType = selectedProperty.PropertyType; var listAttribute = (ListAttribute)Attribute.GetCustomAttribute(selectedProperty, typeof(ListAttribute)); if (listAttribute != null && listAttribute.Id != null) { throw new SharepointCommonException("Cannot ensure list with ID. It may cause mapping problems."); } if (!CommonHelper.ImplementsOpenGenericInterface(propertyType, typeof(IQueryList <>))) { throw new SharepointCommonException("Ensure not IQueryList<> not supported yet!"); } var args = propertyType.GetGenericArguments(); var isRepository = args.Length == 0; Type entityType; if (isRepository) { entityType = GetRepositoryGenericArgument(propertyType); } else { entityType = args[0]; } if (listAttribute != null && !string.IsNullOrEmpty(listAttribute.Name)) { if (QueryWeb.ExistsByName(listAttribute.Name)) { var qList = ((QueryWeb)QueryWeb).GetByName(entityType, listAttribute.Name); if (!isRepository) { // existing list return((TList)qList); } else { //existing repository var splistProp1 = qList.GetType().GetProperty("List"); var splist1 = splistProp1.GetValue(qList, null); return(CreateRepository <TList>(propertyType, splist1)); } } var list = ((QueryWeb)QueryWeb).Create(entityType, selectedPropertyName); var splist = list.GetType().GetProperty("List").GetValue(list, null); list.GetType().GetProperty("Title").SetValue(list, listAttribute.Name, null); if (isRepository) { //new repository return(CreateRepository <TList>(propertyType, splist)); } else { //new list return((TList)list); } } else if (listAttribute != null && listAttribute.Url != null) { var nameForCreateCorrectUrl = listAttribute.Url.Substring( listAttribute.Url.LastIndexOf("/", StringComparison.Ordinal) + 1); if (QueryWeb.ExistsByUrl(listAttribute.Url)) { var existingList = ((QueryWeb)QueryWeb).GetByUrl(entityType, listAttribute.Url); if (!isRepository) { // existing list return((TList)existingList); } else { // existing repository var splist1 = existingList.GetType().GetProperty("List").GetValue(existingList, null); return(CreateRepository <TList>(propertyType, splist1)); } } var list = ((QueryWeb)QueryWeb).Create(entityType, nameForCreateCorrectUrl); var splist = list.GetType().GetProperty("List").GetValue(list, null); list.GetType().GetProperty("Title").SetValue(list, selectedPropertyName, null); if (isRepository) { // new repository return(CreateRepository <TList>(propertyType, splist)); } else { // new list return((TList)list); } } throw new SharepointCommonException("default case."); }
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) { 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; } }