예제 #1
0
        public SPContentType Read(SPList list)
        {
            SPContentType ct = null;

            if (list == null)
            {
                if (Guid.Empty != m_WebGuid && Guid.Empty != m_SiteGuid)
                {
                    using (SPSite site = new SPSite(m_SiteGuid))
                    {
                        SPWeb web = site.OpenWeb(m_WebGuid);
                        ct = Read(web);
                    }
                }
            }
            else
            {
                if (m_ctId.ToString() != "0x")
                {
                    ct = list.ContentTypes[m_ctId];
                }
                else if (!string.IsNullOrEmpty(m_ContentTypeName))
                {
                    ct = list.ContentTypes[m_ContentTypeName];
                }
            }

            if (ct == null)
            {
                throw new SPCmdletPipeBindException("The SPContentType Pipebind object could not be found.");
            }
            return(ct);
        }
 public virtual void EnsureCollectionIsUpdateble(SPWeb web, SPContentTypeId contentTypeId)
 {
     if (web.ContentTypes[contentTypeId] == null)
     {
         if (web.AvailableContentTypes[contentTypeId] != null)
         {
             throw new SPGENGeneralException("The content type '" + contentTypeId.ToString() + "' doesn't exist in the updateble content type collection on this web but is a member of an ancestor web.");
         }
         else
         {
             throw new SPGENGeneralException("The content type '" + contentTypeId.ToString() + "' doesn't exist in the updateble content type collection on this web.");
         }
     }
 }
    public static SPListItem CreateLinkToDocumentFile(SPDocumentLibrary list, SPContentTypeId contentTypeId, string name, SPFieldUrlValue urlVal, string overrideIcon = null)
    {
        SPContentType contentType = list.ContentTypes[contentTypeId];
        string        extension   = "";

        if (urlVal != null && urlVal.Url != null)
        {
            extension = Path.GetExtension((urlVal.Url).Trim()).TrimStart(".".ToCharArray());
        }
        SPFolder         currentFolder = list.RootFolder;
        SPFileCollection files         = currentFolder.Files;
        string           fileUrl       = string.Concat(currentFolder.Url, "/", name, ".aspx");
        string           fileTemplate  = "<%@ Assembly Name='{0}' %>\r\n <%@ Register TagPrefix='SharePoint' Namespace='Microsoft.SharePoint.WebControls' Assembly='Microsoft.SharePoint' %>\r\n <%@ Import Namespace='System.IO' %>\r\n <%@ Import Namespace='Microsoft.SharePoint' %>\r\n <%@ Import Namespace='Microsoft.SharePoint.Utilities' %>\r\n <%@ Import Namespace='Microsoft.SharePoint.WebControls' %>\r\n <html>\r\n <head> <meta name='progid' content='SharePoint.Link' /> </head>\r\n <body>\r\n <form id='Form1' runat='server'>\r\n <SharePoint:UrlRedirector id='Redirector1' runat='server' />\r\n </form>\r\n </body>\r\n </html>";
        StringBuilder    fileContent   = new StringBuilder(fileTemplate.Length + 400);

        fileContent.AppendFormat(fileTemplate, typeof(SPDocumentLibrary).Assembly.FullName);
        Hashtable       properties = new Hashtable();
        SPContentTypeId ctId       = contentType.Id;

        properties["ContentTypeId"] = ctId.ToString();
        SPFile     file = files.Add(fileUrl, new MemoryStream((new UTF8Encoding()).GetBytes(fileContent.ToString())), properties, false, false);
        SPListItem item = file.Item;

        item[FLD_URL] = urlVal;
        if (list.Fields.Contains(g_guidIconOverride))
        {
            item[g_guidIconOverride] = string.Concat("|", overrideIcon ?? extension, "|");
        }
        item.IconOverlay = "linkoverlay.gif";
        item.UpdateOverwriteVersion();
        return(item);
    }
