예제 #1
0
        public override void LoadNodes()
        {
            base.LoadNodes();

            this.Nodes.Add(new FieldCollectionNode(this.ContentType, this.ContentType.Fields));
            this.Nodes.Add(new WorkflowAssociationCollectionNode(this.ContentType, this.ContentType.WorkflowAssociations));
            this.Nodes.Add(new ContentTypeUsageCollectionNode(this.ContentType, SPContentTypeUsage.GetUsages(this.ContentType)));
        }
예제 #2
0
        public static IEnumerable <TList> GetListsByContentType <TList>(SPWeb web, SPContentType contentType)
            where TList : SPList
        {
            if (contentType == null)
            {
                return(new List <TList>());
            }

            IList <SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(contentType);

            return(GetListsByContentType <TList>(web, usages));
        }
        public static void Execute(SPContentType sourceCT, bool updateFields, bool removeFields)
        {
            using (SPSite site = new SPSite(sourceCT.ParentWeb.Site.ID))
            {
                IList <SPContentTypeUsage> ctUsageList = SPContentTypeUsage.GetUsages(sourceCT);
                foreach (SPContentTypeUsage ctu in ctUsageList)
                {
                    if (!ctu.IsUrlToList)
                    {
                        continue;
                    }

                    SPWeb web = null;
                    try
                    {
                        try
                        {
                            string webUrl = ctu.Url;
                            if (webUrl.ToLowerInvariant().Contains("_catalogs/masterpage"))
                            {
                                webUrl = webUrl.Substring(0, webUrl.IndexOf("/_catalogs/masterpage"));
                            }

                            web = site.OpenWeb(webUrl);
                        }
                        catch (SPException ex)
                        {
                            Logger.WriteWarning("Unable to open host web of content type\r\n{0}", ex.Message);
                            continue;
                        }
                        if (web != null)
                        {
                            SPList        list   = web.GetList(ctu.Url);
                            SPContentType listCT = list.ContentTypes[ctu.Id];
                            ProcessContentType(list, sourceCT, listCT, updateFields, removeFields);
                        }
                    }
                    finally
                    {
                        if (web != null)
                        {
                            web.Dispose();
                        }
                    }
                }
            }
        }
예제 #4
0
 private static IList <SPContentTypeUsage> GetContentTypeUsages(SPWeb web, SPContentTypeId contentTypeId)
 {
     try {
         if (SPContentTypeCtor != null && SPContentTypePropertyWeb != null)
         {
             SPContentType ct = (SPContentType)SPContentTypeCtor.Invoke(new object[] { contentTypeId });
             SPContentTypePropertyWeb.SetValue(ct, web);
             return(SPContentTypeUsage.GetUsages(ct));
         }
     } catch {
         SPContentType ct = web.AvailableContentTypes[contentTypeId];
         if (ct != null)
         {
             return(SPContentTypeUsage.GetUsages(ct));
         }
     }
     return(new SPContentTypeUsage[0]);
 }
예제 #5
0
        public void DeleteContentTypeIfNotUsed(SPContentType contentType)
        {
            // Find where the content type is being used.
            ICollection <SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(contentType);

            if (usages.Count <= 0)
            {
                // Delete unused content type.
                contentType.ParentWeb.ContentTypes.Delete(contentType.Id);
            }
            else
            {
                // Prepare the query to get all items in a list that uses the content type.
                SPQuery query = new SPQuery()
                {
                    Query = string.Concat(
                        "<Where><Eq>",
                        "<FieldRef Name='ContentTypeId'/>",
                        string.Format(CultureInfo.InvariantCulture, "<Value Type='Text'>{0}</Value>", contentType.Id),
                        "</Eq></Where>")
                };

                // Get the usages that are in a list.
                List <SPContentTypeUsage> listUsages = (from u in usages where u.IsUrlToList select u).ToList();
                foreach (SPContentTypeUsage usage in listUsages)
                {
                    // For a list usage, we get all the items in the list that use the content type.
                    SPList list = contentType.ParentWeb.GetList(usage.Url);
                    SPListItemCollection listItems = list.GetItems(query);

                    // if no items are found...
                    if (listItems.Count <= 0)
                    {
                        // Delete unused content type.
                        list.ContentTypes.Delete(contentType.Id);
                    }
                }
            }
        }
