Esempio n. 1
0
 public static void EnsureContentType(this SPContentTypeCollection contentTypeCollection, SPContentTypeMetadata contentTypeMetadata, SPWeb parentWeb = null)
 {
     if (contentTypeCollection.GetContentType(contentTypeMetadata) == null)
     {
         contentTypeCollection.Add(contentTypeMetadata, parentWeb);
     }
 }
Esempio n. 2
0
        private static void EnsureUniqueContentTypeOrder(SPFolder folder, FolderInfo folderInfo)
        {
            if (folderInfo.UniqueContentTypeOrder != null && folderInfo.UniqueContentTypeOrder.Count > 0)
            {
                SPContentTypeCollection listContentTypes = folder.ParentWeb.Lists[folder.ParentListId].ContentTypes;
                IList <SPContentType>   uniqueOrder      = new List <SPContentType>();

                foreach (ContentTypeInfo needToEnsureThisCTInfo in folderInfo.UniqueContentTypeOrder)
                {
                    var foundRealCT = listContentTypes[needToEnsureThisCTInfo.ContentTypeId];

                    if (foundRealCT == null)
                    {
                        // Failed to find CT with direct ID lookup: now let's assume that we were given site collection CT IDs as
                        // input and that we need to inspect the list CTs' parents to find a match.
                        foreach (var listCT in listContentTypes.Cast <SPContentType>())
                        {
                            if (listCT.Id.Parent == needToEnsureThisCTInfo.ContentTypeId)
                            {
                                foundRealCT = listCT;
                                break;
                            }
                        }
                    }

                    uniqueOrder.Add(foundRealCT);
                }

                folder.UniqueContentTypeOrder = uniqueOrder;
                folder.Update();
            }
        }
        public SPContentType GetContentType(SPContentTypeCollection contentTypeCollection)
        {
            if (!string.IsNullOrEmpty(ContentTypeId))
                return contentTypeCollection[new SPContentTypeId(ContentTypeId)];

            return contentTypeCollection[Name];
        }
Esempio n. 4
0
        public string[] FetchControlLists(string[] controlListIds, string cultureName)
        {
            //CommonUtilities.ConfirmNotNull(cultureName, "cultureName");
            //ULS.SendTraceTag(0x3670356d, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium, "SharepointPublishingToolboxService.FetchControlLists(, {0})", new object[] { cultureName });
            SPUtility.EnsureAuthentication(SPContext.Current.Site.RootWeb);
            CultureInfo culture = new CultureInfo(cultureName);
            //CommonUtilities.ConfirmNotNull(controlListIds, "controlListIds");
            SPContentTypeCollection contentTypes = this.GetContentTypes();
            TagNameCreator          tagCreator   = new TagNameCreator();

            string[] strArray = new string[controlListIds.Length];
            for (int i = 0; i < controlListIds.Length; i++)
            {
                string str = controlListIds[i];
                if (str == publishingControlspanelTypeIdentifier.ToString())
                {
                    strArray[i] = "";
                }
                else if (contentTypes != null)
                {
                    SPContentTypeId id          = new SPContentTypeId(str);
                    SPContentType   contentType = contentTypes[id];
                    if (contentType == null)
                    {
                        //string message = Resources.GetFormattedStringEx("ErrorContentTypeNotFound", culture, new object[] { str });
                        //ULS.SendTraceTag(0x3670356e, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Unexpected, "The content type {0} is not found", new object[] { str });
                        SoapException exception = new SoapException("ContentType is Empty", SoapException.ClientFaultCode);
                        throw exception;
                    }
                    strArray[i] = GetInfoFromCT(contentType, contentTypes, tagCreator, culture);
                }
            }
            //ULS.SendTraceTag(0x3670356f, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium, "SharepointPublishingToolboxService.GetCustomControlLists ends");
            return(strArray);
        }
Esempio n. 5
0
 public static List<Model.ContentType> ToContentTypes(SPContentTypeCollection contentTypes)
 {
     return contentTypes.Cast<SPContentType>().Select(ct => new Model.ContentType
     {
         Id = ct.Id.ToString(),
         Name = ct.Name
     }).ToList();
 }
Esempio n. 6
0
 public static List <Model.ContentType> ToContentTypes(SPContentTypeCollection contentTypes)
 {
     return(contentTypes.Cast <SPContentType>().Select(ct => new Model.ContentType
     {
         Id = ct.Id.ToString(),
         Name = ct.Name
     }).ToList());
 }
Esempio n. 7
0
 private static ArrayList FilterOutPageFields(SPContentType contentType, SPContentTypeCollection contentTypes)
 {
     if ((contentType.Id != ContentTypeId.PageTemplate) && contentType.Id.IsChildOf(ContentTypeId.PageTemplate))
     {
         SPContentType type = contentTypes[ContentTypeId.PageTemplate];
         return(GetFilteredFields(contentType.Fields, type.Fields));
     }
     return(new ArrayList(contentType.Fields));
 }
Esempio n. 8
0
        public SPContentType GetContentType(SPContentTypeCollection contentTypeCollection)
        {
            if (!string.IsNullOrEmpty(ContentTypeId))
            {
                return(contentTypeCollection[new SPContentTypeId(ContentTypeId)]);
            }

            return(contentTypeCollection[Name]);
        }
        public SPContentTypeCollectionInstance(ObjectInstance prototype, SPContentTypeCollection contentTypeCollection)
            : this(prototype)
        {
            if (contentTypeCollection == null)
            {
                throw new ArgumentNullException("contentTypeCollection");
            }

            m_contentTypeCollection = contentTypeCollection;
        }