예제 #4
0
 private static void SaveAssemblyName(SPSite site, SPContentTypeId contentTypeId, Assembly assembly)
 {
     using (site.RootWeb.GetAllowUnsafeUpdatesScope()) {
         site.RootWeb.AllProperties["SPModel." + contentTypeId.ToString().ToLower() + ".Assembly"] = assembly.FullName;
         site.RootWeb.Update();
     }
 }
예제 #5
0
        //public static DefinitionBase OnCreating(this DefinitionBase model, Action<ListDefinition, SPList> action)
        //{
        //    model.RegisterModelUpdatingEvent(action);

        //    return model;
        //}

        //public static DefinitionBase OnCreated(this DefinitionBase model, Action<ListDefinition, SPList> action)
        //{
        //    model.RegisterModelUpdatedEvent(action);

        //    return model;
        //}

        #endregion

        #region add content type

        public static ModelNode AddContentTypeLink(this ModelNode model, SPContentTypeId contentTypeId)
        {
            return(ContentTypeLinkDefinitionSyntax.AddContentTypeLink(model, new ContentTypeLinkDefinition
            {
                ContentTypeId = contentTypeId.ToString()
            }));
        }
예제 #6
0
 public static ModelNode AddContentTypeLink(this ModelNode model, SPContentTypeId contentTypeId)
 {
     return ContentTypeLinkDefinitionSyntax.AddContentTypeLink(model, new ContentTypeLinkDefinition
     {
         ContentTypeId = contentTypeId.ToString()
     });
 }
        /// <summary>
        /// Builds a valid child content type ID from a parent and a unique identifier
        /// </summary>
        /// <param name="parentContentTypeId">The parent content type's ID</param>
        /// <param name="childContentTypeIdValue">The child CT's unique identifier</param>
        /// <returns>A correctly formatted child CT ID string</returns>
        public static string ConcatAsContentTypeId(SPContentTypeId parentContentTypeId, Guid childContentTypeIdValue)
        {
            const string Format = "{0}00{1}";
            var cleanedUpGuidString = childContentTypeIdValue.ToString().Replace("{", string.Empty).Replace("}", string.Empty).Replace("-", string.Empty);

            return string.Format(CultureInfo.InvariantCulture, Format, parentContentTypeId.ToString(), cleanedUpGuidString);
        }
예제 #8
0
        /// <summary>
        /// Creates a new child content type ID
        /// </summary>
        /// <param name="parentContentTypeId">The parent CT ID</param>
        /// <param name="childContentTypeUniqueId">The unique ID for the new child CT</param>
        /// <returns>The new content type ID</returns>
        public static SPContentTypeId CreateChild(SPContentTypeId parentContentTypeId, string childContentTypeUniqueId)
        {
            if (parentContentTypeId == null)
            {
                throw new ArgumentNullException("parentContentTypeId");
            }

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

            string childIdString = childContentTypeUniqueId.ToString(CultureInfo.InvariantCulture);
            string parentId      = parentContentTypeId.ToString();

            string newContentTypeIdString = string.Format(
                CultureInfo.InvariantCulture,
                "{0}{1}",       // use no separator when you use a dirty string-based CT discriminator: at this point, your loss your CTID logic is messed up.
                parentId,
                childIdString);

            return(new SPContentTypeId(newContentTypeIdString));
        }
예제 #9
0
        /// <summary>
        /// Creates a new child content type ID
        /// </summary>
        /// <param name="parentContentTypeId">The parent CT ID</param>
        /// <param name="childContentTypeUniqueId">The unique ID for the new child CT</param>
        /// <returns>The new content type ID</returns>
        public static SPContentTypeId CreateChild(SPContentTypeId parentContentTypeId, Guid childContentTypeUniqueId)
        {
            if (parentContentTypeId == null)
            {
                throw new ArgumentNullException("parentContentTypeId");
            }

            if (childContentTypeUniqueId == null || childContentTypeUniqueId == Guid.Empty)
            {
                throw new ArgumentNullException("childContentTypeUniqueId");
            }

            string childGuidString = childContentTypeUniqueId.ToString("N", CultureInfo.InvariantCulture).ToUpperInvariant();
            string parentId        = parentContentTypeId.ToString();

            string newContentTypeIdString = string.Format(
                CultureInfo.InvariantCulture,
                "{0}00{1}",     // "00" as separator between CT Guids
                parentId,
                childGuidString);

            return(new SPContentTypeId(newContentTypeIdString));
        }
