Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a model object representing the list item.
        /// </summary>
        /// <param name="adapter">A data access adapter of the list item.</param>
        /// <returns>A model object or *null* if there is no types associated with the content type of the list item.</returns>
        public static SPModel TryCreate(ISPListItemAdapter adapter)
        {
            CommonHelper.ConfirmNotNull(adapter, "adapter");
            SPContentTypeId contentTypeId;

            try {
                contentTypeId = adapter.ContentTypeId;
            } catch (MemberAccessException) {
                return(null);
            }
            SPModelDescriptor descriptor;

            try {
                descriptor = SPModelDescriptor.Resolve(contentTypeId, adapter.Site);
            } catch (ArgumentException) {
                return(null);
            }
            ISPModelManagerInternal manager = descriptor.CreateManager(adapter.Web);

            return(manager.TryCreateModel(adapter, false));
        }