Esempio n. 10
0
        public PanelInfo[] FetchPanelsInformationByUrl(string pageLayoutUrl, string cultureName)
        {
            //CommonUtilities.ConfirmNotNullOrEmptyString(pageLayoutUrl, "pageLayoutUrl");
            //CommonUtilities.ConfirmNotNull(cultureName, "cultureName");
            //ULS.SendTraceTag(0x3670356b, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium, "SharepointPublishingToolboxService.FetchPanelsInformationByUrl({0}, {1})", new object[] { pageLayoutUrl, cultureName });
            PanelInfo[] infoArray = null;
            SPSite      site      = SPContext.Current.Site;

            SPUtility.EnsureAuthentication();
            Uri    uri = new Uri(pageLayoutUrl);
            string url = ConvertToAbsoluteUrl(uri.PathAndQuery, site);
            SPWeb  web = null;

            try
            {
                web = site.OpenWeb();
                SPFile fileFromUrl = GetFileFromUrl(url, web);
                if ((fileFromUrl != null) && fileFromUrl.Exists)
                {
                    ContentTypeIdFieldValue value2;
                    SPListItem item = fileFromUrl.Item;
                    SPContentTypeCollection contentTypes = this.GetContentTypes();
                    try
                    {
                        value2 = (ContentTypeIdFieldValue)item[BuildFieldId.PublishingAssociatedContentType];
                    }
                    catch (ArgumentException)
                    {
                        SPContentType contentType = item.ContentType;
                        while ((contentTypes[contentType.Id] == null) && (contentType.Parent != null))
                        {
                            contentType = contentType.Parent;
                        }
                        value2 = new ContentTypeIdFieldValue(contentType);
                    }
                    string        contentTypeId = value2.Id.ToString();
                    SPContentType type2         = this.GetContentTypes()[value2.Id];
                    if ((type2 == null) || !type2.Id.IsChildOf(ContentTypeId.PageTemplate))
                    {
                        CultureInfo culture = new CultureInfo(cultureName);
                        // throw new SoapException(Resources.GetFormattedStringEx("ErrorInvalidAssociatedContentTypeId", culture, new object[] { contentTypeId, value2.StoredName, pageLayoutUrl }), SoapException.ServerFaultCode);
                    }
                    infoArray = this.FetchPanelsInformation(contentTypeId, cultureName);
                }
            }
            finally
            {
                if (web != null)
                {
                    web.Close();
                }
            }
            //ULS.SendTraceTag(0x3670356c, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium, "SharepointPublishingToolboxService.FetchPanelsInformationByUrl ends");
            return(infoArray);
        }
Esempio n. 11
0
        /// <summary>
        /// Ensure a list of content type
        /// </summary>
        /// <param name="contentTypeCollection">The content type collection</param>
        /// <param name="contentTypeInfos">The content types information</param>
        /// <returns>The content types list</returns>
        public IEnumerable <SPContentType> EnsureContentType(SPContentTypeCollection contentTypeCollection, ICollection <ContentTypeInfo> contentTypeInfos)
        {
            var contentTypes = new List <SPContentType>();

            foreach (ContentTypeInfo contentType in contentTypeInfos)
            {
                contentTypes.Add(this.EnsureContentType(contentTypeCollection, contentType));
            }

            return(contentTypes);
        }