예제 #10
0
        /// <summary>
        /// Creates a new child content type ID
        /// </summary>
        /// <param name="parentContentTypeId">The parent CT ID</param>
        /// <param name="childContentTypeUniqueId">The unique ID for the new child CT</param>
        /// <returns>The new content type ID</returns>
        public static SPContentTypeId CreateChild(SPContentTypeId parentContentTypeId, Guid childContentTypeUniqueId)
        {
            if (parentContentTypeId == null)
            {
                throw new ArgumentNullException("parentContentTypeId");
            }

            if (childContentTypeUniqueId == null || childContentTypeUniqueId == Guid.Empty)
            {
                throw new ArgumentNullException("childContentTypeUniqueId");
            }

            string childGuidString = childContentTypeUniqueId.ToString("N", CultureInfo.InvariantCulture).ToUpperInvariant();
            string parentId = parentContentTypeId.ToString();

            string newContentTypeIdString = string.Format(
                CultureInfo.InvariantCulture,
                "{0}00{1}",     // "00" as separator between CT Guids
                parentId,
                childGuidString);

            return new SPContentTypeId(newContentTypeIdString);
        }
예제 #11
0
        /// <summary>
        /// Creates a new child content type ID
        /// </summary>
        /// <param name="parentContentTypeId">The parent CT ID</param>
        /// <param name="childContentTypeUniqueId">The unique ID for the new child CT</param>
        /// <returns>The new content type ID</returns>
        public static SPContentTypeId CreateChild(SPContentTypeId parentContentTypeId, int childContentTypeUniqueId)
        {
            if (parentContentTypeId == null)
            {
                throw new ArgumentNullException("parentContentTypeId");
            }

            if (childContentTypeUniqueId <= 0)
            {
                throw new ArgumentOutOfRangeException("childContentTypeUniqueId", "Child content type discriminator integer should be larger than 0.");
            }

            string childIdString = childContentTypeUniqueId.ToString(CultureInfo.InvariantCulture);
            string parentId = parentContentTypeId.ToString();

            string newContentTypeIdString = string.Format(
                CultureInfo.InvariantCulture,
                "{0}0{1}",      // "0" as separator between CT int IDs
                parentId,
                childIdString);

            return new SPContentTypeId(newContentTypeIdString);
        }
        private static SPContentType AddContentTypeToList(SPContentTypeId spContentTypeId, SPList list, SPWeb web)
        {
            SPContentType contentType = web.ContentTypes[spContentTypeId];

            if (contentType != null)
            {
                list.ContentTypesEnabled = true;
                list.Update();

                if (list.ContentTypes[spContentTypeId] == null)
                {
                    string IdInCollection =
                        list.ContentTypes.BestMatch(spContentTypeId).ToString();

                    if (!IdInCollection.Contains(spContentTypeId.ToString()))
                    {
                        list.ContentTypes.Add(contentType);
                        list.Update();
                    }
                }
            }
            return(contentType);
        }
예제 #13
0
        /// <summary>
        /// Creates a new child content type ID
        /// </summary>
        /// <param name="parentContentTypeId">The parent CT ID</param>
        /// <param name="childContentTypeUniqueId">The unique ID for the new child CT</param>
        /// <returns>The new content type ID</returns>
        public static SPContentTypeId CreateChild(SPContentTypeId parentContentTypeId, int childContentTypeUniqueId)
        {
            if (parentContentTypeId == null)
            {
                throw new ArgumentNullException("parentContentTypeId");
            }

            if (childContentTypeUniqueId <= 0)
            {
                throw new ArgumentOutOfRangeException("childContentTypeUniqueId", "Child content type discriminator integer should be larger than 0.");
            }

            string childIdString = childContentTypeUniqueId.ToString(CultureInfo.InvariantCulture);
            string parentId      = parentContentTypeId.ToString();

            string newContentTypeIdString = string.Format(
                CultureInfo.InvariantCulture,
                "{0}0{1}",      // "0" as separator between CT int IDs
                parentId,
                childIdString);

            return(new SPContentTypeId(newContentTypeIdString));
        }
