internal static SPModel TryCreate(ISPListItemAdapter adapter, SPModelCollection parentCollection) { CommonHelper.ConfirmNotNull(adapter, "adapter"); CommonHelper.ConfirmNotNull(parentCollection, "parentCollection"); SPModelDescriptor exactType; try { exactType = SPModelDescriptor.Resolve(adapter.ContentTypeId); } catch (ArgumentException) { return(null); } if (exactType.ModelType.IsGenericType) { throw new InvalidOperationException(String.Format("Cannot create object from generic type '{0}'. Consider adding SPModelManagerDefaultTypeAttribute to the model class.", exactType.ModelType.FullName)); } try { calledByInternal = true; SPModel item = (SPModel)exactType.ModelInstanceType.CreateInstance(); item.Adapter = Intercept.ThroughProxy(adapter, new TransparentProxyInterceptor(), new[] { new SPListItemAdapterInterceptionBehavior(item, adapter, parentCollection) }); item.ParentCollection = parentCollection; return(item); } finally { calledByInternal = false; } }
public SPListItemAdapterInterceptionBehavior(SPModel item, ISPListItemAdapter adapter, SPModelCollection parentCollection) { CommonHelper.ConfirmNotNull(item, "item"); CommonHelper.ConfirmNotNull(adapter, "adapter"); CommonHelper.ConfirmNotNull(parentCollection, "parentCollection"); this.parentCollection = parentCollection; this.adapter = adapter; this.item = item; }
internal SPModelEventArgs(SPModelEventType eventType, SPModel model, ISPListItemAdapter previousAdapter, SPItemEventProperties properties) { CommonHelper.ConfirmNotNull(model, "model"); CommonHelper.ConfirmNotNull(properties, "properties"); this.properties = properties; this.previousAdapter = previousAdapter; this.manager = model.ParentCollection.Manager; this.eventType = eventType; }
void ISPModelEventHandler.HandleEvent(SPModel item, SPModelEventArgs e) { T typedItem = CommonHelper.TryCastOrDefault <T>(item); switch (e.EventType) { case SPModelEventType.Adding: OnAdding(typedItem, e); OnAddingOrUpdating(typedItem, e); return; case SPModelEventType.Added: OnAdded(typedItem, e); OnAddedOrUpdated(typedItem, e); return; case SPModelEventType.AddedAsync: OnAddedAsync(typedItem, e); OnAddedOrUpdatedAsync(typedItem, e); return; case SPModelEventType.Updating: OnUpdating(typedItem, e); OnAddingOrUpdating(typedItem, e); return; case SPModelEventType.Updated: OnUpdated(typedItem, e); OnAddedOrUpdated(typedItem, e); return; case SPModelEventType.UpdatedAsync: OnUpdatedAsync(typedItem, e); OnAddedOrUpdatedAsync(typedItem, e); return; case SPModelEventType.Deleting: OnDeleting(typedItem, e); return; case SPModelEventType.Deleted: OnDeleted(typedItem, e); return; case SPModelEventType.Publishing: OnPublishing(typedItem, e); return; case SPModelEventType.Published: OnPublished(typedItem, e); return; } }
protected void HandleEvent(SPItemEventProperties properties, SPModelEventType eventType) { using (new SPMonitoredScope(String.Format("SPModel Event Receiver ({0})", eventType))) { try { if (properties.AfterProperties != null && properties.BeforeProperties != null) { SPListItemCollection itemData = Interlocked.Exchange(ref previousItemData, null); ISPListItemAdapter adapter; ISPListItemAdapter previousAdapter; if (eventType == SPModelEventType.Deleted) { adapter = new SPPreviousEventDataCollectionAdapter(properties, itemData); previousAdapter = adapter; } else if (eventType == SPModelEventType.Adding || eventType == SPModelEventType.Updating || eventType == SPModelEventType.Deleting || eventType == SPModelEventType.Publishing) { adapter = new SPItemEventDataCollectionAdapter(properties); previousAdapter = eventType == SPModelEventType.Adding ? null : new SPListItemAdapter(properties.ListItem); } else { adapter = new SPListItemAdapter(properties.ListItem); previousAdapter = itemData != null ? new SPPreviousEventDataCollectionAdapter(properties, itemData) : adapter; } SPModel currentItem = SPModel.TryCreate(adapter); if (currentItem != null) { currentItem.HandleEvent(new SPModelEventArgs(eventType, currentItem, previousAdapter, properties)); if (eventType == SPModelEventType.Updating || eventType == SPModelEventType.Deleting) { previousItemData = properties.ListItem.ListItems; } } } } catch (Exception ex) { SPDiagnosticsService.Local.WriteTrace(TraceCategory.General, ex); if (HostingEnvironment.IsHosted) { properties.Status = SPEventReceiverStatus.CancelWithError; properties.ErrorMessage = ex.Message; } else { throw; } } } }
public static SPModelDescriptor Resolve(Type type) { CommonHelper.ConfirmNotNull(type, "type"); if (SPModel.IsDynamicConstructedType(type)) { type = type.BaseType; } else { RegisterAssembly(type.Assembly); if (type.IsGenericType) { type = type.GetGenericTypeDefinition(); } } SPModelDescriptor result; if (TargetTypeDictionary.TryGetValue(type, out result)) { return(result); } throw new ArgumentException("type", String.Format("Type '{0}' does not attributed with SPContentTypeAttribute", type.FullName)); }
internal IEnumerable <T> TryGetCachedModel <T>(ISPListItemAdapter source, string fieldName, params int[] lookupIds) { List <T> collection = new List <T>(); SPObjectCache cache = this.Manager.ObjectCache; SPFieldLookup lookupField = cache.GetField(source.WebId, source.ListId, fieldName) as SPFieldLookup; if (lookupField != null) { if (hashtable == null) { hashtable = new Hashtable(); } Guid listId = lookupField.LookupList == "Self" ? source.ListId : new Guid(lookupField.LookupList); List <int> lookupIdsToQuery = new List <int>(); foreach (int id in lookupIds) { LookupKey key = new LookupKey(listId, id); if (hashtable.ContainsKey(key)) { object cachedItem = hashtable[key]; if (cachedItem is T) { collection.Add((T)cachedItem); } } else { lookupIdsToQuery.Add(id); } } if (lookupIdsToQuery.Count > 0) { ISPModelManagerInternal manager = hashtable.EnsureKeyValue(typeof(T), () => (ISPModelManagerInternal)SPModel.GetDefaultManager(typeof(T), this.manager.Site.RootWeb, cache)); SPList list = cache.GetList(lookupField.LookupWebId, listId); SPQuery query = new SPQuery { Query = Caml.EqualsAny(SPBuiltInFieldName.ID, lookupIdsToQuery).ToString() }; foreach (SPListItem item in list.GetItems(query)) { object model = manager.TryCreateModel(new SPListItemAdapter(item, cache), false); hashtable[new LookupKey(listId, item.ID)] = model; if (model is T) { collection.Add((T)model); } cache.AddListItem(item); } } } return(collection); }
/// <summary> /// Gets the meta-data of the list item associated with an <see cref="SPModel"/> instance. /// </summary> public static ISPModelMetaData GetMetaData(this SPModel model) { return(model); }
private SPModelDescriptor(Type targetType, SPModelDefaultsAttribute defaultsAttribute) { this.ModelType = targetType; TargetTypeDictionary.TryAdd(targetType, this); TargetTypeDictionary.TryGetValue(targetType.BaseType, out this.Parent); if (this.Parent is SPModelInterfaceTypeDescriptor) { this.Parent = null; } this.contentTypeAttribute = targetType.GetCustomAttribute <SPContentTypeAttribute>(false); ResolveContentTypeId(contentTypeAttribute, targetType); ContentTypeDictionary.Add(contentTypeAttribute.ContentTypeId, this); this.defaultManagerType = GetDefaultManagerType(targetType); this.provisionEventReceiverType = contentTypeAttribute.ProvisionEventReceiverType; this.hasExplicitListAttribute = targetType.GetCustomAttribute <SPListAttribute>(false) != null; this.listAttribute = targetType.GetCustomAttribute <SPListAttribute>(true) ?? new SPListAttribute(); this.fieldAttributes = SPModelFieldAssociationCollection.EnumerateFieldAttributes(this, targetType).ToArray(); if (contentTypeAttribute.Group == null && defaultsAttribute != null) { contentTypeAttribute.Group = defaultsAttribute.DefaultContentTypeGroup; } foreach (SPFieldAttribute attribute in fieldAttributes) { if (attribute.Group == null) { if (this.Parent != null) { SPFieldAttribute baseAttribute = this.Parent.fieldAttributes.FirstOrDefault(v => v.InternalName == attribute.InternalName); if (baseAttribute != null) { attribute.Group = baseAttribute.Group; continue; } } if (defaultsAttribute != null) { attribute.Group = defaultsAttribute.DefaultFieldGroup; } } } if (contentTypeAttribute.ContentTypeId.IsChildOf(ContentTypeId.Page)) { this.ItemType = SPModelItemType.PublishingPage; } else if (contentTypeAttribute.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.DocumentSet)) { this.ItemType = SPModelItemType.DocumentSet; } else if (contentTypeAttribute.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Folder)) { this.ItemType = SPModelItemType.Folder; } else if (contentTypeAttribute.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Document)) { this.ItemType = SPModelItemType.File; } if (this.ItemType == SPModelItemType.GenericItem) { this.baseType = SPBaseType.GenericList; } else if (contentTypeAttribute.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Issue)) { this.baseType = SPBaseType.Issue; } else { this.baseType = SPBaseType.DocumentLibrary; } if (this.Parent != null) { this.Parent.Children.Add(this); this.fieldAttributes = fieldAttributes.Concat(this.Parent.fieldAttributes).Distinct().ToArray(); if (provisionEventReceiverType == null) { this.provisionEventReceiverType = this.Parent.provisionEventReceiverType; } } foreach (SPFieldAttribute v in fieldAttributes) { AddRequiredViewField(v); } foreach (Type interfaceType in targetType.GetInterfaces()) { if (!interfaceType.IsDefined(typeof(SPModelIgnoreAttribute), true)) { SPModelInterfaceTypeDescriptor interfaceDescriptor = (SPModelInterfaceTypeDescriptor)TargetTypeDictionary.EnsureKeyValue(interfaceType, SPModelInterfaceTypeDescriptor.Create); interfaceDescriptor.AddImplementedType(this); this.Interfaces.Add(interfaceDescriptor); } } if (targetType.BaseType != typeof(SPModel) && targetType.BaseType.GetCustomAttribute <SPContentTypeAttribute>(false) == null) { SPModelInterfaceTypeDescriptor interfaceDescriptor = (SPModelInterfaceTypeDescriptor)TargetTypeDictionary.EnsureKeyValue(targetType.BaseType, SPModelInterfaceTypeDescriptor.Create); interfaceDescriptor.AddImplementedType(this); this.Interfaces.Add(interfaceDescriptor); } if (!targetType.IsAbstract) { instanceType = new Lazy <Type>(() => targetType); } else { instanceType = new Lazy <Type>(() => SPModel.BuildTypeFromAbstractBaseType(targetType), LazyThreadSafetyMode.ExecutionAndPublication); } }