Esempio n. 12
0
 private bool DocLibContainsContentType(SPContentTypeCollection libraryContentTypes, SPContentType contentType)
 {
     foreach (SPContentType libraryContentType in libraryContentTypes)
     {
         if (libraryContentType.Name == contentType.Name)
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 13
0
        private PanelInfo GetPageControlsPanelInfo(SPContentTypeCollection contentTypes, CultureInfo culture)
        {
            SPContentType type = contentTypes[ContentTypeId.PageTemplate];
            PanelInfo     info = new PanelInfo();

            info                     = new PanelInfo();
            info.displayName         = type.Name;// ""; //Resources.GetStringEx("ToolboxPanelNamePageControls", culture);
            info.controlListId       = type.Id.ToString();
            info.panelTypeIdentifier = pageControlspanelTypeIdentifier;
            return(info);
        }
        protected void ddl_contentname_SelectedIndexChanged(object sender, EventArgs e)
        {
            SPWeb rootWeb = SPContext.Current.Site.RootWeb;
            //SPList list = rootWeb.GetList("_catalogs/masterpage");
            //SPContentTypeCollection availableContentTypes = list.ContentTypes;
            SPContentTypeCollection availableContentTypes = rootWeb.AvailableContentTypes;

            SPContentType type = availableContentTypes[new SPContentTypeId(this.ddl_contentname.SelectedValue)];

            this.lbl_Description.Text = type.Description;
        }
Esempio n. 15
0
        private static string[] GetContentTypeGroups(ISharePointCommandContext context)
        {
            SPContentTypeCollection contentTypes         = context.Web.AvailableContentTypes;
            IEnumerable <string>    allContentTypeGroups = (from SPContentType contentType
                                                            in contentTypes
                                                            select contentType.Group);

            string[] contentTypeGroups = allContentTypeGroups.Distinct().ToArray();

            return(contentTypeGroups);
        }
Esempio n. 16
0
        /// <summary>
        /// Ensure a list of content type
        /// </summary>
        /// <param name="contentTypeCollection">The content type collection</param>
        /// <param name="contentTypeInfos">The content types information</param>
        /// <returns>The content types list</returns>
        public IEnumerable<SPContentType> EnsureContentType(SPContentTypeCollection contentTypeCollection, ICollection<ContentTypeInfo> contentTypeInfos)
        {
            var contentTypes = new List<SPContentType>();

            foreach (ContentTypeInfo contentType in contentTypeInfos)
            {
                contentTypes.Add(this.EnsureContentType(contentTypeCollection, contentType));
            }

            return contentTypes;
        }
        private void PopulateControls(string contentTypeGroup, SPContentTypeId contentTypeId)
        {
            if (string.IsNullOrEmpty(contentTypeGroup))
            {
                contentTypeGroup = "Publishing Content Types";
            }

            SPWeb rootWeb = SPContext.Current.Site.RootWeb;
            //SPList list = rootWeb.GetList("_catalogs/masterpage");
            //SPContentTypeCollection availableContentTypes = list.ContentTypes;
            SPContentTypeCollection availableContentTypes = rootWeb.AvailableContentTypes;

            foreach (SPContentType contenttype in availableContentTypes)
            {
                if (!this.ddl_contentgroup.Items.Contains(new ListItem(contenttype.Group)))
                {
                    this.ddl_contentgroup.Items.Add(contenttype.Group);
                }
            }

            this.ddl_contentgroup.Items.Remove("_Hidden");
            this.ddl_contentgroup.SelectedValue = contentTypeGroup;

            foreach (SPContentType contenttype in availableContentTypes)
            {
                if (contenttype.Group == this.ddl_contentgroup.SelectedValue)
                {
                    ListItem item = new ListItem(contenttype.Name, contenttype.Id.ToString());
                    if (!this.ddl_contentname.Items.Contains(item))
                    {
                        this.ddl_contentname.Items.Add(item);
                    }
                }
            }

            if (this.ddl_contentname.Items.Count > 0)
            {
                if (SPContentTypeId.Empty != contentTypeId)
                {
                    SPContentType current = availableContentTypes[contentTypeId];

                    if (current != null)
                    {
                        this.ddl_contentname.SelectedValue = current.Id.ToString();
                        this.lbl_Description.Text          = current.Description;
                    }
                }
                else
                {
                    SPContentType current = availableContentTypes[new SPContentTypeId(this.ddl_contentname.Items[0].Value)];
                    this.lbl_Description.Text = current.Description;
                }
            }
        }
Esempio n. 18
0
        private PanelInfo GetContentTypeControlsPanelInfo(SPContentTypeCollection contentTypes, string contentTypeIdString, CultureInfo culture)
        {
            SPContentTypeId id   = new SPContentTypeId(contentTypeIdString);
            SPContentType   type = contentTypes[id];
            PanelInfo       info = new PanelInfo();

            info                     = new PanelInfo();
            info.displayName         = type.Name;// "";//Resources.GetFormattedStringEx("ToolboxPanelNameContentTypeControls", culture, new object[] { type.Name });
            info.controlListId       = contentTypeIdString;
            info.panelTypeIdentifier = contentTypepanelTypeIdentifier;
            return(info);
        }
Esempio n. 19
0
        protected void buttonCreateType_Click(object sender, EventArgs e)
        {
            //Here we will create a new content type and list
            //Start by getting the current web
            SPWeb web = SPContext.Current.Web;
            //Get the web's collection of content types
            SPContentTypeCollection contentTypes = web.ContentTypes;
            //Create a new content type
            SPContentType newType = new SPContentType(contentTypes[SPBuiltInContentTypeId.Announcement], contentTypes, "Contoso Announcements");

            //Add it to the web
            try
            {
                contentTypes.Add(newType);
            }
            catch (SPException ex)
            {
                //This is probably because the content type already exists
                labelCreateTypeResult.Text = ex.Message;
            }
            //Now get the web's field collection and add a new field to it
            SPFieldCollection siteFields = web.Fields;

            try
            {
                siteFields.Add("Product", SPFieldType.Text, false);
                web.Update();
            }
            catch (SPException ex)
            {
                //This is probably because the field already exists
                labelCreateTypeResult.Text = ex.Message;
            }
            //Add the field to the new content type
            newType.FieldLinks.Add(new SPFieldLink(siteFields["Product"]));
            newType.Update();
            //Get the web's list collection
            SPListCollection lists = web.Lists;

            try
            {
                Guid   newListGuid = lists.Add("Product Announcements", "Announcements about Contoso Products", SPListTemplateType.Announcements);
                SPList newList     = lists[newListGuid];
                newList.ContentTypes.Add(newType);
                newList.Update();
            }
            catch (SPException ex)
            {
                //This is probably because the field already exists
                labelCreateTypeResult.Text = ex.Message;
            }
            labelCreateTypeResult.Text = "Contoso Announcement content type and Product Announcements list created successfully";
        }
Esempio n. 20
0
        protected void ddl_contentname_SelectedIndexChanged(object sender, EventArgs e)
        {
            SPContentTypeId id      = new SPContentTypeId(this.ddl_contentname.SelectedValue);
            SPWeb           rootWeb = SPContext.Current.Site.RootWeb;

            //SPList list = SPContext.Current.Site.GetCatalog(SPListTemplateType.MasterPageCatalog);
            //SPContentTypeCollection availableContentTypes = list.ContentTypes;
            SPContentTypeCollection availableContentTypes = rootWeb.AvailableContentTypes;
            SPContentType           type = availableContentTypes[id];

            this.lbl_description.Text = type.Description;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                SPWeb web     = SPContext.Current.Web;
                SPWeb rootWeb = SPContext.Current.Site.RootWeb;
                SPContentTypeCollection availableContentTypes = rootWeb.AvailableContentTypes;
                SPList catalog = rootWeb.Site.GetCatalog(SPListTemplateType.MasterPageCatalog);

                foreach (SPContentType contenttype in catalog.ContentTypes)
                {
                    if (!this.ddl_contentgroup.Items.Contains(new ListItem(contenttype.Group)))
                    {
                        this.ddl_contentgroup.Items.Add(contenttype.Group);
                    }
                }

                this.ddl_contentgroup.Items.Remove("_Hidden");
                this.ddl_contentgroup.SelectedValue = "Publishing Content Types";

                foreach (SPContentType contenttype in availableContentTypes)
                {
                    if (contenttype.Group == this.ddl_contentgroup.SelectedValue)
                    {
                        if (contenttype.Id.IsChildOf(ContentTypeId.PageTemplate) && contenttype.Id != ContentTypeId.PageTemplate && contenttype.Id != ContentTypeId.PageLayout)
                        {
                            ListItem item = new ListItem(contenttype.Name, contenttype.Id.ToString());
                            this.ddl_contentname.Items.Add(item);
                        }
                    }
                }

                if (this.ddl_contentname.Items.Count > 0)
                {
                    this.ddl_contentname.SelectedIndex = 0;
                    SPContentTypeId contenttypeid = new SPContentTypeId(this.ddl_contentname.SelectedValue);

                    SPContentType type = rootWeb.AvailableContentTypes[contenttypeid];

                    if (type != null)
                    {
                        this.lbl_description.Text = type.Description;
                    }
                    this.Btn_Save.Enabled = true;
                }
                else
                {
                    this.Btn_Save.Enabled     = false;
                    this.lbl_description.Text = "No content template available.";
                }
            }
        }
Esempio n. 22
0
        private void TryAddEachContentTypeToLibrary(SPDocumentLibrary docLibrary, List <SPContentType> ContentTypesOnThisWeb)
        {
            SPContentTypeCollection libraryContentTypes = docLibrary.ContentTypes;

            foreach (SPContentType contentType in ContentTypesOnThisWeb)
            {
                if (DocLibContainsContentType(libraryContentTypes, contentType))
                {
                    continue;
                }
                libraryContentTypes.Add(contentType);
            }
        }
Esempio n. 23
0
        private static ContentTypeNodeInfo[] GetContentTypesFromGroup(ISharePointCommandContext context, string groupName)
        {
            SPContentTypeCollection contentTypes = context.Web.AvailableContentTypes;

            ContentTypeNodeInfo[] contentTypesFromGroup = (from SPContentType contentType
                                                           in contentTypes
                                                           where contentType.Group == groupName
                                                           select new ContentTypeNodeInfo {
                Name = contentType.Name
            }).ToArray();

            return(contentTypesFromGroup);
        }
Esempio n. 24
0
        CreatePRContentTypes(SPContentTypeCollection spContentTypes, Dictionary <Guid, PRList> listsDictionary, Action <ProgressChangedEventArgs> progress, Func <string, bool, bool> selected)
        {
            List <PRContentType> _prContentTypeList = new List <PRContentType>();
            int _ix = 0;

            foreach (SPContentType _spctx in spContentTypes)
            {
                progress(new ProgressChangedEventArgs(_ix * 100 / spContentTypes.Count, _spctx.Name));
                _ix++;
                _prContentTypeList.Add(PRContentType.CreatePRContentType(_spctx, listsDictionary, selected));
            }
            return(_prContentTypeList);
        }
Esempio n. 25
0
        private PanelInfo[] FetchPanelsInformation(string contentTypeId, string cultureName)
        {
            //CommonUtilities.ConfirmNotNullOrEmptyString(contentTypeId, "contentTypeId");
            //CommonUtilities.ConfirmNotNull(cultureName, "cultureName");
            //ULS.SendTraceTag(0x36703569, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium, "SharepointPublishingToolboxService.FetchPanelsInformation({0}, {1})", new object[] { contentTypeId, cultureName });
            SPUtility.EnsureAuthentication(SPContext.Current.Site.RootWeb);
            CultureInfo             culture      = new CultureInfo(cultureName);
            SPContentTypeCollection contentTypes = this.GetContentTypes();

            PanelInfo[] infoArray = new PanelInfo[] { this.GetPageControlsPanelInfo(contentTypes, culture), this.GetContentTypeControlsPanelInfo(contentTypes, contentTypeId, culture) };
            //ULS.SendTraceTag(0x3670356a, ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium, "SharepointPublishingToolboxService.FetchPanelsInformation ends", new object[] { contentTypeId, cultureName });
            return(infoArray);
        }
        /// <summary>
        /// Create content type
        /// </summary>
        /// <param name="contentTypes"></param>
        /// <returns></returns>
        private static SPContentType CreateContentType(this SPContentTypeCollection contentTypes)
        {
            var constructor = (typeof(SPContentType)).GetConstructor(
                BindingFlags.NonPublic | BindingFlags.Instance,
                null,
                Type.EmptyTypes,
                null);

            var contentType = (SPContentType)constructor.Invoke(new object[0]);

            contentType.SetWeb(contentTypes.GetWeb());
            return(contentType);
        }
        /// <summary>
        /// Creates a content type based on the specified schema.
        /// </summary>
        /// <returns>
        /// An instance of the new content type.
        /// </returns>
        /// <param name="contentTypes">Content Type collection</param>
        /// <param name="schemaXml">A Collaborative Application Markup Language (CAML) string that contains the schema.</param>
        public static SPContentType AddContentTypeAsXml(this SPContentTypeCollection contentTypes, string schemaXml)
        {
            SPContentType contentType;

            using (var xrdr = new XmlTextReader(new StringReader(schemaXml)))
            {
                xrdr.ProhibitDtd = true;
                contentType      = contentTypes.CreateContentType();
                LoadXmlInternal(contentType, xrdr);
                contentTypes.Add(contentType);
            }
            return(contentType);
        }
Esempio n. 28
0
        public ContentTypeCollectionNode(Object parent, SPContentTypeCollection collection)
        {
            this.Text = SPMLocalization.GetString("ContentTypes_Text");
            this.ToolTipText = SPMLocalization.GetString("ContentTypes_ToolTip");
            this.Name = "ContentType Collection";
            this.Tag = collection;
            this.SPParent = parent;
            int index = Program.Window.Explorer.AddImage(this.ImageUrl());
            this.ImageIndex = index;
            this.SelectedImageIndex = index;

            this.Nodes.Add(new ExplorerNodeBase("Dummy"));
        }
        private void GetAvailableTargetContentTypes(string targetUrl)
        {
            using (SPSite site = new SPSite(targetUrl))
            {
                using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(targetUrl)])
                {
                    Logger.Write("Progress: Getting existing target fields and content types...");

                    _availableTargetContentTypes = web.AvailableContentTypes;
                    _targetContentTypes          = web.ContentTypes;
                    _targetFields = web.Fields;
                }
            }
        }
        public ContentTypeCollectionNode(Object parent, SPContentTypeCollection collection)
        {
            this.Text        = SPMLocalization.GetString("ContentTypes_Text");
            this.ToolTipText = SPMLocalization.GetString("ContentTypes_ToolTip");
            this.Name        = "ContentType Collection";
            this.Tag         = collection;
            this.SPParent    = parent;
            int index = Program.Window.Explorer.AddImage(this.ImageUrl());

            this.ImageIndex         = index;
            this.SelectedImageIndex = index;

            this.Nodes.Add(new ExplorerNodeBase("Dummy"));
        }
