Пример #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);
        }
Пример #2
0
 public SPModelUsage EnsureList(SPObjectCache objectCache)
 {
     CommonHelper.ConfirmNotNull(objectCache, "objectCache");
     if (this.List != null || this.ListId == Guid.Empty)
     {
         return(this);
     }
     try {
         using (new SPSecurity.SuppressAccessDeniedRedirectInScope()) {
             SPList list = objectCache.GetList(this.WebId, this.ListId);
             if (list != null)
             {
                 return(new SPModelUsage(list, this.ContentTypeId, true));
             }
         }
     } catch { }
     return(this);
 }