예제 #1
0
        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;
            }
        }
예제 #2
0
 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;
 }
예제 #3
0
 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;
 }
예제 #4
0
 /// <summary>
 /// Removes extra white spaces in rich-text fields.
 /// </summary>
 /// <param name="adapter">Data access adapter of the list item.</param>
 public static void FixRichTextFieldWhiteSpaces(ISPListItemAdapter adapter)
 {
     CommonHelper.ConfirmNotNull(adapter, "adapter");
     foreach (BaseRichFieldType field in adapter.ListItem.Fields.OfType <BaseRichFieldType>())
     {
         object value = adapter.GetString(field.InternalName);
         if (value != null)
         {
             adapter.SetString(field.InternalName, FixWhiteSpaces(value.ToString()));
         }
     }
 }
예제 #5
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);
        }
예제 #6
0
 /// <summary>
 /// Removes extra white spaces in rich-text web parts.
 /// </summary>
 /// <param name="adapter">Data access adapter of the list item.</param>
 public static void FixRichTextWebPartWhiteSpaces(ISPListItemAdapter adapter)
 {
     CommonHelper.ConfirmNotNull(adapter, "adapter");
     using (SPLimitedWebPartManager wpm = adapter.ListItem.File.GetLimitedWebPartManager(PersonalizationScope.Shared)) {
         List <ContentEditorWebPart> contentWebParts = wpm.WebParts.OfType <ContentEditorWebPart>().ToList();
         foreach (ContentEditorWebPart contentWebpart in contentWebParts)
         {
             if (contentWebpart.Content != null)
             {
                 try {
                     string      resultXml = FixWhiteSpaces(contentWebpart.Content.OuterXml);
                     XmlDocument resultDoc = new XmlDocument();
                     resultDoc.LoadXml(resultXml);
                     contentWebpart.Content = resultDoc.DocumentElement;
                     wpm.SaveChanges(contentWebpart);
                 } catch {
                 }
             }
         }
     }
 }
예제 #7
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));
        }
예제 #8
0
 /// <summary>
 /// Approves and publishes asset referred by the specified URL.
 /// </summary>
 /// <param name="assetUrl">URL to the asset.</param>
 /// <param name="adapter">Data access adapter of the list item.</param>
 public static void PublishRelatedAsset(string assetUrl, ISPListItemAdapter adapter)
 {
     CommonHelper.ConfirmNotNull(assetUrl, "assetUrl");
     CommonHelper.ConfirmNotNull(adapter, "adapter");
     PublishAssetInternal(assetUrl, adapter.ListItem);
 }
예제 #9
0
        /// <summary>
        /// Approves and publishes all assets referred in any link fields (such as URL or Summary Links), rich-text content and rich-text web parts.
        /// </summary>
        /// <param name="adapter">Data access adapter of the list item.</param>
        /// <param name="additionalFields">Internal name of fields where its HTML content need to be parsed.</param>
        public static void PublishRelatedAssets(ISPListItemAdapter adapter, params string[] additionalFields)
        {
            CommonHelper.ConfirmNotNull(adapter, "adapter");
            CommonHelper.ConfirmNotNull(additionalFields, "additionalFields");

            SPListItem item = adapter.ListItem;

            if (item != null)
            {
                using (SPItemEventHelper.GetEventFiringDisabledScope()) {
                    try {
                        foreach (SPField field in item.Fields)
                        {
                            try {
                                object value = item[field.Id];
                                if (value != null)
                                {
                                    if (additionalFields.Contains(field.StaticName))
                                    {
                                        PublishAssetFromHtml(value.ToString(), item);
                                    }
                                    else if (field is BaseRichFieldType)
                                    {
                                        PublishAssetFromHtml(value.ToString(), item);
                                    }
                                    else if (field is SPFieldUrl)
                                    {
                                        PublishAssetInternal(new SPFieldUrlValue(value.ToString()).Url, item);
                                    }
                                    else if (field is SummaryLinkField)
                                    {
                                        foreach (SummaryLink link in new SummaryLinkFieldValue(value.ToString()).SummaryLinks)
                                        {
                                            PublishAssetInternal(link.LinkUrl, item);
                                        }
                                    }
                                }
                            } catch {
                            }
                        }
                        if (item.File != null && item.File.Name.EndsWith(".aspx"))
                        {
                            using (SPLimitedWebPartManager wpm = item.File.GetLimitedWebPartManager(PersonalizationScope.Shared)) {
                                foreach (object wp in wpm.WebParts)
                                {
                                    if (wp is ContentEditorWebPart)
                                    {
                                        XmlElement elm = ((ContentEditorWebPart)wp).Content;
                                        if (elm != null)
                                        {
                                            PublishAssetFromHtml(elm.InnerText, item);
                                        }
                                    }
                                    else if (wp is ImageWebPart)
                                    {
                                        PublishAssetInternal(((ImageWebPart)wp).ImageLink, item);
                                    }
                                    else if (wp is MediaWebPart)
                                    {
                                        PublishAssetInternal(((MediaWebPart)wp).MediaSource, item);
                                    }
                                    else if (wp is SummaryLinkWebPart)
                                    {
                                        foreach (SummaryLink link in ((SummaryLinkWebPart)wp).SummaryLinkValue.SummaryLinks)
                                        {
                                            PublishAssetInternal(link.LinkUrl, item);
                                        }
                                    }
                                }
                            }
                        }
                    } catch (Exception ex) {
                        throw new Exception("Aborting approval because of some related contents cannot be published or approved", ex);
                    }
                }
            }
        }
예제 #10
0
 /// <summary>
 /// Creates a model object representing the list item.
 /// </summary>
 /// <param name="adapter">A data access adapter of the list item.</param>
 /// <param name="manager">An instance of the <see cref="ISPModelManager"/> class which model object created will belongs to this manager.</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, ISPModelManager manager)
 {
     CommonHelper.ConfirmNotNull(manager, "manager");
     return(((ISPModelManagerInternal)manager).TryCreateModel(adapter, false));
 }