Esempio n. 31
0
        public SPContentType EnsureContentType(SPContentTypeCollection contentTypeCollection, ContentTypeInfo contentTypeInfo)
        {
            var contentType = this.InnerEnsureContentType(contentTypeCollection, contentTypeInfo);

            SPList list;
            if (!TryGetListFromContentTypeCollection(contentTypeCollection, out list))
            {
                // Set the content type title, description, and group information for each language.
                // Only do this when not on a web because the SPContentType Title property does not support resource values at this level.
                // The content type for a list is created at the root web level, then added to the list.
                this.SetTitleDescriptionAndGroupValues(contentTypeInfo, contentType);
            }

            return contentType;
        }
Esempio n. 32
0
        private void GetPublishingTypes(SPList list)
        {
            Func <SPContentType, bool> contentgroup = null;

            if (contentgroup == null)
            {
                contentgroup = delegate(SPContentType f)
                {
                    return(f.Id.Parent == Hemrika.SharePresence.WebSite.ContentTypes.ContentTypeId.PageTemplate);
                };
            }

            SPContentTypeCollection contentypes = list.ContentTypes;

            publishingtypes = contentypes.Cast <SPContentType>().Where <SPContentType>(contentgroup);
        }
Esempio n. 33
0
        private void PopulateContentTypeDD(SPWeb web)
        {
            cbContentTypes.Items.Clear();

            SPContentTypeCollection types = web.ContentTypes;

            foreach (SPContentType ct in types)
            {
                ctDD = new ControlWrapper <SPContentType>(ct.Name, ct);
                cbContentTypes.Items.Add(ctDD);
            }
            if (types.Count > 0)
            {
                cbContentTypes.SelectedIndex = 0;
            }
        }