예제 #6
0
        public static Dictionary <string, List <string> > GetListUrls(SPContentType contentType)
        {
            IList <SPContentTypeUsage>          usages   = SPContentTypeUsage.GetUsages(contentType);
            Dictionary <string, List <string> > listUrls = new Dictionary <string, List <string> >();

            if (usages != null && usages.Count > 0)
            {
                foreach (SPContentTypeUsage usage in usages.Where(usage => usage.IsUrlToList))
                {
                    if (!usage.IsUrlToList)
                    {
                        continue;
                    }

                    Match m = Regex.Match(usage.Url, "^(?<web>.*?/)(Lists/)?(?<list>[^/]+)$");

                    if (m.Success)
                    {
                        string webUrl = m.Groups["web"].Value;

                        if (listUrls.ContainsKey(webUrl))
                        {
                            listUrls[webUrl].Add(m.Groups["list"].Value);
                        }
                        else
                        {
                            listUrls.Add(webUrl, new List <string>()
                            {
                                m.Groups["list"].Value
                            });
                        }
                    }
                }
            }

            return(listUrls);
        }
예제 #7
0
 public override object GetSPObject()
 {
     return(SPContentTypeUsage.GetUsages((SPContentType)Parent.SPObject));
 }
예제 #8
0
        private void ProvisionContentType(string siteUrl, Guid siteId, bool provisionParent, bool provisionChildren, HashSet <SPModelUsage> deferredListUrls)
        {
            if (provisionParent && this.Parent != null)
            {
                this.Parent.ProvisionContentType(siteUrl, siteId, true, false, null);
            }
            if (contentTypeAttribute != null)
            {
                if (TryLockSite(siteId))
                {
                    provisionedSites.TryAdd(siteId, true);
                    try {
                        SPModelProvisionEventReceiver eventReceiver = GetProvisionEventReceiver(true);
                        using (SPModelProvisionHelper helper = new SPModelProvisionHelper(siteId, eventReceiver)) {
                            SPContentType contentType = helper.EnsureContentType(contentTypeAttribute);
                            helper.UpdateContentType(contentType, contentTypeAttribute, fieldAttributes);
                            SaveAssemblyName(helper.TargetSite, contentTypeAttribute.ContentTypeId, this.ModelType.Assembly);

                            foreach (SPContentTypeUsage usage in SPContentTypeUsage.GetUsages(contentType))
                            {
                                if (usage.Id.Parent == contentType.Id && usage.IsUrlToList)
                                {
                                    using (SPSite listParentSite = new SPSite(helper.TargetSite.MakeFullUrl(usage.Url), SPUserToken.SystemAccount)) {
                                        using (SPWeb listParentWeb = listParentSite.OpenWeb()) {
                                            SPList list;
                                            try {
                                                list = listParentWeb.GetListSafe(usage.Url);
                                            } catch (FileNotFoundException) {
                                                continue;
                                            }
                                            SPContentType listContentType = list.ContentTypes[usage.Id];
                                            if (listContentType != null)
                                            {
                                                using (SPModelProvisionHelper helper2 = new SPModelProvisionHelper(siteId, eventReceiver)) {
                                                    helper2.UpdateContentType(listContentType, contentTypeAttribute, fieldAttributes);
                                                    helper2.UpdateList(list, listAttribute.Clone(list.RootFolder.Url), contentTypeAttribute, fieldAttributes, hiddenFields.ToArray(), new SPContentTypeId[0]);
                                                    if (deferredListUrls != null)
                                                    {
                                                        deferredListUrls.Add(SPModelUsage.Create(list, usage.Id).GetWithoutList());
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } catch (Exception ex) {
                        bool dummy;
                        provisionedSites.TryRemove(siteId, out dummy);
                        throw new SPModelProvisionException(String.Format("Unable to provision for type '{0}'. {1}. {2}", this.ModelType.Name, siteUrl, ex.Message), ex);
                    } finally {
                        EventWaitHandle handle;
                        if (provisionedSitesLocks.TryRemove(siteId, out handle))
                        {
                            handle.Set();
                            handle.Close();
                        }
                    }
                }
            }
            if (provisionChildren)
            {
                foreach (SPModelDescriptor child in this.Children)
                {
                    if (!child.hasExplicitListAttribute)
                    {
                        child.ProvisionContentType(siteUrl, siteId, false, true, deferredListUrls);
                    }
                }
            }
        }
        public bool RegisterCtToSync(SPContentType contentType, bool overrideView, bool forceCreating, bool tableNameAsParentList = true, bool hidden = false)
        {
            this._syncProvider = new SpSyncProvider(SyncType.SpContentType);

            bool resultFieldAdding =
                UtilityEventReceiver.CreateRecieverWithDeletingPrevious(FieldAddingName,
                                                                        SPEventReceiverType.FieldAdding, contentType, typeof(ListChangeReceiver), 10001, SPEventReceiverSynchronization.Synchronous);

            bool resultFieldUpdating =
                UtilityEventReceiver.CreateRecieverWithDeletingPrevious(FieldUpdatingName,
                                                                        SPEventReceiverType.FieldUpdating, contentType, typeof(ListChangeReceiver), 10001, SPEventReceiverSynchronization.Synchronous);

            bool resultFieldDeleting =
                UtilityEventReceiver.CreateRecieverWithDeletingPrevious(FieldDeletingName,
                                                                        SPEventReceiverType.FieldDeleting, contentType, typeof(ListChangeReceiver), 10001, SPEventReceiverSynchronization.Synchronous);

            bool resultItemAdded =
                UtilityEventReceiver.CreateRecieverWithDeletingPrevious(ItemAddedName,
                                                                        SPEventReceiverType.ItemAdded, contentType, typeof(ItemChangeReceiver), 10001, SPEventReceiverSynchronization.Synchronous);

            bool resultItemUpdated =
                UtilityEventReceiver.CreateRecieverWithDeletingPrevious(ItemUpdatedName,
                                                                        SPEventReceiverType.ItemUpdated, contentType, typeof(ItemChangeReceiver), 10001, SPEventReceiverSynchronization.Synchronous);

            bool resultItemDeleting =
                UtilityEventReceiver.CreateRecieverWithDeletingPrevious(ItemDeletingName,
                                                                        SPEventReceiverType.ItemDeleting, contentType, typeof(ItemChangeReceiver), 10001, SPEventReceiverSynchronization.Synchronous);

            string tableName          = tableNameAsParentList ? contentType.ParentList.RootFolder.ServerRelativeUrl : contentType.Name;
            bool   resultCreateCachDb = this._syncProvider.CreateDataBaseTable(contentType.ParentWeb.Site, tableName, contentType.Fields);

            if (resultFieldAdding && resultFieldUpdating && resultFieldDeleting && resultItemAdded && resultItemUpdated && resultItemDeleting && resultCreateCachDb)
            {
                if (hidden)
                {
                    return(true);
                }

                IList <SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(contentType);
                foreach (SPContentTypeUsage usage in usages)
                {
                    if (!usage.IsUrlToList)
                    {
                        continue;
                    }

                    SPList usageList = contentType.ParentWeb.GetList(usage.Url);

                    SettingsProvider.Instance(contentType.ParentWeb).SetIsCacheDb(usageList, true);
                    SettingsProvider.Instance(contentType.ParentWeb).SetIsOverrideView(usageList, overrideView);
                    SettingsProvider.Instance(contentType.ParentWeb).SetForceCreating(usageList, forceCreating);
                    SettingsProvider.Instance(contentType.ParentWeb).SetCacheTypeDb(usageList, SyncType.SpContentType);
                    SettingsProvider.Instance(contentType.ParentWeb).SetTableName(usageList, tableName);
                }

                return(true);
            }

            // что то упало, удаляем то что получилось создать
            if (resultFieldAdding)
            {
                UtilityEventReceiver.DeleteReciever(FieldAddingName, contentType);
            }

            if (resultFieldUpdating)
            {
                UtilityEventReceiver.DeleteReciever(FieldUpdatingName, contentType);
            }

            if (resultFieldDeleting)
            {
                UtilityEventReceiver.DeleteReciever(FieldDeletingName, contentType);
            }

            if (resultItemAdded)
            {
                UtilityEventReceiver.DeleteReciever(ItemAddedName, contentType);
            }

            if (resultItemUpdated)
            {
                UtilityEventReceiver.DeleteReciever(ItemUpdatedName, contentType);
            }

            if (resultItemDeleting)
            {
                UtilityEventReceiver.DeleteReciever(ItemDeletingName, contentType);
            }

            SettingsProvider.Instance(contentType.ParentList.ParentWeb).SetIsCacheDb(contentType.ParentList, false);
            return(false);
        }