예제 #14
0
 /// <summary>
 /// Creates a CAML query to determine whether [is or inherits content type] [the specified content type identifier].
 /// </summary>
 /// <param name="contentTypeId">The content type identifier.</param>
 /// <returns>
 /// A string representation of the CAML query.
 /// </returns>
 public string IsOrInheritsContentType(SPContentTypeId contentTypeId)
 {
     return(this.BeginsWith(this.FieldRef("ContentTypeId"), this.Value("ContentTypeId", contentTypeId.ToString())));
 }
예제 #15
0
 /// <summary>
 /// Creates a CAML query to determine whether [is content type] [the specified content type identifier].
 /// </summary>
 /// <param name="contentTypeId">The content type identifier.</param>
 /// <returns>
 /// A string representation of the CAML query.
 /// </returns>
 public string IsContentType(SPContentTypeId contentTypeId)
 {
     return this.Equal(this.FieldRef("ContentTypeId"), this.Value("ContentTypeId", contentTypeId.ToString()));
 }
예제 #16
0
 /// <summary>
 /// Creates a CAML query to determine whether [is or inherits content type] [the specified content type identifier].
 /// </summary>
 /// <param name="contentTypeId">The content type identifier.</param>
 /// <returns>
 /// A string representation of the CAML query.
 /// </returns>
 public string IsOrInheritsContentType(SPContentTypeId contentTypeId)
 {
     return this.BeginsWith(this.FieldRef("ContentTypeId"), this.Value("ContentTypeId", contentTypeId.ToString()));
 }
예제 #17
0
        /// <summary>
        /// Creates a new child content type ID
        /// </summary>
        /// <param name="parentContentTypeId">The parent CT ID</param>
        /// <param name="childContentTypeUniqueId">The unique ID for the new child CT</param>
        /// <returns>The new content type ID</returns>
        public static SPContentTypeId CreateChild(SPContentTypeId parentContentTypeId, string childContentTypeUniqueId)
        {
            if (parentContentTypeId == null)
            {
                throw new ArgumentNullException("parentContentTypeId");
            }

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

            string childIdString = childContentTypeUniqueId.ToString(CultureInfo.InvariantCulture);
            string parentId = parentContentTypeId.ToString();

            string newContentTypeIdString = string.Format(
                CultureInfo.InvariantCulture,
                "{0}{1}",       // use no separator when you use a dirty string-based CT discriminator: at this point, your loss your CTID logic is messed up. 
                parentId,
                childIdString);

            return new SPContentTypeId(newContentTypeIdString);
        }
예제 #18
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);
        }
 /// <summary>
 ///     Specifies a ContentTypeId value
 /// </summary>
 /// <param name="contentTypeId">the ContentTypeId value to be expressed in CAML</param>
 /// <returns>a new CAML Value element</returns>
 public static string Value(SPContentTypeId contentTypeId)
 {
     return(ValueForContentTypeIdAsString(contentTypeId.ToString()));
 }
예제 #20
0
 /// <summary>
 /// Creates a CAML query to determine whether [is content type] [the specified content type identifier].
 /// </summary>
 /// <param name="contentTypeId">The content type identifier.</param>
 /// <returns>
 /// A string representation of the CAML query.
 /// </returns>
 public string IsContentType(SPContentTypeId contentTypeId)
 {
     return(this.Equal(this.FieldRef("ContentTypeId"), this.Value("ContentTypeId", contentTypeId.ToString())));
 }