Esempio n. 34
0
        public SPContentType EnsureContentType(SPContentTypeCollection contentTypeCollection, ContentTypeInfo contentTypeInfo)
        {
            var contentType = this.InnerEnsureContentType(contentTypeCollection, contentTypeInfo);

            SPList list;

            if (!TryGetListFromContentTypeCollection(contentTypeCollection, out list))
            {
                // Set the content type title, description, and group information for each language.
                // Only do this when not on a web because the SPContentType Title property does not support resource values at this level.
                // The content type for a list is created at the root web level, then added to the list.
                this.SetTitleDescriptionAndGroupValues(contentTypeInfo, contentType);
            }

            return(contentType);
        }
Esempio n. 35
0
        private void PopulateControls(string Group)//, PageLayout pageLayout)
        {
            SPWeb rootWeb = SPContext.Current.Site.RootWeb;
            SPContentTypeCollection availableContentTypes = rootWeb.AvailableContentTypes;

            foreach (SPContentType contenttype in availableContentTypes)
            {
                if (contenttype.Group == Group)//"Publishing Content Types")
                {
                    //if (contenttype.Id == SPContext.Current.ListItem.ContentTypeId)
                    //{
                    List <PageLayout> layouts = servicePageLayouts.GetPageLayouts(contenttype.Id);
                    foreach (PageLayout layout in layouts)
                    {
                        ListItem item = new ListItem(layout.Title, layout.UniqueId.ToString());
                        if (!this.ddl_PublishingLayout.Items.Contains(item))
                        {
                            this.ddl_PublishingLayout.Items.Add(item);
                        }
                    }
                    //}
                }
            }

            /*
             * if (this.ddl_PublishingLayout.Items.Count > 0)
             * {
             *  if (SPContentTypeId.Empty != contentTypeId)
             *  {
             *      SPContentType current = availableContentTypes[contentTypeId];
             *      this.ddl_PublishingLayout.SelectedValue = current.Id.ToString();
             *      this.lbl_Description.Text = current.Description;
             *  }
             *  else
             *  {
             *      SPContentType current = availableContentTypes[new SPContentTypeId(this.ddl_PublishingLayout.Items[0].Value)];
             *      this.lbl_Description.Text = current.Description;
             *  }
             */
            /*
             * //this.ddl_contentname.SelectedValue = Hemrika.SharePresence.WebSite.ContentTypes.ContentTypeId.PageLayout.ToString();
             * SPContentType type = availableContentTypes[new SPContentTypeId(this.ddl_PublishingLayout.Items[0].Value)];
             * this.lbl_Description.Text = type.Description;
             */
            //}
        }
        public void DeleteContentTypeIfNotUsed(SPContentTypeCollection collection, SPContentTypeId contentTypeId)
        {
            if (contentTypeId == null)
            {
                throw new ArgumentNullException("contentTypeId");
            }

            if (contentTypeId == null)
            {
                throw new ArgumentNullException("contentTypeId");
            }

            // Get the content type from the web.
            SPContentType contentType = collection[collection.BestMatch(contentTypeId)];

            // return false if the content type does not exist.
            if (contentType != null)
            {
                // Delete the content type if not used.
                this.DeleteContentTypeIfNotUsed(contentType);
            }
        }
        public void AddContentTypeTo(SPContentTypeCollection contentTypeCollection, SPWeb parentWeb = null)
        {
            SPContentType newCt = GetContentType(contentTypeCollection);
            if (newCt != null)
                return;

            if (parentWeb != null && ((newCt = GetContentType(parentWeb.Site.RootWeb.AvailableContentTypes)) != null))
            {
                contentTypeCollection.Add(newCt);
            }
            else
            {
                if (!string.IsNullOrEmpty(ContentTypeId))
                {
                    newCt = new SPContentType(new SPContentTypeId(ContentTypeId), contentTypeCollection, Name);
                }
                else
                {
                    SPContentType parentCt = parentWeb.AvailableContentTypes[ParentContentType];
                    newCt = new SPContentType(parentCt, contentTypeCollection, Name);
                }

                if (!string.IsNullOrEmpty(Group))
                    newCt.Group = Group;
                if (!string.IsNullOrEmpty(Description))
                    newCt.Description = Description;

                try { contentTypeCollection.Add(newCt); }
                catch (SPException)
                {
                    parentWeb.Site.RootWeb.ContentTypes.Add(newCt);
                    newCt = parentWeb.Site.RootWeb.ContentTypes[newCt.Id];
                    contentTypeCollection.Add(newCt);
                }
            }
        }
 public virtual SPContentType GetContentType(SPContentTypeCollection contentTypeCollection, SPContentTypeId contentTypeId, bool isCollectionFromList)
 {
     if (isCollectionFromList)
     {
         return contentTypeCollection.OfType<SPContentType>().FirstOrDefault<SPContentType>(c => c.Id == contentTypeId || c.Parent.Id == contentTypeId);
     }
     else
     {
         return contentTypeCollection[contentTypeId];
     }
 }
