示例#1
0
 public static void EnsureContentType(this SPContentTypeCollection contentTypeCollection, SPContentTypeMetadata contentTypeMetadata, SPWeb parentWeb = null)
 {
     if (contentTypeCollection.GetContentType(contentTypeMetadata) == null)
     {
         contentTypeCollection.Add(contentTypeMetadata, parentWeb);
     }
 }
示例#2
0
        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);
                }
            }
        }
示例#3
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";
        }
示例#4
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);
            }
        }
        /// <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);
        }
        public SPContentTypeInstance Add(SPContentTypeInstance contentType)
        {
            if (contentType == null)
            {
                throw new JavaScriptException(this.Engine, "Error", "When adding a content type to a content type collection, the content type must be provided as the first argument.");
            }

            var addedContentType = m_contentTypeCollection.Add(contentType.ContentType);

            return(addedContentType == null
        ? null
        : new SPContentTypeInstance(this.Engine.Object.InstancePrototype, addedContentType));
        }
        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 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 content type.
        /// </summary>
        /// <param name="targetUrl">The target URL.</param>
        /// <param name="sourceCT">The source content type.</param>
        /// <param name="sourceFields">The source fields.</param>
        private void CreateContentType(string targetUrl, SPContentType sourceCT, SPFieldCollection sourceFields)
        {
            // Make sure any parent content types exist - they have to be there before we can create this content type.
            if (_availableTargetContentTypes[sourceCT.Parent.Id] == null)
            {
                Logger.Write("Progress: Parent of content type '{0}' does not exist - creating...", sourceCT.Name);

                CreateContentType(targetUrl, sourceCT.Parent, sourceFields);

                // Reset the fields and content types.
                GetAvailableTargetContentTypes(targetUrl);
            }

            Logger.Write("Progress: Creating content type '{0}'...", sourceCT.Name);

            // Create a new content type using information from the source content type.
            SPContentType newCT = new SPContentType(sourceCT.Id, _targetContentTypes, sourceCT.Name);

            Logger.Write("Progress: Setting fields for content type '{0}'...", sourceCT.Name);

            // Set all the core properties for the content type.
            newCT.Group                       = sourceCT.Group;
            newCT.Hidden                      = sourceCT.Hidden;
            newCT.NewDocumentControl          = sourceCT.NewDocumentControl;
            newCT.NewFormTemplateName         = sourceCT.NewFormTemplateName;
            newCT.NewFormUrl                  = sourceCT.NewFormUrl;
            newCT.ReadOnly                    = sourceCT.ReadOnly;
            newCT.RequireClientRenderingOnNew = sourceCT.RequireClientRenderingOnNew;
            newCT.Description                 = sourceCT.Description;
            newCT.DisplayFormTemplateName     = sourceCT.DisplayFormTemplateName;
            newCT.DisplayFormUrl              = sourceCT.DisplayFormUrl;
            newCT.EditFormTemplateName        = sourceCT.EditFormTemplateName;
            newCT.EditFormUrl                 = sourceCT.EditFormUrl;
            newCT.MobileDisplayFormUrl        = sourceCT.MobileDisplayFormUrl;
            newCT.MobileEditFormUrl           = sourceCT.MobileEditFormUrl;
            newCT.MobileNewFormUrl            = sourceCT.MobileNewFormUrl;
            newCT.RequireClientRenderingOnNew = sourceCT.RequireClientRenderingOnNew;

            Logger.Write("Progress: Adding content type '{0}' to collection...", sourceCT.Name);

            // Add the content type to the content types collection and update all the settings.
            _targetContentTypes.Add(newCT);
            newCT.Update();

            // Add all the peripheral items

            try
            {
                if (_copyColumns)
                {
                    Logger.Write("Progress: Adding site columns for content type '{0}'...", sourceCT.Name);

                    AddSiteColumns(newCT, sourceCT, sourceFields);
                }

                if (_copyWorkflows)
                {
                    Logger.Write("Progress: Adding workflow associations for content type '{0}'...", sourceCT.Name);

                    AddWorkflowAssociations(newCT, sourceCT);
                }

                if (_copyDocTemplate)
                {
                    Logger.Write("Progress: Adding document template for content type '{0}'...", sourceCT.Name);

                    AddDocumentTemplate(newCT, sourceCT);
                }

                if (_copyDocConversions)
                {
                    Logger.Write("Progress: Adding document conversion settings for content type '{0}'...", sourceCT.Name);

                    AddDocumentConversionSettings(newCT, sourceCT);
                }

                if (_copyPolicies)
                {
                    Logger.Write("Progress: Adding information rights policies for content type '{0}'...", sourceCT.Name);

                    AddInformationRightsPolicies(newCT, sourceCT);
                }

                if (_copyDocInfoPanel)
                {
                    Logger.Write("Progress: Adding document information panel for content type '{0}'...", sourceCT.Name);

                    AddDocumentInfoPanelToContentType(sourceCT, newCT);
                }
            }
            finally
            {
                newCT.ParentWeb.Site.Dispose();
                newCT.ParentWeb.Dispose();
            }
        }
        public virtual SPContentType AddContentTypeToCollection(SPContentTypeCollection contentTypeCollection, SPContentType contentType)
        {
            var result = contentTypeCollection.Add(contentType);

            return result;
        }