Esempio n. 39
0
        private static bool TryGetWebFromContentTypeCollection(SPContentTypeCollection collection, out SPWeb web)
        {
            if (collection.Count > 0)
            {
                SPContentType first = collection[0];
                if (first != null)
                {
                    if (first.ParentWeb != null)
                    {
                        web = first.ParentWeb;
                        return true;
                    }
                }
            }

            web = null;
            return false;
        }
Esempio n. 40
0
        private static bool TryGetListFromContentTypeCollection(SPContentTypeCollection collection, out SPList list)
        {
            if (collection.Count > 0)
            {
                SPContentType first = collection[0];
                if (first != null)
                {
                    if (first.ParentList != null)
                    {
                        list = first.ParentList;
                        return true;
                    }
                }
            }

            list = null;
            return false;
        }
Esempio n. 41
0
        private SPContentType InnerEnsureContentType(SPContentTypeCollection contentTypeCollection, ContentTypeInfo contentTypeInfo)
        {
            if (contentTypeCollection == null)
            {
                throw new ArgumentNullException("contentTypeCollection");
            }

            SPContentTypeId contentTypeId = contentTypeInfo.ContentTypeId;
            SPList list = null;

            var contentTypeResourceTitle = this.resourceLocator.GetResourceString(contentTypeInfo.ResourceFileName, contentTypeInfo.DisplayNameResourceKey);

            if (TryGetListFromContentTypeCollection(contentTypeCollection, out list))
            {
                // Make sure its not already in the list.
                var contentTypeInList = list.ContentTypes.Cast<SPContentType>().FirstOrDefault(ct => ct.Id == contentTypeId || ct.Parent.Id == contentTypeId);
                if (contentTypeInList == null)
                {
                    // Can we add the content type to the list?
                    if (list.IsContentTypeAllowed(contentTypeId))
                    {
                        // Enable content types if not yet done.
                        if (!list.ContentTypesEnabled)
                        {
                            list.ContentTypesEnabled = true;
                            list.Update(true);
                        }

                        // Try to use the list's web's content type if it already exists
                        var contentTypeInWeb = list.ParentWeb.Site.RootWeb.AvailableContentTypes[contentTypeId];

                        if (contentTypeInWeb == null)
                        {
                            // By convention, content types should always exist on root web as site-collection-wide
                            // content types before they get linked on a specific list.
                            var rootWebContentTypeCollection = list.ParentWeb.Site.RootWeb.ContentTypes;
                            contentTypeInWeb = this.EnsureContentType(rootWebContentTypeCollection, contentTypeInfo);

                            this.log.Warn(
                                "EnsureContentType - Forced the creation of Content Type (name={0} ctid={1}) on the root web (url=) instead of adding the CT directly on the list (id={2} title={3}). By convention, all CTs should be provisonned on RootWeb before being re-used in lists.",
                                contentTypeInWeb.Name,
                                contentTypeInWeb.Id.ToString(),
                                list.ID,
                                list.Title);
                        }

                        // Add the web content type to the collection.
                        return list.ContentTypes.Add(contentTypeInWeb);
                    }
                }
                else
                {
                    this.InnerEnsureFieldInContentType(contentTypeInList, contentTypeInfo.Fields);

                    return contentTypeInList;
                }
            }
            else
            {
                SPWeb web = null;
                if (TryGetWebFromContentTypeCollection(contentTypeCollection, out web))
                {
                    // Make sure its not already in ther web.
                    var contentTypeInWeb = web.ContentTypes[contentTypeId];
                    if (contentTypeInWeb == null)
                    {
                        SPContentTypeCollection rootWebContentTypeCollection = null;

                        if (web.ID == web.Site.RootWeb.ID)
                        {
                            rootWebContentTypeCollection = contentTypeCollection;
                        }
                        else
                        {
                            rootWebContentTypeCollection = web.Site.RootWeb.ContentTypes;

                            this.log.Warn(
                                "EnsureContentType - Will force creation of content type (id={0} name={1}) on root web instead of on specified sub-web. This is to enforce the following convention: all CTs should be provisioned at root of site collection, to ease maintenance. Ensure your content types on the root web's SPContentTypeCollection to avoid this warning.",
                                contentTypeId.ToString(),
                                contentTypeInfo.DisplayNameResourceKey);
                        }

                        var contentTypeInRootWeb = rootWebContentTypeCollection[contentTypeId];

                        if (contentTypeInRootWeb == null)
                        {
                            // Add the content type to the Root Web collection. By convention, we avoid provisioning
                            // CTs directly on sub-webs to make CT management easier (i.e. all of your site collection's
                            // content types should be configured at the root of the site collection).
                            var newWebContentType = new SPContentType(contentTypeId, rootWebContentTypeCollection, contentTypeResourceTitle);
                            contentTypeInRootWeb = rootWebContentTypeCollection.Add(newWebContentType);
                        }

                        this.InnerEnsureFieldInContentType(contentTypeInRootWeb, contentTypeInfo.Fields);

                        return contentTypeInRootWeb;
                    }
                    else
                    {
                        this.InnerEnsureFieldInContentType(contentTypeInWeb, contentTypeInfo.Fields);
                        return contentTypeInWeb;
                    }
                }

                // Case if there is no Content Types in the Web (e.g single SPWeb)
                var returnedContentType = this.EnsureContentType(contentTypeCollection, contentTypeInfo);
                return returnedContentType;
            }

            return null;
        }
        private void GetAvailableTargetContentTypes(string targetUrl)
        {
            using (SPSite site = new SPSite(targetUrl))
            {
                using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(targetUrl)])
                {
                    Logger.Write("Progress: Getting existing target fields and content types...");

                    _availableTargetContentTypes = web.AvailableContentTypes;
                    _targetContentTypes = web.ContentTypes;
                    _targetFields = web.Fields;
                }
            }
        }
Esempio n. 43
0
 internal abstract SPContentType Provision(SPContentTypeCollection contentTypeCollection, SPList list, bool updateIfExists, bool updateChildren, bool stopOnSealedOrReadOnlyUpdate);
        public virtual SPContentType AddContentTypeToCollection(SPContentTypeCollection contentTypeCollection, SPContentType contentType)
        {
            var result = contentTypeCollection.Add(contentType);

            return result;
        }
Esempio n. 45
0
 internal abstract void Unprovision(SPContentTypeCollection contentTypeCollection, bool isCollectionFromList, bool deleteAllUsages, bool ignoreError);
        /// <summary>
        /// Creates the Example Document Content Type
        /// </summary>
        /// <param name="collection">The collection the content type will be added to.</param>
        private void CreateExampleContentType(SPContentTypeCollection collection)
        {
            var contentType = this.contentTypeHelper.EnsureContentType(
                collection,
                MyContentTypes.ContentOrganizerExampleContentType,
                "Content Organizer Example Content Type");

            this.contentTypeHelper.EnsureFieldInContentType(contentType, this.contentOrganizerExampleFields);

            contentType.Group = "Dynamite";
            contentType.Description = "Content Organizer Example Content Type";

            contentType.Update(true);

            this.taxonomyHelper.EnsureTaxonomyEventReceivers(contentType.EventReceivers);
        }
        public SPContentType EnsureContentType(SPContentTypeCollection contentTypeCollection, ContentTypeInfo contentTypeInfo)
        {
            SPContentType contentType = this.EnsureContentType(
                contentTypeCollection,
                contentTypeInfo.ContentTypeId,
                contentTypeInfo.TitleResourceString);

            this.EnsureFieldInContentType(contentType, contentTypeInfo.Fields);

            contentType.Description = contentTypeInfo.DescriptionResourceString;
            contentType.Group = contentTypeInfo.ContentGroupResourceString;
            contentType.Update();

            return contentType;
        }
        public SPContentType EnsureContentType(SPContentTypeCollection contentTypeCollection, SPContentTypeId contentTypeId, string contentTypeName)
        {
            if (contentTypeCollection == null)
            {
                throw new ArgumentNullException("contentTypeCollection");
            }

            if (contentTypeId == null)
            {
                throw new ArgumentNullException("contentTypeId");
            }

            if (string.IsNullOrEmpty(contentTypeName))
            {
                throw new ArgumentNullException("contentTypeName");
            }

            SPList list = null;

            if (TryGetListFromContentTypeCollection(contentTypeCollection, out list))
            {
                // Make sure its not already in the list.
                var contentTypeInList = list.ContentTypes.Cast<SPContentType>().FirstOrDefault(ct => ct.Parent.Id == contentTypeId);
                if (contentTypeInList == null)
                {
                    // Can we add the content type to the list?
                    if (list.IsContentTypeAllowed(contentTypeId))
                    {
                        // Try to use the list's web's content type if it already exists
                        var contentTypeInWeb = list.ParentWeb.AvailableContentTypes[contentTypeId];

                        if (contentTypeInWeb != null)
                        {
                            // Add the web content type to the collection.
                            return list.ContentTypes.Add(contentTypeInWeb);
                        }
                        else
                        {
                            // Create the content type directly on the list
                            var newListContentType = new SPContentType(contentTypeId, contentTypeCollection, contentTypeName);
                            var returnedListContentType = list.ContentTypes.Add(newListContentType);
                            return returnedListContentType;
                        }
                    }
                }
                else
                {
                    return contentTypeInList;
                }
            }
            else
            {
                SPWeb web = null;
                if (TryGetWebFromContentTypeCollection(contentTypeCollection, out web))
                {
                    // Make sure its not already in ther web.
                    var contentTypeInWeb = web.ContentTypes[contentTypeId];
                    if (contentTypeInWeb == null)
                    {
                        // Add the content type to the collection.
                        var newWebContentType = new SPContentType(contentTypeId, contentTypeCollection, contentTypeName);
                        var returnedWebContentType = contentTypeCollection.Add(newWebContentType);
                        return returnedWebContentType;
                    }
                    else
                    {
                        return contentTypeInWeb;
                    }
                }

                // Case if there is no Content Types in the Web (e.g single SPWeb)
                var newContentType = new SPContentType(contentTypeId, contentTypeCollection, contentTypeName);
                var returnedContentType = contentTypeCollection.Add(newContentType);
                return returnedContentType;
            }

            return null;
        }
        /// <summary>
        /// Creates the wall post content type.
        /// </summary>
        /// <param name="collection">The collection the content type will be added to.</param>
        private void CreateWallPostContentType(SPContentTypeCollection collection)
        {
            SPContentType contentType = this._contentTypeHelper.EnsureContentType(
                collection,
                ContentTypes.WallPostsContentTypeId,
                "$Resources:GSoft.Dynamite.Examples.Global,ContentType_WallPost;");

            this._contentTypeHelper.EnsureFieldInContentType(contentType, this.WallPostFields);

            contentType.Group = "$Resources:GSoft.Dynamite.Examples.Global,ContentGroup;";
            contentType.Description = "$Resources:GSoft.Dynamite.Examples.Global,ContentType_WallPostDescription;";
            contentType.Update(true);

            this._taxonomyHelper.EnsureTaxonomyEventReceivers(contentType.EventReceivers);
        }
        public virtual SPContentType CreateNewContentType(SPContentTypeCollection contentTypeCollection, SPContentTypeId contentTypeId, string contentTypeName)
        {
            var result = new SPContentType(contentTypeId, contentTypeCollection, contentTypeName);

            return result;
        }
 private static SPContentType FindContentTypeWithField(SPContentTypeCollection contentTypeCollection, TaxonomyField fieldToSet)
 {
     return contentTypeCollection.Cast<SPContentType>().FirstOrDefault(ct =>
     {
         return ct.Fields.Cast<SPField>().Any(spField => spField.InternalName == fieldToSet.InternalName);
     });
 }
 private SPContentType FindContentTypeByName(SPContentTypeCollection contentTypes, string contentTypeName)
 {
     return contentTypes.OfType<SPContentType>()
                        .FirstOrDefault(ct => ct.Name.ToUpper() == contentTypeName.ToUpper());
 }